Skip to content

Commit 5efa66f

Browse files
committed
Improve tests
1 parent 7fba87e commit 5efa66f

5 files changed

Lines changed: 363 additions & 61 deletions

packages/relay-runtime/store/__tests__/EmptyChecker-test.js

Lines changed: 0 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -298,51 +298,4 @@ describe('EmptyChecker', () => {
298298
expect(isEmpty(operation.root)).toBe(false);
299299
});
300300
});
301-
302-
describe('store integration', () => {
303-
let RelayFeatureFlags;
304-
let RelayModernStore;
305-
let RelayRecordSource;
306-
307-
beforeEach(() => {
308-
RelayFeatureFlags = require('../../util/RelayFeatureFlags');
309-
RelayModernStore = require('../RelayModernStore');
310-
RelayRecordSource = require('../RelayRecordSource');
311-
});
312-
313-
it('emits log event when query is empty', () => {
314-
const originalFlag = RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK;
315-
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = true;
316-
317-
const logEvents = [];
318-
const source = RelayRecordSource.create();
319-
const store = new RelayModernStore(source, {
320-
gcReleaseBufferSize: 0,
321-
log: event => {
322-
logEvents.push(event);
323-
},
324-
});
325-
326-
const query = graphql`
327-
query EmptyCheckerTestEmptyQuery($cond: Boolean!) {
328-
me @include(if: $cond) {
329-
id
330-
}
331-
}
332-
`;
333-
const operation = createOperationDescriptor(query, {cond: false});
334-
335-
const availability = store.check(operation);
336-
337-
expect(availability).toEqual({status: 'empty'});
338-
expect(logEvents).toContainEqual(
339-
expect.objectContaining({
340-
name: 'store.check.empty',
341-
operation,
342-
}),
343-
);
344-
345-
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = originalFlag;
346-
});
347-
});
348301
});
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict-local
8+
* @format
9+
* @oncall relay
10+
*/
11+
12+
'use strict';
13+
14+
const RelayNetwork = require('../../network/RelayNetwork');
15+
const {graphql} = require('../../query/GraphQLTag');
16+
const RelayFeatureFlags = require('../../util/RelayFeatureFlags');
17+
const RelayModernEnvironment = require('../RelayModernEnvironment');
18+
const {
19+
createOperationDescriptor,
20+
} = require('../RelayModernOperationDescriptor');
21+
const {disallowWarnings} = require('relay-test-utils-internal');
22+
23+
disallowWarnings();
24+
25+
describe('RelayModernEnvironment empty query handling', () => {
26+
let originalFlag;
27+
28+
beforeEach(() => {
29+
originalFlag = RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK;
30+
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = true;
31+
});
32+
33+
afterEach(() => {
34+
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = originalFlag;
35+
});
36+
37+
it('isEmpty returns true for empty operations and false for non-empty', () => {
38+
const environment = new RelayModernEnvironment({
39+
network: RelayNetwork.create(() => {
40+
throw new Error(
41+
'Network requests should not be made during this test',
42+
);
43+
}),
44+
});
45+
46+
const query = graphql`
47+
query RelayModernEnvironmentEmptyTestIsEmptyQuery($cond: Boolean!) {
48+
me @include(if: $cond) {
49+
id
50+
}
51+
}
52+
`;
53+
const emptyOperation = createOperationDescriptor(query, {cond: false});
54+
const nonEmptyOperation = createOperationDescriptor(query, {cond: true});
55+
56+
expect(environment.isEmpty(emptyOperation)).toBe(true);
57+
expect(environment.isEmpty(nonEmptyOperation)).toBe(false);
58+
});
59+
60+
it('skips network fetch when query is empty', () => {
61+
const logEvents: Array<mixed> = [];
62+
const environment = new RelayModernEnvironment({
63+
network: RelayNetwork.create(() => {
64+
throw new Error(
65+
'Network requests should not be made during this test',
66+
);
67+
}),
68+
log: event => {
69+
logEvents.push(event);
70+
},
71+
});
72+
73+
const query = graphql`
74+
query RelayModernEnvironmentEmptyTestExecuteQuery($cond: Boolean!) {
75+
me @include(if: $cond) {
76+
id
77+
}
78+
}
79+
`;
80+
const operation = createOperationDescriptor(query, {cond: false});
81+
82+
const callbacks = {
83+
complete: jest.fn<[], void>(),
84+
error: jest.fn<[Error], void>(),
85+
next: jest.fn<[mixed], void>(),
86+
};
87+
environment.execute({operation}).subscribe(callbacks);
88+
89+
// Should emit execute.skipped log event
90+
expect(logEvents).toContainEqual(
91+
expect.objectContaining({
92+
name: 'execute.skipped',
93+
reason: 'empty',
94+
}),
95+
);
96+
97+
// Observable should complete with empty data
98+
expect(callbacks.next).toHaveBeenCalledWith({data: {}});
99+
expect(callbacks.complete).toHaveBeenCalled();
100+
expect(callbacks.error).not.toHaveBeenCalled();
101+
});
102+
103+
it('isEmpty respects feature flag', () => {
104+
const logEvents: Array<mixed> = [];
105+
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = false;
106+
107+
const environment = new RelayModernEnvironment({
108+
network: RelayNetwork.create(() => {
109+
throw new Error(
110+
'Network requests should not be made during this test',
111+
);
112+
}),
113+
log: event => {
114+
logEvents.push(event);
115+
},
116+
});
117+
118+
const query = graphql`
119+
query RelayModernEnvironmentEmptyTestFeatureFlagQuery($cond: Boolean!) {
120+
me @include(if: $cond) {
121+
id
122+
}
123+
}
124+
`;
125+
const operation = createOperationDescriptor(query, {cond: false});
126+
127+
// When flag is disabled, isEmpty always returns false
128+
expect(environment.isEmpty(operation)).toBe(false);
129+
130+
// No execute.skipped logs should be emitted when checking isEmpty
131+
expect(
132+
logEvents.filter(e => (e: $FlowFixMe).name === 'execute.skipped'),
133+
).toHaveLength(0);
134+
135+
// When flag is enabled, isEmpty returns true for empty queries
136+
RelayFeatureFlags.ENABLE_EMPTY_QUERY_CHECK = true;
137+
expect(environment.isEmpty(operation)).toBe(true);
138+
});
139+
});

packages/relay-runtime/store/__tests__/__generated__/EmptyCheckerTestEmptyQuery.graphql.js renamed to packages/relay-runtime/store/__tests__/__generated__/RelayModernEnvironmentEmptyTestExecuteQuery.graphql.js

Lines changed: 14 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/relay-runtime/store/__tests__/__generated__/RelayModernEnvironmentEmptyTestFeatureFlagQuery.graphql.js

Lines changed: 105 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)