Skip to content

Commit ffbbfdc

Browse files
Add missing tests for GraphQLFileLoader, JsonFileLoader and ModuleLoader (#1797)
1 parent b2e5ca8 commit ffbbfdc

File tree

17 files changed

+1331
-14
lines changed

17 files changed

+1331
-14
lines changed

packages/loaders/git/tests/loader.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ describe('GitLoader', () => {
2424
sync: loader.canLoadSync.bind(loader),
2525
})(canLoad => {
2626
it('should return true for a valid pointer', async () => {
27-
expect(canLoad(getPointer('some-file.graphql'))).resolves.toBe(true);
27+
await expect(canLoad(getPointer('some-file.graphql'))).resolves.toBe(true);
2828
});
2929

3030
it('should return false if pointer does not begin with "git:"', async () => {
31-
expect(canLoad(getPointer('some-file.graphql').substring(4))).resolves.toBe(false);
31+
await expect(canLoad(getPointer('some-file.graphql').substring(4))).resolves.toBe(false);
3232
});
3333

3434
it('should return false if pointer is not a string', async () => {
35-
expect(canLoad(42 as any)).resolves.toBe(false);
35+
await expect(canLoad(42 as any)).resolves.toBe(false);
3636
});
3737
});
3838
});

packages/loaders/graphql-file/src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
6363
): Promise<boolean> {
6464
if (isValidPath(pointer)) {
6565
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
66-
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
66+
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || processCwd(), pointer);
6767
return pathExists(normalizedFilePath);
6868
}
6969
}
@@ -74,7 +74,7 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
7474
canLoadSync(pointer: SchemaPointerSingle | DocumentPointerSingle, options: GraphQLFileLoaderOptions): boolean {
7575
if (isValidPath(pointer)) {
7676
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
77-
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
77+
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || processCwd(), pointer);
7878
return pathExistsSync(normalizedFilePath);
7979
}
8080
}
@@ -83,15 +83,14 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
8383
}
8484

8585
async load(pointer: SchemaPointerSingle | DocumentPointerSingle, options: GraphQLFileLoaderOptions): Promise<Source> {
86-
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
86+
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || processCwd(), pointer);
8787
const rawSDL: string = await readFile(normalizedFilePath, { encoding: 'utf8' });
8888

8989
return this.handleFileContent(rawSDL, pointer, options);
9090
}
9191

