Skip to content

Commit 2dd50a5

Browse files
committed
Add tests
1 parent 5930f73 commit 2dd50a5

4 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
query User1a {
2+
user1a: user {
3+
id
4+
}
5+
}
6+
7+
query User1b {
8+
user1b: user {
9+
id
10+
name
11+
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
query User2 {
2+
user2: user {
3+
...UserFragment3
4+
}
5+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fragment UserFragment3 on User {
2+
id
3+
}

packages/presets/near-operation-file/tests/near-operation-file.spec.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1413,6 +1413,62 @@ describe('near-operation-file preset', () => {
14131413
"
14141414
`);
14151415
});
1416+
1417+
it('generates a file per operation (instead of file) when using filePerOperation:true', async () => {
1418+
const { result } = await executeCodegen({
1419+
schema: /* GraphQL */ `
1420+
type Query {
1421+
user: User
1422+
}
1423+
1424+
type User {
1425+
id: ID!
1426+
name: String!
1427+
}
1428+
`,
1429+
documents: path.join(__dirname, 'fixtures/file-per-operation.*.graphql'),
1430+
generates: {
1431+
[__dirname]: {
1432+
preset,
1433+
presetConfig: {
1434+
filePerOperation: true,
1435+
},
1436+
plugins: ['typescript-operations'],
1437+
},
1438+
},
1439+
});
1440+
1441+
expect(result.length).toBe(3);
1442+
1443+
const file1 = result.find(file => file.filename.endsWith('User1a.generated.ts'));
1444+
expect(file1.content).toMatchInlineSnapshot(`
1445+
"export type User1aQueryVariables = Exact<{ [key: string]: never; }>;
1446+
1447+
1448+
export type User1aQuery = { __typename?: 'Query', user1a?: { __typename?: 'User', id: string } | null };
1449+
1450+
export type User1bQueryVariables = Exact<{ [key: string]: never; }>;
1451+
1452+
1453+
export type User1bQuery = { __typename?: 'Query', user1b?: { __typename?: 'User', id: string, name: string } | null };
1454+
"
1455+
`);
1456+
1457+
const file2 = result.find(file => file.filename.endsWith('User2.generated.ts'));
1458+
expect(file2.content).toMatchInlineSnapshot(`
1459+
"export type User2QueryVariables = Exact<{ [key: string]: never; }>;
1460+
1461+
1462+
export type User2Query = { __typename?: 'Query', user2?: { __typename?: 'User', id: string } | null };
1463+
"
1464+
`);
1465+
1466+
const file3 = result.find(file => file.filename.endsWith('UserFragment3.generated.ts'));
1467+
expect(file3.content).toMatchInlineSnapshot(`
1468+
"export type UserFragment3Fragment = { __typename?: 'User', id: string };
1469+
"
1470+
`);
1471+
});
14161472
});
14171473

14181474
const getFragmentImportsFromResult = (result: Types.GenerateOptions[], index = 0) =>

0 commit comments

Comments
 (0)