Skip to content

Commit 7b61e4f

Browse files
committed
test: replace onQuery and onMutation methods with a unified resolver method in tests
1 parent 2f58fbe commit 7b61e4f

File tree

2 files changed

+54
-41
lines changed

2 files changed

+54
-41
lines changed

packages/event-handler/tests/unit/appsync-graphql/AppSyncGraphQLResolver.test.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,18 @@ describe('Class: AppSyncGraphQLResolver', () => {
6969
it('returns the response of the onQuery handler', async () => {
7070
// Prepare
7171
const app = new AppSyncGraphQLResolver({ logger: console });
72-
app.onQuery('getPost', async ({ id }) => {
73-
return {
74-
id,
75-
title: 'Post Title',
76-
content: 'Post Content',
77-
};
78-
});
72+
app.resolver<{ id: string }>(
73+
async ({ id }) => {
74+
return {
75+
id,
76+
title: 'Post Title',
77+
content: 'Post Content',
78+
};
79+
},
80+
{
81+
fieldName: 'getPost',
82+
}
83+
);
7984

8085
// Act
8186
const result = await app.resolve(
@@ -94,13 +99,19 @@ describe('Class: AppSyncGraphQLResolver', () => {
9499
it('returns the response of the onMutation handler', async () => {
95100
// Prepare
96101
const app = new AppSyncGraphQLResolver({ logger: console });
97-
app.onMutation('addPost', async ({ title, content }) => {
98-
return {
99-
id: '123',
100-
title,
101-
content,
102-
};
103-
});
102+
app.resolver<{ title: string; content: string }>(
103+
async ({ title, content }) => {
104+
return {
105+
id: '123',
106+
title,
107+
content,
108+
};
109+
},
110+
{
111+
fieldName: 'addPost',
112+
typeName: 'Mutation',
113+
}
114+
);
104115

105116
// Act
106117
const result = await app.resolve(
@@ -114,15 +125,11 @@ describe('Class: AppSyncGraphQLResolver', () => {
114125
// Assess
115126
expect(console.debug).toHaveBeenNthCalledWith(
116127
1,
117-
'Adding onMutation resolver for field Mutation.addPost'
128+
'Adding resolver for field Mutation.addPost'
118129
);
119130
expect(console.debug).toHaveBeenNthCalledWith(
120131
2,
121-
'Looking for onQuery resolver for type=Mutation, field=addPost'
122-
);
123-
expect(console.debug).toHaveBeenNthCalledWith(
124-
3,
125-
'Looking for onMutation resolver for type=Mutation, field=addPost'
132+
'Looking for resolver for type=Mutation, field=addPost'
126133
);
127134
expect(result).toEqual({
128135
id: '123',
@@ -152,9 +159,15 @@ describe('Class: AppSyncGraphQLResolver', () => {
152159
async ({ error, message }) => {
153160
// Prepare
154161
const app = new AppSyncGraphQLResolver({ logger: console });
155-
app.onMutation('addPost', async () => {
156-
throw error;
157-
});
162+
app.resolver(
163+
async () => {
164+
throw error;
165+
},
166+
{
167+
fieldName: 'addPost',
168+
typeName: 'Mutation',
169+
}
170+
);
158171

159172
// Act
160173
const result = await app.resolve(

packages/event-handler/tests/unit/appsync-graphql/Router.test.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,17 @@ describe('Class: Router', () => {
1313
const addPost = vi.fn(async () => true);
1414

1515
// Act
16-
router.onQuery('getPost', getPost, { typeName: 'Query' });
17-
router.onMutation('addPost', addPost, { typeName: 'Mutation' });
16+
router.resolver(getPost, { typeName: 'Query', fieldName: 'getPost' });
17+
router.resolver(addPost, { typeName: 'Mutation', fieldName: 'addPost' });
1818

1919
// Assess
2020
expect(console.debug).toHaveBeenNthCalledWith(
2121
1,
22-
'Adding onQuery resolver for field Query.getPost'
22+
'Adding resolver for field Query.getPost'
2323
);
2424
expect(console.debug).toHaveBeenNthCalledWith(
2525
2,
26-
'Adding onMutation resolver for field Mutation.addPost'
26+
'Adding resolver for field Mutation.addPost'
2727
);
2828
});
2929

@@ -35,22 +35,22 @@ describe('Class: Router', () => {
3535
class Lambda {
3636
readonly prop = 'value';
3737

38-
@router.onQuery('getPost')
38+
@router.resolver({ fieldName: 'getPost' })
3939
public getPost() {
4040
return `${this.prop} foo`;
4141
}
4242

43-
@router.onQuery('getAuthor', { typeName: 'Query' })
43+
@router.resolver({ fieldName: 'getAuthor', typeName: 'Query' })
4444
public getAuthor() {
4545
return `${this.prop} bar`;
4646
}
4747

48-
@router.onMutation('addPost')
48+
@router.resolver({ fieldName: 'addPost', typeName: 'Mutation' })
4949
public addPost() {
5050
return `${this.prop} bar`;
5151
}
5252

53-
@router.onMutation('updatePost', { typeName: 'Mutation' })
53+
@router.resolver({ fieldName: 'updatePost', typeName: 'Mutation' })
5454
public updatePost() {
5555
return `${this.prop} baz`;
5656
}
@@ -64,19 +64,19 @@ describe('Class: Router', () => {
6464
// Assess
6565
expect(console.debug).toHaveBeenNthCalledWith(
6666
1,
67-
'Adding onQuery resolver for field Query.getPost'
67+
'Adding resolver for field Query.getPost'
6868
);
6969
expect(console.debug).toHaveBeenNthCalledWith(
7070
2,
71-
'Adding onQuery resolver for field Query.getAuthor'
71+
'Adding resolver for field Query.getAuthor'
7272
);
7373
expect(console.debug).toHaveBeenNthCalledWith(
7474
3,
75-
'Adding onMutation resolver for field Mutation.addPost'
75+
'Adding resolver for field Mutation.addPost'
7676
);
7777
expect(console.debug).toHaveBeenNthCalledWith(
7878
4,
79-
'Adding onMutation resolver for field Mutation.updatePost'
79+
'Adding resolver for field Mutation.updatePost'
8080
);
8181

8282
// verify that class scope is preserved after decorating
@@ -94,8 +94,8 @@ describe('Class: Router', () => {
9494
class Lambda {
9595
readonly prop = 'value';
9696

97-
@router.onQuery('listLocations')
98-
@router.onQuery('locations')
97+
@router.resolver({ fieldName: 'listLocations' })
98+
@router.resolver({ fieldName: 'locations' })
9999
public getLocations() {
100100
return [{ name: 'Location 1', description: 'Description 1' }];
101101
}
@@ -106,11 +106,11 @@ describe('Class: Router', () => {
106106
// Assess
107107
expect(console.debug).toHaveBeenNthCalledWith(
108108
1,
109-
'Adding onQuery resolver for field Query.locations'
109+
'Adding resolver for field Query.locations'
110110
);
111111
expect(console.debug).toHaveBeenNthCalledWith(
112112
2,
113-
'Adding onQuery resolver for field Query.listLocations'
113+
'Adding resolver for field Query.listLocations'
114114
);
115115

116116
expect(response).toEqual([
@@ -123,7 +123,7 @@ describe('Class: Router', () => {
123123
const router = new Router();
124124

125125
// Act
126-
router.onQuery('getPost', vi.fn());
126+
router.resolver(vi.fn(), { fieldName: 'getPost' });
127127

128128
// Assess
129129
expect(console.debug).not.toHaveBeenCalled();
@@ -135,7 +135,7 @@ describe('Class: Router', () => {
135135
const router = new Router();
136136

137137
// Act
138-
router.onQuery('getPost', vi.fn());
138+
router.resolver(vi.fn(), { fieldName: 'getPost' });
139139

140140
// Assess
141141
expect(console.debug).toHaveBeenCalled();

0 commit comments

Comments
 (0)