Skip to content

Commit 21d4af1

Browse files
committed
tests: update 'getIntrospectionQuery' tests to use custom matchers
1 parent 139d0f6 commit 21d4af1

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

src/utilities/__tests__/getIntrospectionQuery-test.js

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,77 @@
11
import { expect } from 'chai';
22
import { describe, it } from 'mocha';
33

4+
import type { IntrospectionOptions } from '../getIntrospectionQuery';
45
import { getIntrospectionQuery } from '../getIntrospectionQuery';
56

7+
function expectIntrospectionQuery(options?: IntrospectionOptions) {
8+
const query = getIntrospectionQuery(options);
9+
10+
return {
11+
toMatch(pattern: RegExp, times: number = 1): void {
12+
expect(query).to.match(pattern);
13+
14+
const gPattern = new RegExp(pattern, 'g');
15+
expect(query.match(gPattern)).to.have.lengthOf(times);
16+
},
17+
toNotMatch(pattern: RegExp): void {
18+
expect(query).to.not.match(pattern);
19+
},
20+
};
21+
}
22+
623
describe('getIntrospectionQuery', () => {
724
it('skip all "description" fields', () => {
8-
expect(getIntrospectionQuery()).to.match(/\bdescription\b/);
25+
expectIntrospectionQuery().toMatch(/\bdescription\b/, 5);
926

10-
expect(getIntrospectionQuery({ descriptions: true })).to.match(
27+
expectIntrospectionQuery({ descriptions: true }).toMatch(
1128
/\bdescription\b/,
29+
5,
1230
);
1331

14-
expect(getIntrospectionQuery({ descriptions: false })).to.not.match(
32+
expectIntrospectionQuery({ descriptions: false }).toNotMatch(
1533
/\bdescription\b/,
1634
);
1735
});
1836

1937
it('include "isRepeatable" field on directives', () => {
20-
expect(getIntrospectionQuery()).to.not.match(/\bisRepeatable\b/);
38+
expectIntrospectionQuery().toNotMatch(/\bisRepeatable\b/);
2139

22-
expect(getIntrospectionQuery({ directiveIsRepeatable: true })).to.match(
40+
expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch(
2341
/\bisRepeatable\b/,
2442
);
2543

26-
expect(
27-
getIntrospectionQuery({ directiveIsRepeatable: false }),
28-
).to.not.match(/\bisRepeatable\b/);
44+
expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch(
45+
/\bisRepeatable\b/,
46+
);
2947
});
3048

3149
it('include "description" field on schema', () => {
32-
expect(getIntrospectionQuery().match(/\bdescription\b/g)).to.have.lengthOf(
50+
expectIntrospectionQuery().toMatch(/\bdescription\b/, 5);
51+
52+
expectIntrospectionQuery({ schemaDescription: false }).toMatch(
53+
/\bdescription\b/,
3354
5,
3455
);
56+
expectIntrospectionQuery({ schemaDescription: true }).toMatch(
57+
/\bdescription\b/,
58+
6,
59+
);
3560

36-
expect(
37-
getIntrospectionQuery({ schemaDescription: false }).match(
38-
/\bdescription\b/g,
39-
),
40-
).to.have.lengthOf(5);
41-
42-
expect(
43-
getIntrospectionQuery({ schemaDescription: true }).match(
44-
/\bdescription\b/g,
45-
),
46-
).to.have.lengthOf(6);
47-
48-
expect(
49-
getIntrospectionQuery({ descriptions: false, schemaDescription: true }),
50-
).to.not.match(/\bdescription\b/);
61+
expectIntrospectionQuery({
62+
descriptions: false,
63+
schemaDescription: true,
64+
}).toNotMatch(/\bdescription\b/);
5165
});
5266

5367
it('include "specifiedByUrl" field', () => {
54-
expect(getIntrospectionQuery()).to.not.match(/\bspecifiedByUrl\b/);
68+
expectIntrospectionQuery().toNotMatch(/\bspecifiedByUrl\b/);
5569

56-
expect(getIntrospectionQuery({ specifiedByUrl: true })).to.match(
70+
expectIntrospectionQuery({ specifiedByUrl: true }).toMatch(
5771
/\bspecifiedByUrl\b/,
5872
);
5973

60-
expect(getIntrospectionQuery({ specifiedByUrl: false })).to.not.match(
74+
expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch(
6175
/\bspecifiedByUrl\b/,
6276
);
6377
});

0 commit comments

Comments
 (0)