Skip to content

Commit a53340f

Browse files
committed
add test with sortkey fields
1 parent fcca641 commit a53340f

File tree

2 files changed

+68
-2
lines changed

2 files changed

+68
-2
lines changed

packages/appsync-modelgen-plugin/src/__tests__/utils/process-index.test.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,72 @@ describe('processIndex', () => {
194194
]);
195195
});
196196

197+
it('should generate a default name to @index directive with sortkeys', () => {
198+
const model: CodeGenModel = {
199+
directives: [
200+
{
201+
name: 'model',
202+
arguments: {},
203+
},
204+
],
205+
name: 'testModel',
206+
type: 'model',
207+
fields: [
208+
{
209+
type: 'field',
210+
isList: false,
211+
isNullable: true,
212+
name: 'connectionField',
213+
directives: [
214+
{
215+
name: 'index',
216+
arguments: {
217+
sortKeyFields: ['sortField'],
218+
},
219+
},
220+
],
221+
},
222+
{
223+
type: 'field',
224+
isList: false,
225+
isNullable: true,
226+
name: 'anotherConnection',
227+
directives: [
228+
{
229+
name: 'index',
230+
arguments: {
231+
sortKeyFields: ['sortField1', 'sortField2'],
232+
},
233+
},
234+
],
235+
},
236+
],
237+
};
238+
processIndex(model);
239+
expect(model.directives).toEqual([
240+
{
241+
name: 'model',
242+
arguments: {},
243+
},
244+
{
245+
name: 'key',
246+
arguments: {
247+
name: 'testModelsByConnectionFieldAndSortField',
248+
fields: ['connectionField', 'sortField'],
249+
queryField: undefined,
250+
},
251+
},
252+
{
253+
name: 'key',
254+
arguments: {
255+
name: 'testModelsByAnotherConnectionAndSortField1AndSortField2',
256+
fields: ['anotherConnection', 'sortField1', 'sortField2'],
257+
queryField: undefined,
258+
},
259+
},
260+
]);
261+
});
262+
197263
it('does nothing if no @index directives in model', () => {
198264
const model: CodeGenModel = {
199265
directives: [

packages/appsync-modelgen-plugin/src/utils/process-index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ export const processIndex = (model: CodeGenModel) => {
3535
* Accepts a model and field name, and potentially empty list of sortKeyFields to generate a unique index name.
3636
* e.g. modelName = Employee, fieldName = manager, sortKeyFields = [level]
3737
* will generate a name like employeeByManagerAndLevel.
38-
*
39-
* This naming logic is used to generate a default index name for @index directives that don't have a name argument.
38+
* (This naming logic is used to generate a default index name for @index directives that don't have a name argument).
39+
* Refer https://github.com/aws-amplify/amplify-category-api/blob/main/packages/amplify-graphql-index-transformer/src/utils.ts
4040
*/
4141
export const generateDefaultIndexName = (modelName: string, fieldNames: string[]): string => {
4242
return `${toLower(pluralize(modelName))}By${fieldNames.map(toUpper).join('And')}`;

0 commit comments

Comments
 (0)