|
| 1 | +import { Test } from '@nestjs/testing'; |
| 2 | + |
| 3 | +import { GraphQLAstExplorer, GraphQLFactory } from '../lib'; |
| 4 | +import { GraphQLSchemaBuilder } from '../lib/graphql-schema.builder'; |
| 5 | +import { ResolversExplorerService } from '../lib/services/resolvers-explorer.service'; |
| 6 | +import { ScalarsExplorerService } from '../lib/services/scalars-explorer.service'; |
| 7 | +import gql from 'graphql-tag'; |
| 8 | +import { GraphQLSchema } from 'graphql'; |
| 9 | + |
| 10 | +describe('GraphQLFactory', () => { |
| 11 | + let graphqlFactory: GraphQLFactory; |
| 12 | + let resolverExplorer: { explore: jest.Mock }; |
| 13 | + let scalarExplorer: { explore: jest.Mock }; |
| 14 | + |
| 15 | + beforeEach(async () => { |
| 16 | + resolverExplorer = { explore: jest.fn() }; |
| 17 | + scalarExplorer = { explore: jest.fn() }; |
| 18 | + const module = await Test.createTestingModule({ |
| 19 | + providers: [ |
| 20 | + { |
| 21 | + provide: ResolversExplorerService, |
| 22 | + useValue: resolverExplorer, |
| 23 | + }, |
| 24 | + { |
| 25 | + provide: ScalarsExplorerService, |
| 26 | + useValue: scalarExplorer, |
| 27 | + }, |
| 28 | + { |
| 29 | + provide: GraphQLAstExplorer, |
| 30 | + useValue: jest.fn(), |
| 31 | + }, |
| 32 | + { |
| 33 | + provide: GraphQLSchemaBuilder, |
| 34 | + useValue: jest.fn(), |
| 35 | + }, |
| 36 | + GraphQLFactory, |
| 37 | + ], |
| 38 | + }).compile(); |
| 39 | + |
| 40 | + graphqlFactory = module.get(GraphQLFactory); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('generateSchema', () => { |
| 44 | + it('should support transforming the resolvers', async () => { |
| 45 | + const resolvers = [{ Query: { echo: jest.fn() } }]; |
| 46 | + |
| 47 | + resolverExplorer.explore.mockReturnValueOnce(resolvers); |
| 48 | + scalarExplorer.explore.mockReturnValueOnce([]); |
| 49 | + |
| 50 | + const transformResolvers = jest.fn((r) => r); |
| 51 | + |
| 52 | + const schema = await graphqlFactory.generateSchema({ |
| 53 | + transformResolvers, |
| 54 | + typeDefs: ` |
| 55 | + type Query { |
| 56 | + echo(text: String!): String |
| 57 | + } |
| 58 | + `, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(transformResolvers).toHaveBeenCalledWith(resolvers); |
| 62 | + expect(resolverExplorer.explore).toHaveBeenCalled(); |
| 63 | + expect(scalarExplorer.explore).toHaveBeenCalled(); |
| 64 | + expect(schema).toBeInstanceOf(GraphQLSchema); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should support undefined transformResolvers option', async () => { |
| 68 | + const resolvers = [{ Query: { echo: jest.fn() } }]; |
| 69 | + |
| 70 | + resolverExplorer.explore.mockReturnValueOnce(resolvers); |
| 71 | + scalarExplorer.explore.mockReturnValueOnce([]); |
| 72 | + |
| 73 | + const schema = await graphqlFactory.generateSchema({ |
| 74 | + typeDefs: ` |
| 75 | + type Query { |
| 76 | + echo(text: String!): String |
| 77 | + } |
| 78 | + `, |
| 79 | + }); |
| 80 | + |
| 81 | + expect(resolverExplorer.explore).toHaveBeenCalled(); |
| 82 | + expect(scalarExplorer.explore).toHaveBeenCalled(); |
| 83 | + expect(schema).toBeInstanceOf(GraphQLSchema); |
| 84 | + }); |
| 85 | + }); |
| 86 | +}); |
0 commit comments