From e495a3f79043509cba33fd4bc1e6b0e6f9e36c91 Mon Sep 17 00:00:00 2001 From: Yaacov Rydzinski Date: Tue, 26 Nov 2024 01:50:38 +0200 Subject: [PATCH] start adding tests --- .../__tests__/mapSchemaConfig-test.ts | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) create mode 100644 src/utilities/__tests__/mapSchemaConfig-test.ts diff --git a/src/utilities/__tests__/mapSchemaConfig-test.ts b/src/utilities/__tests__/mapSchemaConfig-test.ts new file mode 100644 index 0000000000..9bbaf60afa --- /dev/null +++ b/src/utilities/__tests__/mapSchemaConfig-test.ts @@ -0,0 +1,70 @@ +import { expect } from 'chai'; +import { describe, it } from 'mocha'; + +import { expectJSON } from '../../__testUtils__/expectJSON.js'; + +import type { GraphQLNamedType } from '../../type/index.js'; +import type { GraphQLSchemaNormalizedConfig } from '../../type/schema.js'; + +import { buildSchema } from '../buildASTSchema.js'; +import { mapSchemaConfig, SchemaElementKind } from '../mapSchemaConfig.js'; + +function toTypeMap(types: ReadonlyArray) { + const typeMap = Object.create(null); + for (const type of types) { + typeMap[type.name] = type; + } + return typeMap; +} + +describe('mapSchemaConfig', () => { + it('returns the original config when no mappers are included', () => { + const schema = buildSchema('type Query'); + const schemaConfig = schema.toConfig(); + const newSchemaConfig = mapSchemaConfig(schemaConfig, () => ({})); + expectJSON(newSchemaConfig).toDeepEqual(schemaConfig); + }); + + it('returns a new schema config when using a schema mapper', () => { + const schema = buildSchema('type Query'); + const schemaConfig = schema.toConfig(); + const schemaConfigWithDescription = { + ...schemaConfig, + description: 'New description', + }; + + const newSchemaConfig = mapSchemaConfig(schema.toConfig(), () => ({ + [SchemaElementKind.SCHEMA]: () => schemaConfigWithDescription, + })); + + expect(schemaConfigWithDescription).to.equal(newSchemaConfig); + }); + + it('executes schema mappers last such that they receive mapped types', () => { + const schema = buildSchema('type Query'); + const schemaTypeMap = schema.getTypeMap(); + const schemaConfig = schema.toConfig(); + const schemaConfigWithDescription = { + ...schemaConfig, + description: 'New description', + }; + + function schemaMapper(config: GraphQLSchemaNormalizedConfig) { + expect(config).not.to.equal(schemaConfig); + + const mappedSchemaTypeMap = toTypeMap(config.types); + for (const [typeName, type] of Object.entries(schemaTypeMap)) { + if (typeName === 'Query') { + expect(mappedSchemaTypeMap[typeName]).not.to.equal(type); + } else { + expect(mappedSchemaTypeMap[typeName]).to.equal(type); + } + } + return schemaConfigWithDescription; + } + + mapSchemaConfig(schema.toConfig(), () => ({ + [SchemaElementKind.SCHEMA]: schemaMapper, + })); + }); +});