Skip to content

Commit 4f11d4c

Browse files
authored
feat: add commandCall method to AwsStub (#241)
1 parent dfdbc83 commit 4f11d4c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

packages/aws-sdk-client-mock/src/awsClientStub.ts

+27
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,33 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer, TCon
131131
});
132132
}
133133

134+
/**
135+
* Returns n-th call of given Command only.
136+
* @param n Index of the call
137+
* @param commandType Command type to match
138+
* @param input Command payload to match
139+
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match)
140+
*/
141+
commandCall<TCmd extends AwsCommand<any, any>,
142+
TCmdInput extends TCmd extends AwsCommand<infer TIn, any> ? TIn : never,
143+
TCmdOutput extends TCmd extends AwsCommand<any, infer TOut> ? TOut : never,
144+
>(
145+
n: number,
146+
commandType: new (input: TCmdInput) => TCmd,
147+
input?: Partial<TCmdInput>,
148+
strict?: boolean,
149+
): SinonSpyCall<[TCmd], Promise<TCmdOutput>> {
150+
const calls = this.commandCalls(commandType, input, strict);
151+
if (n < 0) {
152+
n += calls.length;
153+
}
154+
if (n >= calls.length) {
155+
// @ts-expect-error this matches the behaviour of the call method.
156+
return null;
157+
}
158+
return calls[n];
159+
}
160+
134161
/**
135162
* Allows specifying the behavior for any Command with given input (parameters).
136163
*

packages/aws-sdk-client-mock/test/mockClient.test.ts

+11
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,17 @@ describe('spying on the mock', () => {
127127
expect(snsMock.commandCalls(PublishCommand, {...publishCmd1.input}, true)).toHaveLength(1);
128128
});
129129

130+
it('finds nth call of given command type', async () => {
131+
const sns = new SNSClient({});
132+
await sns.send(publishCmd1);
133+
await sns.send(publishCmd2);
134+
135+
expect(snsMock.commandCall(0, PublishCommand).args[0].input).toStrictEqual(publishCmd1.input);
136+
expect(snsMock.commandCall(1, PublishCommand).args[0].input).toStrictEqual(publishCmd2.input);
137+
expect(snsMock.commandCall(-1, PublishCommand).args[0].input).toStrictEqual(publishCmd2.input);
138+
expect(snsMock.commandCall(2, PublishCommand)).toBeNull();
139+
});
140+
130141
it('resets calls history', async () => {
131142
const sns = new SNSClient({});
132143
await sns.send(publishCmd1);

0 commit comments

Comments
 (0)