From 2f6a628388f77494d88b416c17758dcb7804a21c Mon Sep 17 00:00:00 2001 From: Ivan Goncharov Date: Sun, 22 Nov 2020 17:16:26 +0200 Subject: [PATCH] tests: update 'getIntrospectionQuery' tests to use custom matchers (#2851) --- .../__tests__/getIntrospectionQuery-test.js | 81 +++++++++++-------- 1 file changed, 47 insertions(+), 34 deletions(-) diff --git a/src/utilities/__tests__/getIntrospectionQuery-test.js b/src/utilities/__tests__/getIntrospectionQuery-test.js index 3a6fad2c4b6..14509ed5638 100644 --- a/src/utilities/__tests__/getIntrospectionQuery-test.js +++ b/src/utilities/__tests__/getIntrospectionQuery-test.js @@ -1,64 +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(name: string, times: number = 1): void { + const pattern = toRegExp(name); + + expect(query).to.match(pattern); + expect(query.match(pattern)).to.have.lengthOf(times); + }, + toNotMatch(name: string): void { + expect(query).to.not.match(toRegExp(name)); + }, + }; + + function toRegExp(name: string): RegExp { + return new RegExp('\\b' + name + '\\b', 'g'); + } +} + describe('getIntrospectionQuery', () => { it('skip all "description" fields', () => { - expect(getIntrospectionQuery()).to.match(/\bdescription\b/); + expectIntrospectionQuery().toMatch('description', 5); - expect(getIntrospectionQuery({ descriptions: true })).to.match( - /\bdescription\b/, - ); + expectIntrospectionQuery({ descriptions: true }).toMatch('description', 5); - expect(getIntrospectionQuery({ descriptions: false })).to.not.match( - /\bdescription\b/, - ); + expectIntrospectionQuery({ descriptions: false }).toNotMatch('description'); }); it('include "isRepeatable" field on directives', () => { - expect(getIntrospectionQuery()).to.not.match(/\bisRepeatable\b/); + expectIntrospectionQuery().toNotMatch('isRepeatable'); - expect(getIntrospectionQuery({ directiveIsRepeatable: true })).to.match( - /\bisRepeatable\b/, + expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch( + 'isRepeatable', ); - expect( - getIntrospectionQuery({ directiveIsRepeatable: false }), - ).to.not.match(/\bisRepeatable\b/); + expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch( + 'isRepeatable', + ); }); it('include "description" field on schema', () => { - expect(getIntrospectionQuery().match(/\bdescription\b/g)).to.have.lengthOf( + expectIntrospectionQuery().toMatch('description', 5); + + expectIntrospectionQuery({ schemaDescription: false }).toMatch( + 'description', 5, ); + expectIntrospectionQuery({ schemaDescription: true }).toMatch( + 'description', + 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('description'); }); it('include "specifiedByUrl" field', () => { - expect(getIntrospectionQuery()).to.not.match(/\bspecifiedByUrl\b/); + expectIntrospectionQuery().toNotMatch('specifiedByUrl'); - expect(getIntrospectionQuery({ specifiedByUrl: true })).to.match( - /\bspecifiedByUrl\b/, + expectIntrospectionQuery({ specifiedByUrl: true }).toMatch( + 'specifiedByUrl', ); - expect(getIntrospectionQuery({ specifiedByUrl: false })).to.not.match( - /\bspecifiedByUrl\b/, + expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch( + 'specifiedByUrl', ); }); });