Skip to content

add more tests for findGraphQLTags, fix cache/schema tests #2533

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

jest.mock('cross-undici-fetch', () => ({
fetch: require('fetch-mock').fetchHandler,
AbortController: global.AbortController,
}));

import { GraphQLSchema } from 'graphql/type';
import { parse } from 'graphql/language';
import { loadConfig, GraphQLExtensionDeclaration } from 'graphql-config';
Expand Down Expand Up @@ -72,8 +74,7 @@ describe('GraphQLCache', () => {
});
});

// TODO: figure out mocking undici
describe.skip('getSchema', () => {
describe('getSchema', () => {
it('generates the schema correctly for the test app config', async () => {
const schema = await cache.getSchema('testWithSchema');
expect(schema instanceof GraphQLSchema).toEqual(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ query Test {
`);
});

it('finds queries with nested graphql.experimental template tag expression', async () => {
const text = `const query = graphql.experimental\` query {} \``;

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

it('finds queries with nested template tag expressions', async () => {
const text = `export default {
else: () => gql\` query {} \`
Expand All @@ -151,13 +158,39 @@ query Test {

it('finds queries with template tags inside call expressions', async () => {
const text = `something({
else: () => gql\` query {} \`
else: () => graphql\` query {} \`
})`;

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

it('finds multiple queries in a single file', async () => {
const text = `something({
else: () => gql\` query {} \`
})
const query = graphql\`query myQuery {}\``;

const contents = findGraphQLTags(text, '.ts');

expect(contents.length).toEqual(2);

// let's double check that we're properly
// extracting the positions of each embedded string
expect(contents[0].range.start.line).toEqual(1);
expect(contents[0].range.start.character).toEqual(18);
expect(contents[0].range.end.line).toEqual(1);
expect(contents[0].range.end.character).toEqual(28);
expect(contents[0].template).toEqual(` query {} `);

// and the second string, with correct positional information!
expect(contents[1].range.start.line).toEqual(3);
expect(contents[1].range.start.character).toEqual(22);
expect(contents[1].range.end.line).toEqual(3);
expect(contents[1].range.end.character).toEqual(38);
expect(contents[1].template).toEqual(`query myQuery {}`);
});

it('ignores non gql tagged templates', async () => {
const text = `
// @flow
Expand Down