Skip to content

Commit 7df6057

Browse files
authoredJul 31, 2024··
chore: remove redundant data-schema unit tests (#13659)
1 parent 6daf0d3 commit 7df6057

File tree

11 files changed

+440
-13377
lines changed

11 files changed

+440
-13377
lines changed
 

‎.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"--inspect-brk",
1616
"${workspaceRoot}/node_modules/.bin/jest",
1717
// Optionally specify a single test file to run/debug:
18-
"generateClientWithAmplifyInstance.test.ts",
18+
"generateClient.test.ts",
1919
"--runInBand",
2020
"--testTimeout",
2121
"600000", // 10 min timeout so jest doesn't error while we're stepping through code

‎packages/api-graphql/__tests__/fixtures/modeled/amplifyconfiguration.ts

+4-1,752
Large diffs are not rendered by default.

‎packages/api-graphql/__tests__/fixtures/modeled/schema.ts

-213
Original file line numberDiff line numberDiff line change
@@ -24,219 +24,6 @@ const schema = a.schema({
2424
data: a.json(),
2525
})
2626
.authorization(allow => [allow.publicApiKey(), allow.owner()]),
27-
ThingWithCustomerOwnerField: a
28-
.model({
29-
id: a.id(),
30-
description: a.string(),
31-
})
32-
.authorization(allow => [allow.ownerDefinedIn('customField', 'userPools')]),
33-
ThingWithOwnerFieldSpecifiedInModel: a
34-
.model({
35-
id: a.id(),
36-
name: a.string(),
37-
owner: a.string(),
38-
})
39-
.authorization(allow => [allow.owner()]),
40-
ThingWithAPIKeyAuth: a
41-
.model({
42-
id: a.id(),
43-
description: a.string(),
44-
})
45-
.authorization(allow => [allow.publicApiKey()]),
46-
ThingWithoutExplicitAuth: a.model({
47-
id: a.id(),
48-
description: a.string(),
49-
}),
50-
ThingWithCustomPk: a
51-
.model({
52-
cpk_cluster_key: a.string().required(),
53-
cpk_sort_key: a.string().required(),
54-
otherField: a.string(),
55-
})
56-
.identifier(['cpk_cluster_key', 'cpk_sort_key']),
57-
58-
CommunityPostMetadata: a.customType({
59-
type: a.string().required(),
60-
deleted: a.boolean(),
61-
}),
62-
63-
CommunityPost: a.model({
64-
id: a.id().required(),
65-
communityPostPollId: a.id(),
66-
poll: a.hasOne('CommunityPoll', 'communityPostPollId'),
67-
metadata: a.ref('CommunityPostMetadata'),
68-
}),
69-
CommunityPoll: a.model({
70-
id: a.id().required(),
71-
question: a.string().required(),
72-
answers: a.hasMany('CommunityPollAnswer', 'communityPollAnswersId').valueRequired()
73-
}),
74-
CommunityPollAnswer: a.model({
75-
id: a.id().required(),
76-
answer: a.string().required(),
77-
communityPollAnswersId: a.id(),
78-
votes: a.hasMany('CommunityPollVote', 'communityPollAnswerVotesId').valueRequired(),
79-
}),
80-
CommunityPollVote: a
81-
.model({
82-
id: a.id().required(),
83-
communityPollAnswerVotesId: a.id()
84-
})
85-
.authorization(allow => [allow.publicApiKey(), allow.owner()]),
86-
SecondaryIndexModel: a
87-
.model({
88-
title: a.string(),
89-
description: a.string(),
90-
viewCount: a.integer(),
91-
status: a.enum(['draft', 'pending', 'published']),
92-
})
93-
.secondaryIndexes(index => [
94-
index('title'),
95-
index('description').sortKeys(['viewCount']),
96-
]),
97-
Product: a
98-
.model({
99-
sku: a.string().required(),
100-
factoryId: a.string().required(),
101-
description: a.string(),
102-
warehouseProductsId: a.id(),
103-
warehouse: a.belongsTo("Warehouse", 'warehouseProductsId'),
104-
trackingMeta: a.customType({
105-
productMeta: a.ref('ProductMeta'),
106-
note: a.string(),
107-
}),
108-
})
109-
.identifier(['sku', 'factoryId'])
110-
.authorization(allow => [allow.owner(), allow.publicApiKey().to(["read"])]),
111-
Warehouse: a.model({
112-
name: a.string().required(),
113-
products: a.hasMany("Product", 'warehouseProductsId'),
114-
}).authorization(allow => [allow.owner(), allow.publicApiKey().to(["read"])]),
115-
ProductMeta: a.customType({
116-
releaseDate: a.date(),
117-
status: a.enum(['in_production', 'discontinued']),
118-
deepMeta: a.customType({
119-
content: a.string(),
120-
}),
121-
}),
122-
123-
// #region Custom queries and mutations
124-
EchoResult: a.customType({
125-
resultContent: a.string().required(),
126-
}),
127-
128-
// custom query returning a non-model type
129-
echo: a
130-
.query()
131-
.arguments({
132-
argumentContent: a.string().required(),
133-
})
134-
.returns(a.ref('EchoResult'))
135-
.handler(a.handler.function('echoFunction'))
136-
.authorization(allow => [allow.publicApiKey()]),
137-
138-
// custom query returning a primitive type
139-
echoString: a
140-
.query()
141-
.arguments({
142-
inputString: a.string().required(),
143-
})
144-
.returns(a.string())
145-
.handler(a.handler.function('echoFunction'))
146-
.authorization(allow => [allow.publicApiKey()]),
147-
echoNestedCustomTypes: a
148-
.query()
149-
.arguments({
150-
input: a.string().required(),
151-
})
152-
.returns(a.ref('ProductTrackingMeta'))
153-
.handler(a.handler.function('echoFunction'))
154-
.authorization(allow => [allow.publicApiKey()]),
155-
echoModelHasNestedTypes: a
156-
.query()
157-
.arguments({
158-
input: a.string().required(),
159-
})
160-
.returns(a.ref('Product'))
161-
.handler(a.handler.function('echoFunction'))
162-
.authorization(allow => [allow.publicApiKey()]),
163-
// custom mutation returning a non-model type
164-
PostLikeResult: a.customType({
165-
likes: a.integer().required(),
166-
}),
167-
likePost: a
168-
.mutation()
169-
.arguments({
170-
postId: a.id().required(),
171-
})
172-
.returns(a.ref('PostLikeResult'))
173-
.handler(a.handler.function('echoFunction'))
174-
.authorization(allow => [allow.guest()]),
175-
176-
// custom mutation returning a model type
177-
Post: a
178-
.model({
179-
id: a.id().required(),
180-
content: a.string(),
181-
comments: a.hasMany('Comment', 'postCommentsId'),
182-
})
183-
.authorization(allow => [allow.publicApiKey(), allow.owner()]),
184-
Comment: a
185-
.model({
186-
id: a.id().required(),
187-
content: a.string().required(),
188-
postCommentsId: a.id().required(),
189-
post: a.belongsTo('Post', 'postCommentsId'),
190-
})
191-
.authorization(allow => [allow.publicApiKey(), allow.owner()]),
192-
likePostReturnPost: a
193-
.mutation()
194-
.arguments({
195-
postId: a.id().required(),
196-
})
197-
.returns(a.ref('Post'))
198-
.handler(a.handler.function('echoFunction'))
199-
.authorization(allow => [allow.guest()]),
200-
201-
onPostLiked: a
202-
.subscription()
203-
.for(a.ref('likePostReturnPost'))
204-
.handler(a.handler.custom({ entry: './jsResolver_base.js' })),
205-
206-
onPostUpdated: a
207-
.subscription()
208-
.for(a.ref('Post').mutations(['update']))
209-
.arguments({ postId: a.string() })
210-
.handler(a.handler.custom({ entry: './jsResolver_base.js' })),
211-
//#endregion
212-
213-
// #region implicit ownership models
214-
ImplicitOwner: a
215-
.model({
216-
description: a.string(),
217-
})
218-
.authorization(allow => [allow.owner()]),
219-
CustomImplicitOwner: a
220-
.model({
221-
description: a.string(),
222-
})
223-
.authorization(allow => [allow.ownerDefinedIn('customOwner')]),
224-
ModelGroupDefinedIn: a
225-
.model({
226-
description: a.string(),
227-
})
228-
.authorization(allow => [allow.groupDefinedIn('groupField')]),
229-
ModelGroupsDefinedIn: a
230-
.model({
231-
description: a.string(),
232-
})
233-
.authorization(allow => [allow.groupsDefinedIn('groupsField')]),
234-
ModelStaticGroup: a
235-
.model({
236-
description: a.string(),
237-
})
238-
.authorization(allow => [allow.group('Admin')]),
239-
// #endregion
24027
});
24128

