Skip to content

Commit

Permalink
tests: update 'getIntrospectionQuery' tests to use custom matchers
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov committed Nov 22, 2020
1 parent 139d0f6 commit 21d4af1
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions 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/,
);
});
Expand Down

0 comments on commit 21d4af1

Please sign in to comment.