Skip to content

Commit

Permalink
Fix printWithComments (#3124)
Browse files Browse the repository at this point in the history
* Reproduction of printWithComments issue

* Fix tests

* Break tests

* temp fix

* Fix TSC Build

* comments fix

Co-authored-by: Dotan Simha <[email protected]>
Co-authored-by: gilgardosh <[email protected]>
  • Loading branch information
3 people authored Jul 14, 2021
1 parent 9c26b84 commit 650c4a1
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 130 deletions.
43 changes: 31 additions & 12 deletions packages/load/tests/loaders/documents/documents-from-glob.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { loadDocuments, loadDocumentsSync } from '@graphql-tools/load';
import { join } from 'path';
import { separateOperations } from 'graphql';
import { parse, separateOperations } from 'graphql';
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader';
import { CodeFileLoader } from '@graphql-tools/code-file-loader';
import { runTests } from '../../../../testing/utils';
import '../../../../testing/to-be-similar-string'
import globby from 'globby';
import { readFileSync } from 'fs';
import { removeLoc } from '@graphql-tools/optimize';

describe('documentsFromGlob', () => {
runTests({
Expand All @@ -15,21 +19,38 @@ describe('documentsFromGlob', () => {
const result = await load(glob, {
loaders: [new GraphQLFileLoader()]
});
expect(result.length).toBe(1);
expect(result[0].document).toBeDefined();
expect(result).toHaveLength(1);
const expectedFiles = globby.sync(glob);
for (const expectedFileName of expectedFiles) {
const fileNameResult = result?.find(({ location }) => location === expectedFileName);
if (fileNameResult) {
const fileContent = readFileSync(expectedFileName, 'utf-8');
const expectedDocument: any = parse(fileContent);
expect(removeLoc(fileNameResult!.document!)).toStrictEqual(removeLoc(expectedDocument));
expect(fileNameResult!.rawSDL!).toBeSimilarString(fileContent);
}
}
});

test(`Should load multiple GraphQL document from glob expression`, async () => {
const glob = join(__dirname, './test-files/', '*.graphql');
const result = await load(glob, {
loaders: [new GraphQLFileLoader()]
});
expect(result.length).toBe(2);
expect(result[0].document).toBeDefined();
expect(result[1].document).toBeDefined();
expect(result).toHaveLength(2);
const expectedFiles = globby.sync(glob);
for (const expectedFileName of expectedFiles) {
const fileNameResult = result?.find(({ location }) => location === expectedFileName);
if (fileNameResult) {
const fileContent = readFileSync(expectedFileName, 'utf-8');
const expectedDocument: any = parse(fileContent);
expect(removeLoc(fileNameResult!.document!)).toStrictEqual(removeLoc(expectedDocument));
expect(fileNameResult!.rawSDL!).toBeSimilarString(fileContent);
}
}
});

test(`Should load two GraphQL documents both for gatsby and graphql-tag by default`, async () => {
test(`Should load two GraphQL operations both for gatsby and graphql-tag by default`, async () => {
const glob = join(__dirname, './test-files/', 'tags.js');
const result = await load(glob, {
loaders: [new CodeFileLoader()]
Expand All @@ -38,7 +59,7 @@ describe('documentsFromGlob', () => {
expect(result).toHaveLength(2);
});

test(`Should load GraphQL documents that match custom settings`, async () => {
test(`Should load GraphQL operations that match custom settings`, async () => {
const glob = join(__dirname, './test-files/', 'tags.js');

const result = await load(glob, {
Expand All @@ -56,10 +77,8 @@ describe('documentsFromGlob', () => {
]
});

const { document } = result[0];
const operations = document && separateOperations(document);

expect(operations && Object.keys(operations)).toHaveLength(1);
expect(result[0]?.document).toBeDefined();
expect(Object.keys(separateOperations(result[0].document!))).toHaveLength(1);
});

test(`Should throw on syntax errors`, async () => {
Expand Down
Loading

0 comments on commit 650c4a1

Please sign in to comment.