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 6c4a7af
Showing 1 changed file with 47 additions and 34 deletions.
81 changes: 47 additions & 34 deletions 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',
);
});
});

0 comments on commit 6c4a7af

Please sign in to comment.