24229
export type Schema = ClientSchema<typeof schema>;

‎packages/api-graphql/__tests__/fixtures/schema-models/with-custom-primary-key/models.ts

-199
This file was deleted.

‎packages/api-graphql/__tests__/internals/__snapshots__/generateClient.test.ts.snap

+86-5,187
Large diffs are not rendered by default.

‎packages/api-graphql/__tests__/internals/generateClient.test.ts

+333-5,686
Large diffs are not rendered by default.

‎packages/api-graphql/__tests__/internals/implicit-auth-fields.test.ts

-90
This file was deleted.

‎packages/api-graphql/__tests__/internals/server/__snapshots__/generateClientWithAmplifyInstance.test.ts.snap

-51
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,5 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3-
exports[`server generateClient with cookies can custom query 1`] = `
4-
[
5-
[
6-
"AmplifyClassV6",
7-
{
8-
"abortController": AbortController {},
9-
"options": {
10-
"body": {
11-
"query": "query ($argumentContent: String!) {
12-
echo(argumentContent: $argumentContent) {
13-
resultContent
14-
}
15-
}
16-
",
17-
"variables": {
18-
"argumentContent": "echo argumentContent value",
19-
},
20-
},
21-
"headers": {
22-
"X-Api-Key": "FAKE-KEY",
23-
"x-amz-user-agent": "aws-amplify/latest api/latest framework/latest",
24-
},
25-
"signingServiceInfo": undefined,
26-
"withCredentials": undefined,
27-
},
28-
"url": "https://localhost/graphql",
29-
},
30-
],
31-
]
32-
`;
33-
343
exports[`server generateClient with cookies can list 1`] = `
354
[
365
[
@@ -261,23 +230,3 @@ exports[`server generateClient with cookies can list with sort direction (descen
261230
],
262231
]
263232
`;
264-
265-
exports[`server generateClient with cookies with request can custom query 1`] = `
266-
[
267-
[
268-
"AmplifyClassV6",
269-
{
270-
"authMode": undefined,
271-
"authToken": undefined,
272-
"query": "
273-
query($argumentContent: String!) {
274-
echo(argumentContent: $argumentContent) {resultContent}
275-
}
276-
",
277-
"variables": {
278-
"argumentContent": "echo argumentContent value",
279-
},
280-
},
281-
],
282-
]
283-
`;

‎packages/api-graphql/__tests__/internals/server/generateClientWithAmplifyInstance.test.ts

+2-59
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ const config: ResourcesConfig = {
2525
},
2626
};
2727

28+
// sanity check for CRUD model ops using server clients
29+
// exhaustive tests live in https://github.com/aws-amplify/amplify-api-next
2830
describe('server generateClient', () => {
2931
beforeEach(() => {
3032
jest.clearAllMocks();
@@ -266,37 +268,6 @@ describe('server generateClient', () => {
266268
expect(normalizePostGraphqlCalls(spy)).toMatchSnapshot();
267269
});
268270

269-
test('can custom query', async () => {
270-
Amplify.configure(configFixture as any);
271-
const config = Amplify.getConfig();
272-
273-
const spy = mockApiResponse({
274-
data: {
275-
echo: {
276-
resultContent: 'echo result content',
277-
},
278-
},
279-
});
280-
281-
const getAmplify = async (fn: any) => await fn(Amplify);
282-
283-
const client = generateClientWithAmplifyInstance<
284-
Schema,
285-
V6ClientSSRCookies<Schema>
286-
>({
287-
amplify: getAmplify,
288-
config: config,
289-
});
290-
291-
const result = await client.queries.echo({
292-
argumentContent: 'echo argumentContent value',
293-
});
294-
295-
expect(normalizePostGraphqlCalls(spy)).toMatchSnapshot();
296-
expect(result?.data).toEqual({
297-
resultContent: 'echo result content',
298-
});
299-
});
300271
describe('with request', () => {
301272
test('subscriptions are disabled', () => {
302273
const client = generateClientWithAmplifyInstance<
@@ -344,34 +315,6 @@ describe('server generateClient', () => {
344315
{},
345316
);
346317
});
347-
348-
test('can custom query', async () => {
349-
Amplify.configure(configFixture as any);
350-
const config = Amplify.getConfig();
351-
352-
const client = generateClientWithAmplifyInstance<
353-
Schema,
354-
V6ClientSSRRequest<Schema>
355-
>({
356-
amplify: null,
357-
config: config,
358-
});
359-
360-
const spy = jest
361-
.spyOn(client, 'graphql')
362-
.mockImplementation(async () => {
363-
const result: any = {};
364-
return result;
365-
});
366-
367-
const mockContextSpec = { token: { value: Symbol('test') } };
368-
369-
const result = await client.queries.echo(mockContextSpec, {
370-
argumentContent: 'echo argumentContent value',
371-
});
372-
373-
expect(normalizePostGraphqlCalls(spy)).toMatchSnapshot();
374-
});
375318
});
376319
});
377320
});

‎packages/api-graphql/__tests__/server/generateClient.test.ts.bak

-119
This file was deleted.

‎packages/api-graphql/__tests__/utils/expects.ts

+14-20
Original file line numberDiff line numberDiff line change
@@ -194,31 +194,25 @@ export function expectSubWithHeadersFn(
194194
* @param spy The jest spy to check.
195195
* @param opName The name of the graphql operation. E.g., `onCreateTodo`.
196196
* @param item The item we expect to have been in the `variables`
197-
* @param libraryConfigHeaders TODO
197+
* @param headers client/request and config-level headers
198+
* @param configHeaders resolved config-level headers
198199
*/
199-
export function expectSubWithlibraryConfigHeaders(
200+
export async function expectSubWithlibraryConfigHeaders(
200201
spy: jest.SpyInstance<any, any>,
201202
opName: string,
202203
item: Record<string, any>,
203-
headers?: CustomHeaders,
204+
expectedHeaders?: CustomHeaders,
205+
expectedConfigHeaders?: CustomHeaders,
204206
) {
205-
expect(spy).toHaveBeenCalledWith(
206-
expect.objectContaining({
207-
authenticationType: 'apiKey',
208-
apiKey: 'FAKE-KEY',
209-
appSyncGraphqlEndpoint: 'https://localhost/graphql',
210-
// Code-gen'd queries have an owner param; TypeBeast queries don't:
211-
query: expect.stringContaining(`${opName}(filter: $filter`),
212-
variables: expect.objectContaining(item),
213-
additionalHeaders: expect.objectContaining(headers),
214-
// `headers` that are included in `Amplify.configure` options
215-
libraryConfigHeaders: expect.any(Function),
216-
}),
217-
{
218-
action: '1',
219-
category: 'api',
220-
},
221-
);
207+
const [[{ query, variables, additionalHeaders, libraryConfigHeaders }]] =
208+
spy.mock.calls;
209+
210+
const configHeaders = await libraryConfigHeaders();
211+
212+
expect(query).toEqual(expect.stringContaining(`${opName}(filter: $filter`));
213+
expect(variables).toEqual(expect.objectContaining(item));
214+
expect(additionalHeaders).toEqual(expect.objectContaining(expectedHeaders));
215+
expect(configHeaders).toEqual(expect.objectContaining(expectedConfigHeaders));
222216
}
223217

224218
/**

0 commit comments

Comments
 (0)
Please sign in to comment.