Skip to content

Commit f89eb27

Browse files
committed
Add createTestDocument to support testing docs
1 parent 104e6c2 commit f89eb27

File tree

10 files changed

+313
-213
lines changed

10 files changed

+313
-213
lines changed

.changeset/hot-terms-bet.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-codegen/testing': minor
3+
---
4+
5+
Add createTestingDocument to use in operation tests

packages/graphql-codegen-cli/tests/codegen.spec.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,7 +1297,7 @@ describe('Codegen Executor', () => {
12971297
describe('Document Transform', () => {
12981298
it('Should transform documents', async () => {
12991299
const transform: Types.DocumentTransformFunction = ({ documents }) => {
1300-
const newDocuments = [
1300+
const newDocuments: Types.DocumentFile[] = [
13011301
{
13021302
document: {
13031303
...documents[0].document,
@@ -1308,6 +1308,8 @@ describe('Codegen Executor', () => {
13081308
} as OperationDefinitionNode,
13091309
],
13101310
},
1311+
hash: 'mocked-abc',
1312+
type: 'standard',
13111313
},
13121314
];
13131315
return newDocuments;
@@ -1334,7 +1336,7 @@ describe('Codegen Executor', () => {
13341336
}) => Types.DocumentTransformObject = ({ queryName }) => {
13351337
return {
13361338
transform: ({ documents }) => {
1337-
const newDocuments = [
1339+
const newDocuments: Types.DocumentFile[] = [
13381340
{
13391341
document: {
13401342
...documents[0].document,
@@ -1345,6 +1347,8 @@ describe('Codegen Executor', () => {
13451347
} as OperationDefinitionNode,
13461348
],
13471349
},
1350+
hash: 'mocked-abc',
1351+
type: 'standard',
13481352
},
13491353
];
13501354
return newDocuments;
@@ -1454,7 +1458,7 @@ describe('Codegen Executor', () => {
14541458

14551459
it('Should transform documents with client-preset', async () => {
14561460
const transform: Types.DocumentTransformFunction = ({ documents }) => {
1457-
const newDocuments = [
1461+
const newDocuments: Types.DocumentFile[] = [
14581462
{
14591463
document: {
14601464
...documents[0].document,
@@ -1465,6 +1469,8 @@ describe('Codegen Executor', () => {
14651469
} as OperationDefinitionNode,
14661470
],
14671471
},
1472+
hash: 'mocked-abc',
1473+
type: 'standard',
14681474
},
14691475
];
14701476
return newDocuments;
@@ -1492,7 +1498,7 @@ describe('Codegen Executor', () => {
14921498

14931499
const capturePreset: Types.OutputPreset = {
14941500
buildGeneratesSection: options => {
1495-
capturedDocumentsReadOnly = options.documentsReadOnly;
1501+
capturedDocumentsReadOnly = options.documents.filter(d => d.type === 'read-only');
14961502
return [
14971503
{
14981504
filename: 'out1/result.ts',
@@ -1525,8 +1531,8 @@ describe('Codegen Executor', () => {
15251531

15261532
const capturePreset: Types.OutputPreset = {
15271533
buildGeneratesSection: options => {
1528-
capturedDocuments = options.documents;
1529-
capturedDocumentsReadOnly = options.documentsReadOnly;
1534+
capturedDocuments = options.documents.filter(d => d.type === 'standard');
1535+
capturedDocumentsReadOnly = options.documents.filter(d => d.type === 'read-only');
15301536
return [
15311537
{
15321538
filename: 'out1/result.ts',
@@ -1586,7 +1592,7 @@ describe('Codegen Executor', () => {
15861592

15871593
const capturePreset: Types.OutputPreset = {
15881594
buildGeneratesSection: options => {
1589-
capturedDocumentsReadOnly = options.documentsReadOnly;
1595+
capturedDocumentsReadOnly = options.documents.filter(d => d.type === 'read-only');
15901596
return [
15911597
{
15921598
filename: 'out1/result.ts',
@@ -1618,7 +1624,7 @@ describe('Codegen Executor', () => {
16181624

16191625
const capturePreset: Types.OutputPreset = {
16201626
buildGeneratesSection: options => {
1621-
capturedDocumentsReadOnly = options.documentsReadOnly;
1627+
capturedDocumentsReadOnly = options.documents.filter(d => d.type === 'read-only');
16221628
return [
16231629
{
16241630
filename: 'out1/result.ts',

packages/graphql-codegen-cli/tests/custom-document-transforms/document-transform.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = {
1111
},
1212
],
1313
},
14+
hash: 'mocked-abc',
15+
type: 'standard',
1416
},
1517
];
1618
return newDocuments;

packages/graphql-codegen-cli/tests/custom-document-transforms/test-config.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ module.exports = {
1111
},
1212
],
1313
},
14+
hash: 'mocked-abc',
15+
type: 'standard',
1416
},
1517
];
1618
return newDocuments;

packages/plugins/typescript/operations/tests/extract-all-types.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { buildSchema, parse } from 'graphql';
22
import { mergeOutputs, Types } from '@graphql-codegen/plugin-helpers';
3-
import { validateTs } from '@graphql-codegen/testing';
3+
import { createTestDocument, validateTs } from '@graphql-codegen/testing';
44
import { plugin as tsPlugin } from '../../typescript/src/index.js';
55
import { plugin, TypeScriptDocumentsPluginConfig } from '../src/index.js';
66

@@ -78,7 +78,7 @@ describe('extractAllFieldsToTypes: true', () => {
7878
};
7979
const { content } = await plugin(
8080
dummyUserTestSchema,
81-
[{ location: 'test-file.ts', document: dummyUserDoc }],
81+
[createTestDocument({ location: 'test-file.ts', document: dummyUserDoc })],
8282
config,
8383
{ outputFile: '' },
8484
);
@@ -396,7 +396,7 @@ describe('extractAllFieldsToTypes: true', () => {
396396
};
397397
const { content } = await plugin(
398398
complexTestSchemaWithUnionsAndInterfaces,
399-
[{ location: 'test-file.ts', document: fragmentsOnComplexSchema }],
399+
[createTestDocument({ location: 'test-file.ts', document: fragmentsOnComplexSchema })],
400400
config,
401401
{ outputFile: '' },
402402
);
@@ -560,7 +560,7 @@ describe('extractAllFieldsToTypes: true', () => {
560560
};
561561
const { content } = await plugin(
562562
complexTestSchemaWithUnionsAndInterfaces,
563-
[{ location: 'test-file.ts', document: fragmentsOnComplexSchema }],
563+
[createTestDocument({ location: 'test-file.ts', document: fragmentsOnComplexSchema })],
564564
config,
565565
{ outputFile: '' },
566566
);
@@ -729,7 +729,7 @@ describe('extractAllFieldsToTypes: true', () => {
729729
};
730730
const { content } = await plugin(
731731
complexTestSchemaWithUnionsAndInterfaces,
732-
[{ location: 'test-file.ts', document: fragmentsOnComplexSchema }],
732+
[createTestDocument({ location: 'test-file.ts', document: fragmentsOnComplexSchema })],
733733
config,
734734
{ outputFile: '' },
735735
);
@@ -967,7 +967,7 @@ describe('extractAllFieldsToTypes: true', () => {
967967
};
968968
const { content } = await plugin(
969969
complexTestSchemaWithUnionsAndInterfaces,
970-
[{ location: 'test-file.ts', document: fragmentsOnComplexSchema }],
970+
[createTestDocument({ location: 'test-file.ts', document: fragmentsOnComplexSchema })],
971971
config,
972972
{ outputFile: '' },
973973
);
@@ -1202,7 +1202,7 @@ describe('extractAllFieldsToTypes: true', () => {
12021202
};
12031203
const { content } = await plugin(
12041204
complexTestSchemaWithUnionsAndInterfaces,
1205-
[{ location: 'test-file.ts', document: fragmentsOnComplexSchema }],
1205+
[createTestDocument({ location: 'test-file.ts', document: fragmentsOnComplexSchema })],
12061206
config,
12071207
{ outputFile: '' },
12081208
);
@@ -1516,7 +1516,7 @@ describe('extractAllFieldsToTypes: true', () => {
15161516

15171517
const { content } = await plugin(
15181518
nestedInterfacesSchema,
1519-
[{ location: 'test-file.ts', document: nestedInterfacesQuery }],
1519+
[createTestDocument({ location: 'test-file.ts', document: nestedInterfacesQuery })],
15201520
config,
15211521
{ outputFile: '' },
15221522
);
@@ -1618,7 +1618,7 @@ describe('extractAllFieldsToTypes: true', () => {
16181618

16191619
const { content } = await plugin(
16201620
interfaceFragmentSchema,
1621-
[{ location: 'test-file.ts', document: interfaceFragmentDoc }],
1621+
[createTestDocument({ location: 'test-file.ts', document: interfaceFragmentDoc })],
16221622
config,
16231623
{ outputFile: '' },
16241624
);
@@ -1728,7 +1728,7 @@ describe('extractAllFieldsToTypes: true', () => {
17281728

17291729
const { content } = await plugin(
17301730
notificationSchema,
1731-
[{ location: 'test-file.ts', document: notificationDoc }],
1731+
[createTestDocument({ location: 'test-file.ts', document: notificationDoc })],
17321732
config,
17331733
{ outputFile: '' },
17341734
);
@@ -1806,7 +1806,7 @@ describe('extractAllFieldsToTypes: true', () => {
18061806

18071807
const { content } = await plugin(
18081808
notificationSchema,
1809-
[{ location: 'test-file.ts', document: notificationDoc }],
1809+
[createTestDocument({ location: 'test-file.ts', document: notificationDoc })],
18101810
config,
18111811
{ outputFile: '' },
18121812
);

packages/plugins/typescript/operations/tests/ts-documents.apolloUnmask.spec.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parse } from 'graphql';
2+
import { createTestDocument } from '@graphql-codegen/testing';
23
import { plugin } from '../src/index.js';
34
import { schema } from './shared/schema.js';
45

@@ -16,7 +17,7 @@ describe('TypeScript Operations Plugin - apolloUnmask', () => {
1617
`);
1718
const result = await plugin(
1819
schema,
19-
[{ location: 'test-file.ts', document: ast }],
20+
[createTestDocument({ location: 'test-file.ts', document: ast })],
2021
{ inlineFragmentTypes: 'mask', customDirectives: { apolloUnmask: true } },
2122
{ outputFile: '' },
2223
);
@@ -45,7 +46,7 @@ describe('TypeScript Operations Plugin - apolloUnmask', () => {
4546
`);
4647
const result = await plugin(
4748
schema,
48-
[{ location: 'test-file.ts', document: ast }],
49+
[createTestDocument({ location: 'test-file.ts', document: ast })],
4950
{ inlineFragmentTypes: 'mask' },
5051
{ outputFile: '' },
5152
);
@@ -76,7 +77,7 @@ describe('TypeScript Operations Plugin - apolloUnmask', () => {
7677
`);
7778
const result = await plugin(
7879
schema,
79-
[{ location: 'test-file.ts', document: ast }],
80+
[createTestDocument({ location: 'test-file.ts', document: ast })],
8081
{
8182
inlineFragmentTypes: 'mask',
8283
customDirectives: { apolloUnmask: false },
@@ -115,7 +116,7 @@ describe('TypeScript Operations Plugin - apolloUnmask', () => {
115116
`);
116117
const result = await plugin(
117118
schema,
118-
[{ location: 'test-file.ts', document: ast }],
119+
[createTestDocument({ location: 'test-file.ts', document: ast })],
119120
{ inlineFragmentTypes: 'mask', customDirectives: { apolloUnmask: true } },
120121
{ outputFile: '' },
121122
);
@@ -154,7 +155,7 @@ describe('TypeScript Operations Plugin - apolloUnmask', () => {
154155
`);
155156
const result = await plugin(
156157
schema,
157-
[{ location: 'test-file.ts', document: ast }],
158+
[createTestDocument({ location: 'test-file.ts', document: ast })],
158159
{ inlineFragmentTypes: 'mask', customDirectives: { apolloUnmask: true } },
159160
{ outputFile: '' },
160161
);

packages/plugins/typescript/operations/tests/ts-documents.nullability.spec.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { buildSchema, parse } from 'graphql';
22
import * as prettier from 'prettier';
3+
import { createTestDocument } from '@graphql-codegen/testing';
34
import { plugin } from '../src/index.js';
45

56
const schema = buildSchema(/* GraphQL */ `
@@ -62,7 +63,7 @@ const document = parse(/* GraphQL */ `
6263

6364
describe('TypeScript Operations Plugin - nullability', () => {
6465
it('converts semanticNonNull to nonNull when nullability.errorHandlingClient=true', async () => {
65-
const result = await plugin(schema, [{ document }], {
66+
const result = await plugin(schema, [createTestDocument({ location: '', document })], {
6667
nullability: {
6768
errorHandlingClient: true,
6869
},
@@ -105,7 +106,7 @@ describe('TypeScript Operations Plugin - nullability', () => {
105106
});
106107

107108
it('does not convert nullability to nonNull when nullability.errorHandlingClient=false', async () => {
108-
const result = await plugin(schema, [{ document }], {
109+
const result = await plugin(schema, [createTestDocument({ location: '', document })], {
109110
nullability: {
110111
errorHandlingClient: false,
111112
},

0 commit comments

Comments
 (0)