9292
loadSync(pointer: SchemaPointerSingle | DocumentPointerSingle, options: GraphQLFileLoaderOptions): Source {
93-
const cwd = options.cwd || processCwd();
94-
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(cwd, pointer);
93+
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || processCwd(), pointer);
9594
const rawSDL = readFileSync(normalizedFilePath, { encoding: 'utf8' });
9695
return this.handleFileContent(rawSDL, pointer, options);
9796
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { join } from 'path';
2+
3+
import { Source } from '@graphql-tools/utils';
4+
5+
import { GraphQLFileLoader } from '../src';
6+
import { runTests } from '../../../testing/utils';
7+
8+
describe('GraphQLFileLoader', () => {
9+
const loader = new GraphQLFileLoader();
10+
const getPointer = (fileName: string) => {
11+
return join('packages/loaders/graphql-file/tests/test-files', fileName);
12+
};
13+
14+
describe('loaderId', () => {
15+
it('should return a loader id', () => {
16+
expect(loader.loaderId()).toBeDefined();
17+
});
18+
});
19+
20+
describe('canLoad', () => {
21+
runTests({
22+
async: loader.canLoad.bind(loader),
23+
sync: loader.canLoadSync.bind(loader),
24+
})(canLoad => {
25+
it('should return true for a valid pointer', async () => {
26+
await expect(canLoad(getPointer('type-defs.graphql'), {})).resolves.toBe(true);
27+
});
28+
29+
it('should return true for a valid absolute path', async () => {
30+
await expect(canLoad(join(process.cwd(), getPointer('type-defs.graphql')), {})).resolves.toBe(true);
31+
});
32+
33+
it('should return false if pointer is not a valid path', async () => {
34+
await expect(canLoad(getPointer('!bad-path.graphql'), {})).resolves.toBe(false);
35+
});
36+
37+
it('should return false if pointer does not end with correct file extension', async () => {
38+
await expect(canLoad(getPointer('bad-ext.garphql'), {})).resolves.toBe(false);
39+
});
40+
41+
it('should return false if pointer is for non-existent file', async () => {
42+
await expect(canLoad(getPointer('bad-file.graphql'), {})).resolves.toBe(false);
43+
});
44+
});
45+
});
46+
47+
describe('load', () => {
48+
runTests({
49+
async: loader.load.bind(loader),
50+
sync: loader.loadSync.bind(loader),
51+
})(load => {
52+
it('should load type definitions from a .graphql file', async () => {
53+
const result: Source = await load(getPointer('type-defs.graphql'), {});
54+
expect(result.document).toBeDefined();
55+
});
56+
57+
it('should load file from absolute path', async () => {
58+
const result: Source = await load(join(process.cwd(), getPointer('type-defs.graphql')), {});
59+
expect(result.document).toBeDefined();
60+
});
61+
62+
it('should load file with #import expression', async () => {
63+
const result: Source = await load(getPointer('type-defs-with-import.graphql'), {});
64+
expect(result.document?.definitions.length).toBe(2);
65+
});
66+
});
67+
});
68+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Mutation {
2+
sayGoodbye: String
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#import Mutation from "./import-me.graphql"
2+
3+
type Query {
4+
hello: String
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
type Query {
2+
hello: String
3+
}

packages/loaders/json-file/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export class JsonFileLoader implements DocumentLoader {
7575
const jsonContent: string = await readFile(normalizedFilePath, { encoding: 'utf8' });
7676
return parseGraphQLJSON(pointer, jsonContent, options);
7777
} catch (e) {
78-
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || e}`);
78+
throw new Error(`Unable to read JSON file: ${normalizedFilePath}: ${e.message || /* istanbul ignore next */ e}`);
7979
}
8080
}
8181

@@ -86,7 +86,7 @@ export class JsonFileLoader implements DocumentLoader {
8686
const jsonContent = readFileSync(normalizedFilepath, 'utf8');
8787
return parseGraphQLJSON(pointer, jsonContent, options);
8888
} catch (e) {
89-
throw new Error(`Unable to read JSON file: ${normalizedFilepath}: ${e.message || e}`);
89+
throw new Error(`Unable to read JSON file: ${normalizedFilepath}: ${e.message || /* istanbul ignore next */ e}`);
9090
}
9191
}
9292
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import { join } from 'path';
2+
3+
import { Source } from '@graphql-tools/utils';
4+
5+
import { JsonFileLoader } from '../src';
6+
import { runTests } from '../../../testing/utils';
7+
8+
describe('JsonFileLoader', () => {
9+
const loader = new JsonFileLoader();
10+
const getPointer = (fileName: string) => {
11+
return join('packages/loaders/json-file/tests/test-files', fileName);
12+
};
13+
14+
describe('loaderId', () => {
15+
it('should return a loader id', () => {
16+
expect(loader.loaderId()).toBeDefined();
17+
});
18+
});
19+
20+
describe('canLoad', () => {
21+
runTests({
22+
async: loader.canLoad.bind(loader),
23+
sync: loader.canLoadSync.bind(loader),
24+
})(canLoad => {
25+
it('should return true for a valid pointer', async () => {
26+
await expect(canLoad(getPointer('introspection.json'), {})).resolves.toBe(true);
27+
});
28+
29+
it('should return true for a valid absolute path', async () => {
30+
await expect(canLoad(join(process.cwd(), getPointer('introspection.json')), {})).resolves.toBe(true);
31+
});
32+
33+
it('should return false if pointer is not a valid path', async () => {
34+
await expect(canLoad(getPointer('!bad-path.json'), {})).resolves.toBe(false);
35+
});
36+
37+
it('should return false if pointer does not end with correct file extension', async () => {
38+
await expect(canLoad(getPointer('bad-ext.json5'), {})).resolves.toBe(false);
39+
});
40+
41+
it('should return false if pointer is for non-existent file', async () => {
42+
await expect(canLoad(getPointer('bad-file.json'), {})).resolves.toBe(false);
43+
});
44+
});
45+
});
46+
47+
describe('load', () => {
48+
runTests({
49+
async: loader.load.bind(loader),
50+
sync: loader.loadSync.bind(loader),
51+
})(load => {
52+
it('should load introspection data from a .json file', async () => {
53+
const result: Source = await load(getPointer('introspection.json'), {});
54+
expect(result.schema).toBeDefined();
55+
});
56+
57+
it('should load type definitions from a .json file', async () => {
58+
const result: Source = await load(getPointer('type-defs.json'), {});
59+
expect(result.document).toBeDefined();
60+
});
61+
62+
it('should load file from absolute path', async () => {
63+
const result: Source = await load(join(process.cwd(), getPointer('type-defs.json')), {});
64+
expect(result.document).toBeDefined();
65+
});
66+
67+
it('should throw when the file content is malformed', async () => {
68+
await expect(load(getPointer('malformed.json'), {})).rejects.toThrowError('Unable to read JSON file');
69+
});
70+
});
71+
});
72+
});

0 commit comments

Comments
 (0)