|
| 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