Skip to content

Commit ce03dab

Browse files
tests: update 'getIntrospectionQuery' tests to use custom matchers (#2851)
1 parent 139d0f6 commit ce03dab

File tree

1 file changed

+47
-34
lines changed

1 file changed

+47
-34
lines changed
Lines changed: 47 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +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(name: string, times: number = 1): void {
12+
const pattern = toRegExp(name);
13+
14+
expect(query).to.match(pattern);
15+
expect(query.match(pattern)).to.have.lengthOf(times);
16+
},
17+
toNotMatch(name: string): void {
18+
expect(query).to.not.match(toRegExp(name));
19+
},
20+
};
21+
22+
function toRegExp(name: string): RegExp {
23+
return new RegExp('\\b' + name + '\\b', 'g');
24+
}
25+
}
26+
627
describe('getIntrospectionQuery', () => {
728
it('skip all "description" fields', () => {
8-
expect(getIntrospectionQuery()).to.match(/\bdescription\b/);
29+
expectIntrospectionQuery().toMatch('description', 5);
930

10-
expect(getIntrospectionQuery({ descriptions: true })).to.match(
11-
/\bdescription\b/,
12-
);
31+
expectIntrospectionQuery({ descriptions: true }).toMatch('description', 5);
1332

14-
expect(getIntrospectionQuery({ descriptions: false })).to.not.match(
15-
/\bdescription\b/,
16-
);
33+
expectIntrospectionQuery({ descriptions: false }).toNotMatch('description');
1734
});
1835

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

22-
expect(getIntrospectionQuery({ directiveIsRepeatable: true })).to.match(
23-
/\bisRepeatable\b/,
39+
expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch(
40+
'isRepeatable',
2441
);
2542

26-
expect(
27-
getIntrospectionQuery({ directiveIsRepeatable: false }),
28-
).to.not.match(/\bisRepeatable\b/);
43+
expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch(
44+
'isRepeatable',
45+
);
2946
});
3047

3148
it('include "description" field on schema', () => {
32-
expect(getIntrospectionQuery().match(/\bdescription\b/g)).to.have.lengthOf(
49+
expectIntrospectionQuery().toMatch('description', 5);
50+
51+
expectIntrospectionQuery({ schemaDescription: false }).toMatch(
52+
'description',
3353
5,
3454
);
55+
expectIntrospectionQuery({ schemaDescription: true }).toMatch(
56+
'description',
57+
6,
58+
);
3559

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/);
60+
expectIntrospectionQuery({
61+
descriptions: false,
62+
schemaDescription: true,
63+
}).toNotMatch('description');
5164
});
5265

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

56-
expect(getIntrospectionQuery({ specifiedByUrl: true })).to.match(
57-
/\bspecifiedByUrl\b/,
69+
expectIntrospectionQuery({ specifiedByUrl: true }).toMatch(
70+
'specifiedByUrl',
5871
);
5972

60-
expect(getIntrospectionQuery({ specifiedByUrl: false })).to.not.match(
61-
/\bspecifiedByUrl\b/,
73+
expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch(
74+
'specifiedByUrl',
6275
);
6376
});
6477
});

0 commit comments

Comments
 (0)