|
1 | 1 | import { expect } from 'chai';
|
2 | 2 | import { describe, it } from 'mocha';
|
3 | 3 |
|
| 4 | +import type { IntrospectionOptions } from '../getIntrospectionQuery'; |
4 | 5 | import { getIntrospectionQuery } from '../getIntrospectionQuery';
|
5 | 6 |
|
| 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 | + |
6 | 27 | describe('getIntrospectionQuery', () => {
|
7 | 28 | it('skip all "description" fields', () => {
|
8 |
| - expect(getIntrospectionQuery()).to.match(/\bdescription\b/); |
| 29 | + expectIntrospectionQuery().toMatch('description', 5); |
9 | 30 |
|
10 |
| - expect(getIntrospectionQuery({ descriptions: true })).to.match( |
11 |
| - /\bdescription\b/, |
12 |
| - ); |
| 31 | + expectIntrospectionQuery({ descriptions: true }).toMatch('description', 5); |
13 | 32 |
|
14 |
| - expect(getIntrospectionQuery({ descriptions: false })).to.not.match( |
15 |
| - /\bdescription\b/, |
16 |
| - ); |
| 33 | + expectIntrospectionQuery({ descriptions: false }).toNotMatch('description'); |
17 | 34 | });
|
18 | 35 |
|
19 | 36 | it('include "isRepeatable" field on directives', () => {
|
20 |
| - expect(getIntrospectionQuery()).to.not.match(/\bisRepeatable\b/); |
| 37 | + expectIntrospectionQuery().toNotMatch('isRepeatable'); |
21 | 38 |
|
22 |
| - expect(getIntrospectionQuery({ directiveIsRepeatable: true })).to.match( |
23 |
| - /\bisRepeatable\b/, |
| 39 | + expectIntrospectionQuery({ directiveIsRepeatable: true }).toMatch( |
| 40 | + 'isRepeatable', |
24 | 41 | );
|
25 | 42 |
|
26 |
| - expect( |
27 |
| - getIntrospectionQuery({ directiveIsRepeatable: false }), |
28 |
| - ).to.not.match(/\bisRepeatable\b/); |
| 43 | + expectIntrospectionQuery({ directiveIsRepeatable: false }).toNotMatch( |
| 44 | + 'isRepeatable', |
| 45 | + ); |
29 | 46 | });
|
30 | 47 |
|
31 | 48 | 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', |
33 | 53 | 5,
|
34 | 54 | );
|
| 55 | + expectIntrospectionQuery({ schemaDescription: true }).toMatch( |
| 56 | + 'description', |
| 57 | + 6, |
| 58 | + ); |
35 | 59 |
|
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'); |
51 | 64 | });
|
52 | 65 |
|
53 | 66 | it('include "specifiedByUrl" field', () => {
|
54 |
| - expect(getIntrospectionQuery()).to.not.match(/\bspecifiedByUrl\b/); |
| 67 | + expectIntrospectionQuery().toNotMatch('specifiedByUrl'); |
55 | 68 |
|
56 |
| - expect(getIntrospectionQuery({ specifiedByUrl: true })).to.match( |
57 |
| - /\bspecifiedByUrl\b/, |
| 69 | + expectIntrospectionQuery({ specifiedByUrl: true }).toMatch( |
| 70 | + 'specifiedByUrl', |
58 | 71 | );
|
59 | 72 |
|
60 |
| - expect(getIntrospectionQuery({ specifiedByUrl: false })).to.not.match( |
61 |
| - /\bspecifiedByUrl\b/, |
| 73 | + expectIntrospectionQuery({ specifiedByUrl: false }).toNotMatch( |
| 74 | + 'specifiedByUrl', |
62 | 75 | );
|
63 | 76 | });
|
64 | 77 | });
|
0 commit comments