Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: update 'getIntrospectionQuery' tests to use custom matchers #2851

Merged
merged 1 commit into from Nov 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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',
);
});
});