Skip to content

Commit 66d0437

Browse files
authored
add more tests for findGraphQLTags, fix cache/schema tests (#2533)
1 parent 737d418 commit 66d0437

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

packages/graphql-language-service-server/src/__tests__/GraphQLCache-test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
jest.mock('cross-undici-fetch', () => ({
1111
fetch: require('fetch-mock').fetchHandler,
12+
AbortController: global.AbortController,
1213
}));
14+
1315
import { GraphQLSchema } from 'graphql/type';
1416
import { parse } from 'graphql/language';
1517
import { loadConfig, GraphQLExtensionDeclaration } from 'graphql-config';
@@ -72,8 +74,7 @@ describe('GraphQLCache', () => {
7274
});
7375
});
7476

75-
// TODO: figure out mocking undici
76-
describe.skip('getSchema', () => {
77+
describe('getSchema', () => {
7778
it('generates the schema correctly for the test app config', async () => {
7879
const schema = await cache.getSchema('testWithSchema');
7980
expect(schema instanceof GraphQLSchema).toEqual(true);

packages/graphql-language-service-server/src/__tests__/findGraphQLTags-test.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ query Test {
140140
`);
141141
});
142142

143+
it('finds queries with nested graphql.experimental template tag expression', async () => {
144+
const text = `const query = graphql.experimental\` query {} \``;
145+
146+
const contents = findGraphQLTags(text, '.ts');
147+
expect(contents[0].template).toEqual(` query {} `);
148+
});
149+
143150
it('finds queries with nested template tag expressions', async () => {
144151
const text = `export default {
145152
else: () => gql\` query {} \`
@@ -151,13 +158,39 @@ query Test {
151158

152159
it('finds queries with template tags inside call expressions', async () => {
153160
const text = `something({
154-
else: () => gql\` query {} \`
161+
else: () => graphql\` query {} \`
155162
})`;
156163

157164
const contents = findGraphQLTags(text, '.ts');
158165
expect(contents[0].template).toEqual(` query {} `);
159166
});
160167

168+
it('finds multiple queries in a single file', async () => {
169+
const text = `something({
170+
else: () => gql\` query {} \`
171+
})
172+
const query = graphql\`query myQuery {}\``;
173+
174+
const contents = findGraphQLTags(text, '.ts');
175+
176+
expect(contents.length).toEqual(2);
177+
178+
// let's double check that we're properly
179+
// extracting the positions of each embedded string
180+
expect(contents[0].range.start.line).toEqual(1);
181+
expect(contents[0].range.start.character).toEqual(18);
182+
expect(contents[0].range.end.line).toEqual(1);
183+
expect(contents[0].range.end.character).toEqual(28);
184+
expect(contents[0].template).toEqual(` query {} `);
185+
186+
// and the second string, with correct positional information!
187+
expect(contents[1].range.start.line).toEqual(3);
188+
expect(contents[1].range.start.character).toEqual(22);
189+
expect(contents[1].range.end.line).toEqual(3);
190+
expect(contents[1].range.end.character).toEqual(38);
191+
expect(contents[1].template).toEqual(`query myQuery {}`);
192+
});
193+
161194
it('ignores non gql tagged templates', async () => {
162195
const text = `
163196
// @flow

0 commit comments

Comments
 (0)