|
| 1 | +import { buildSchema, OperationDefinitionNode, parse } from 'graphql'; |
| 2 | +import { ClientSideBaseVisitor, DocumentMode } from '../src/client-side-base-visitor.js'; |
| 3 | + |
| 4 | +describe('getImports', () => { |
| 5 | + describe('when documentMode "external", importDocumentNodeExternallyFrom is "near-operation-file"', () => { |
| 6 | + const schema = buildSchema(/* GraphQL */ ` |
| 7 | + type Query { |
| 8 | + a: A |
| 9 | + } |
| 10 | +
|
| 11 | + type A { |
| 12 | + foo: String |
| 13 | + bar: String |
| 14 | + } |
| 15 | + `); |
| 16 | + |
| 17 | + describe('when emitLegacyCommonJSImports is true', () => { |
| 18 | + it('does not append `.js` to Operations import path', () => { |
| 19 | + const fileName = 'fooBarQuery'; |
| 20 | + const importPath = `src/queries/${fileName}`; |
| 21 | + |
| 22 | + const document = parse( |
| 23 | + `query fooBarQuery { |
| 24 | + a { |
| 25 | + foo |
| 26 | + bar |
| 27 | + } |
| 28 | + } |
| 29 | + ` |
| 30 | + ); |
| 31 | + |
| 32 | + const visitor = new ClientSideBaseVisitor( |
| 33 | + schema, |
| 34 | + [], |
| 35 | + { |
| 36 | + emitLegacyCommonJSImports: true, |
| 37 | + importDocumentNodeExternallyFrom: 'near-operation-file', |
| 38 | + documentMode: DocumentMode.external, |
| 39 | + }, |
| 40 | + {}, |
| 41 | + [{ document, location: importPath }] |
| 42 | + ); |
| 43 | + |
| 44 | + visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode); |
| 45 | + |
| 46 | + const imports = visitor.getImports(); |
| 47 | + expect(imports[0]).toBe(`import * as Operations from './${fileName}';`); |
| 48 | + }); |
| 49 | + }); |
| 50 | + |
| 51 | + describe('when emitLegacyCommonJSImports is false', () => { |
| 52 | + it('appends `.js` to Operations import path', () => { |
| 53 | + const fileName = 'fooBarQuery'; |
| 54 | + const importPath = `src/queries/${fileName}`; |
| 55 | + |
| 56 | + const document = parse( |
| 57 | + `query fooBarQuery { |
| 58 | + a { |
| 59 | + foo |
| 60 | + bar |
| 61 | + } |
| 62 | + } |
| 63 | + ` |
| 64 | + ); |
| 65 | + |
| 66 | + const visitor = new ClientSideBaseVisitor( |
| 67 | + schema, |
| 68 | + [], |
| 69 | + { |
| 70 | + emitLegacyCommonJSImports: false, |
| 71 | + importDocumentNodeExternallyFrom: 'near-operation-file', |
| 72 | + documentMode: DocumentMode.external, |
| 73 | + }, |
| 74 | + {}, |
| 75 | + [{ document, location: importPath }] |
| 76 | + ); |
| 77 | + |
| 78 | + visitor.OperationDefinition(document.definitions[0] as OperationDefinitionNode); |
| 79 | + |
| 80 | + const imports = visitor.getImports(); |
| 81 | + expect(imports[0]).toBe(`import * as Operations from './${fileName}.js';`); |
| 82 | + }); |
| 83 | + }); |
| 84 | + }); |
| 85 | +}); |
0 commit comments