-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
RelayFindGraphQLTags-test.js
207 lines (190 loc) · 6.63 KB
/
RelayFindGraphQLTags-test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @format
* @emails oncall+relay
*/
'use strict';
const FindGraphQLTags = require('../../language/javascript/FindGraphQLTags');
const RelayFindGraphQLTags = require('../RelayFindGraphQLTags');
describe('RelayFindGraphQLTags', () => {
function find(text, absPath: string = '/path/to/FindGraphQLTags.js') {
return RelayFindGraphQLTags.find(FindGraphQLTags.find, text, absPath).map(
tag => tag.template,
);
}
describe('query parsing', () => {
it('parses a simple file', () => {
expect(find('const foo = 1;')).toEqual([]);
});
it('parses graphql templates', () => {
expect(
find(`
const foo = 1;
foo(graphql\`fragment FindGraphQLTags on User { id }\`);
graphql\`fragment FindGraphQLTags on User { name }\`;
createFragmentContainer(Component, {
foo: graphql\`fragment FindGraphQLTags_foo on Page { id }\`,
});
createPaginationContainer(
Component,
{},
{
query: graphql\`query FindGraphQLTagsPaginationQuery { me { id } }\`,
}
);
createRefetchContainer(
Component,
{},
graphql\`query FindGraphQLTagsRefetchQuery { me { id } }\`
);
Relay.createFragmentContainer(Component, {
foo: graphql\`fragment FindGraphQLTags_foo on Page { name }\`,
});
Relay.createPaginationContainer(
Component,
{},
{
query: graphql\`query FindGraphQLTagsPaginationQuery { me { name } }\`,
}
);
Relay.createRefetchContainer(
Component,
{},
graphql\`query FindGraphQLTagsRefetchQuery { me { name } }\`
);
`),
).toEqual([
'fragment FindGraphQLTags on User { id }',
'fragment FindGraphQLTags on User { name }',
'fragment FindGraphQLTags_foo on Page { id }',
'query FindGraphQLTagsPaginationQuery { me { id } }',
'query FindGraphQLTagsRefetchQuery { me { id } }',
'fragment FindGraphQLTags_foo on Page { name }',
'query FindGraphQLTagsPaginationQuery { me { name } }',
'query FindGraphQLTagsRefetchQuery { me { name } }',
]);
});
it('parses modern JS syntax with Flow annotations', () => {
expect(
find(`
class RelayContainer extends React.Component {
// graphql\`this in a comment\`;
_loadMore = (
pageSize: number,
options?: ?RefetchOptions
): ?Disposable => {
graphql\`fragment FindGraphQLTags on User { id }\`;
}
render() {
const x = window?.foo?.bar ?? 'default';
return <>A Fragment!</>;
}
}
`),
).toEqual(['fragment FindGraphQLTags on User { id }']);
});
it('parses JS with functions sharing names with object prototype methods', () => {
expect(
find(`
toString();
foo(graphql\`fragment FindGraphQLTags on User { id }\`);
`),
).toEqual(['fragment FindGraphQLTags on User { id }']);
});
});
describe('query validation', () => {
it('prints correct file numbers in errors', () => {
expect(() => {
find(
'const foo = 1;\n' +
'foo(graphql`\n' +
' fragment FindGraphQLTags on User {\n' +
' ?\n' +
' id\n' +
' }\n' +
'`);\n',
);
}).toThrow('Syntax Error: Cannot parse the unexpected character "?".');
});
});
describe('query name validation', () => {
it('throws for invalid query names', () => {
expect(() => find('graphql`query NotModuleName { me { id } }`;')).toThrow(
'FindGraphQLTags: Operation names in graphql tags must be prefixed with ' +
'the module name and end in "Mutation", "Query", or "Subscription". ' +
'Got `NotModuleName` in module `FindGraphQLTags`.',
);
});
it('parses queries with valid names', () => {
expect(
find('graphql`query FindGraphQLTagsQuery { me { id } }`;'),
).toEqual(['query FindGraphQLTagsQuery { me { id } }']);
});
it('parses queries with valid names from filepath', () => {
expect(
find(
'graphql`query TestComponentQuery { me { id } }`;',
'./PathTo/SuperDuper/TestComponent.js',
),
).toEqual(['query TestComponentQuery { me { id } }']);
expect(
find(
'graphql`query TestComponentQuery { me { id } }`;',
'./PathTo/SuperDuper/TestComponent.react.js',
),
).toEqual(['query TestComponentQuery { me { id } }']);
expect(
find(
'graphql`query TestComponentQuery { me { id } }`;',
'./PathTo/SuperDuper/TestComponent.react.jsx',
),
).toEqual(['query TestComponentQuery { me { id } }']);
expect(
find(
'graphql`query TestComponentQuery { me { id } }`;',
'./PathTo/SuperDuper/TestComponent/index.js',
),
).toEqual(['query TestComponentQuery { me { id } }']);
});
it('throws for invalid top-level fragment names', () => {
expect(() =>
find('graphql`fragment NotModuleName on User { name }`;'),
).toThrow(
'FindGraphQLTags: Fragment names in graphql tags ' +
'must be prefixed with the module name. Got ' +
'`NotModuleName` in module `FindGraphQLTags`.',
);
});
it('parses top-level fragments with valid names', () => {
expect(
find('graphql`fragment FindGraphQLTags on User { name }`;'),
).toEqual(['fragment FindGraphQLTags on User { name }']);
});
it('throws for invalid container fragment names', () => {
expect(() =>
find(`
createFragmentContainer(Foo, {
foo: graphql\`fragment FindGraphQLTags_notFoo on User { name }\`,
});
`),
).toThrow(
'FindGraphQLTags: Container fragment names must be ' +
'`<ModuleName>_<propName>`. Got `FindGraphQLTags_notFoo`, expected ' +
'`FindGraphQLTags_foo`.',
);
});
it('parses container fragments with valid names', () => {
expect(
find(`
createFragmentContainer(Foo, {
foo: graphql\`fragment FindGraphQLTags_foo on User { name }\`,
});
`),
).toEqual(['fragment FindGraphQLTags_foo on User { name }']);
});
});
});