|
| 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 | +}); |
0 commit comments