|
| 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 | +import type {RelayResolverModelWithContextTestFragment$key} from './__generated__/RelayResolverModelWithContextTestFragment.graphql'; |
| 15 | +import type {TestResolverContextType} from 'relay-runtime/mutations/__tests__/TestResolverContextType'; |
| 16 | + |
| 17 | +const React = require('react'); |
| 18 | +const { |
| 19 | + RelayEnvironmentProvider, |
| 20 | + useClientQuery, |
| 21 | + useFragment, |
| 22 | +} = require('react-relay'); |
| 23 | +const TestRenderer = require('react-test-renderer'); |
| 24 | +const {Observable} = require('relay-runtime'); |
| 25 | +const RelayNetwork = require('relay-runtime/network/RelayNetwork'); |
| 26 | +const {graphql} = require('relay-runtime/query/GraphQLTag'); |
| 27 | +const { |
| 28 | + addTodo, |
| 29 | + resetStore, |
| 30 | +} = require('relay-runtime/store/__tests__/resolvers/ExampleTodoStore'); |
| 31 | +const RelayModernEnvironment = require('relay-runtime/store/RelayModernEnvironment'); |
| 32 | +const RelayModernStore = require('relay-runtime/store/RelayModernStore.js'); |
| 33 | +const RelayRecordSource = require('relay-runtime/store/RelayRecordSource'); |
| 34 | +const { |
| 35 | + disallowConsoleErrors, |
| 36 | + disallowWarnings, |
| 37 | + injectPromisePolyfill__DEPRECATED, |
| 38 | +} = require('relay-test-utils-internal'); |
| 39 | + |
| 40 | +injectPromisePolyfill__DEPRECATED(); |
| 41 | +disallowWarnings(); |
| 42 | +disallowConsoleErrors(); |
| 43 | + |
| 44 | +beforeEach(() => { |
| 45 | + resetStore(() => {}); |
| 46 | +}); |
| 47 | + |
| 48 | +function EnvironmentWrapper({ |
| 49 | + children, |
| 50 | + environment, |
| 51 | +}: { |
| 52 | + children: React.Node, |
| 53 | + environment: RelayModernEnvironment, |
| 54 | +}) { |
| 55 | + return ( |
| 56 | + <RelayEnvironmentProvider environment={environment}> |
| 57 | + <React.Suspense fallback="Loading...">{children}</React.Suspense> |
| 58 | + </RelayEnvironmentProvider> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +const RelayResolverModelWithContextTestFragment = graphql` |
| 63 | + fragment RelayResolverModelWithContextTestFragment on TodoModel { |
| 64 | + id |
| 65 | + description |
| 66 | + another_value_from_context |
| 67 | + } |
| 68 | +`; |
| 69 | + |
| 70 | +const RelayResolverModelWithContextTestQuery = graphql` |
| 71 | + query RelayResolverModelWithContextTestQuery($id: ID!) { |
| 72 | + todo_model(todoID: $id) { |
| 73 | + ...RelayResolverModelWithContextTestFragment |
| 74 | + } |
| 75 | + } |
| 76 | +`; |
| 77 | + |
| 78 | +describe('RelayResolverModelWithContext', () => { |
| 79 | + function TodoComponent(props: { |
| 80 | + fragmentKey: ?RelayResolverModelWithContextTestFragment$key, |
| 81 | + }) { |
| 82 | + const data = useFragment( |
| 83 | + RelayResolverModelWithContextTestFragment, |
| 84 | + props.fragmentKey, |
| 85 | + ); |
| 86 | + if (data == null) { |
| 87 | + return null; |
| 88 | + } |
| 89 | + |
| 90 | + return data.another_value_from_context; |
| 91 | + } |
| 92 | + |
| 93 | + function TodoRootComponent(props: {todoID: string}) { |
| 94 | + const data = useClientQuery(RelayResolverModelWithContextTestQuery, { |
| 95 | + id: props.todoID, |
| 96 | + }); |
| 97 | + if (data?.todo_model == null) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + return <TodoComponent fragmentKey={data?.todo_model} />; |
| 102 | + } |
| 103 | + |
| 104 | + test('returns a value from context when resolverDataInjector is used', () => { |
| 105 | + const resolverContext: TestResolverContextType = { |
| 106 | + greeting: { |
| 107 | + myHello: 'This is a value from context', |
| 108 | + }, |
| 109 | + counter: Observable.create<number>(observer => { |
| 110 | + observer.next(10); |
| 111 | + }), |
| 112 | + }; |
| 113 | + |
| 114 | + const store = new RelayModernStore(RelayRecordSource.create(), { |
| 115 | + resolverContext, |
| 116 | + }); |
| 117 | + const environment = new RelayModernEnvironment({ |
| 118 | + network: RelayNetwork.create(jest.fn()), |
| 119 | + store, |
| 120 | + }); |
| 121 | + |
| 122 | + addTodo('Test todo'); |
| 123 | + |
| 124 | + let renderer; |
| 125 | + TestRenderer.act(() => { |
| 126 | + renderer = TestRenderer.create( |
| 127 | + <EnvironmentWrapper environment={environment}> |
| 128 | + <TodoRootComponent todoID="todo-1" /> |
| 129 | + </EnvironmentWrapper>, |
| 130 | + ); |
| 131 | + }); |
| 132 | + expect(renderer?.toJSON()).toEqual('This is a value from context'); |
| 133 | + }); |
| 134 | +}); |
0 commit comments