Skip to content

Commit

Permalink
start adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yaacovCR committed Nov 25, 2024
1 parent f6325d3 commit e495a3f
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/utilities/__tests__/mapSchemaConfig-test.ts
Original file line number Diff line number Diff line change
@@ -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<GraphQLNamedType>) {
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,
}));
});
});

0 comments on commit e495a3f

Please sign in to comment.