diff --git a/src/utilities/__tests__/getIntrospectionQuery-test.js b/src/utilities/__tests__/getIntrospectionQuery-test.js index 3a6fad2c4b6..3628ffb057d 100644 --- a/src/utilities/__tests__/getIntrospectionQuery-test.js +++ b/src/utilities/__tests__/getIntrospectionQuery-test.js @@ -1,63 +1,77 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; +import type { IntrospectionOptions } from '../getIntrospectionQuery'; import { getIntrospectionQuery } from '../getIntrospectionQuery'; +function expectIntrospectionQuery(options?: IntrospectionOptions) { + const query = getIntrospectionQuery(options); + + return { + toMatch(pattern: RegExp, times: number = 1): void { + expect(query).to.match(pattern); + + const gPattern = new RegExp(pattern, 'g'); + expect(query.match(gPattern)).to.have.lengthOf(times); + }, + toNotMatch(pattern: RegExp): void { + expect(query).to.not.match(pattern); + }, + }; +} + describe('getIntrospectionQuery', () => { it('skip all "description" fields', () => { - expect(getIntrospectionQuery()).to.match(/\bdescription\b/); + expectIntrospectionQuery().toMatch(/\bdescription\b/, 5); - expect(getIntrospectionQuery({ descriptions: true })).to.match( + expectIntrospectionQuery({ descriptions: true }).toMatch( /\bdescription\b/, + 5, ); - expect(getIntrospectionQuery({ descriptions: false })).to.not.match( + expectIntrospectionQuery({ descriptions: false }).toNotMatch( /\bdescription\b/, ); }); it('include "isRepeatable" field on directives', () => { - expect(getIntrospectionQuery()).to.not.match(/\bisRepeatable\b/); + expectIntrospectionQuery().toNotMatch(/\bisRepeatable\b/); - expect(getIntrospectionQuery({ directiveIsRepeatable: true })).to.match( + expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch( /\bisRepeatable\b/, ); - expect( - getIntrospectionQuery({ directiveIsRepeatable: false }), - ).to.not.match(/\bisRepeatable\b/); + expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch( + /\bisRepeatable\b/, + ); }); it('include "description" field on schema', () => { - expect(getIntrospectionQuery().match(/\bdescription\b/g)).to.have.lengthOf( + expectIntrospectionQuery().toMatch(/\bdescription\b/, 5); + + expectIntrospectionQuery({ schemaDescription: false }).toMatch( + /\bdescription\b/, 5, ); + expectIntrospectionQuery({ schemaDescription: true }).toMatch( + /\bdescription\b/, + 6, + ); - expect( - getIntrospectionQuery({ schemaDescription: false }).match( - /\bdescription\b/g, - ), - ).to.have.lengthOf(5); - - expect( - getIntrospectionQuery({ schemaDescription: true }).match( - /\bdescription\b/g, - ), - ).to.have.lengthOf(6); - - expect( - getIntrospectionQuery({ descriptions: false, schemaDescription: true }), - ).to.not.match(/\bdescription\b/); + expectIntrospectionQuery({ + descriptions: false, + schemaDescription: true, + }).toNotMatch(/\bdescription\b/); }); it('include "specifiedByUrl" field', () => { - expect(getIntrospectionQuery()).to.not.match(/\bspecifiedByUrl\b/); + expectIntrospectionQuery().toNotMatch(/\bspecifiedByUrl\b/); - expect(getIntrospectionQuery({ specifiedByUrl: true })).to.match( + expectIntrospectionQuery({ specifiedByUrl: true }).toMatch( /\bspecifiedByUrl\b/, ); - expect(getIntrospectionQuery({ specifiedByUrl: false })).to.not.match( + expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch( /\bspecifiedByUrl\b/, ); });