Skip to content

Commit

Permalink
feat: add commandCall method to AwsStub
Browse files Browse the repository at this point in the history
fixes: #240
  • Loading branch information
icholy committed Oct 15, 2024
1 parent dfdbc83 commit 804139d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/aws-sdk-client-mock/src/awsClientStub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,33 @@ export class AwsStub<TInput extends object, TOutput extends MetadataBearer, TCon
});
}

/**
* Returns n-th call of given Command only.
* @param n Index of the call
* @param commandType Command type to match
* @param input Command payload to match
* @param strict Should the payload match strictly (default false, will match if all defined payload properties match)
*/
commandCall<TCmd extends AwsCommand<any, any>,
TCmdInput extends TCmd extends AwsCommand<infer TIn, any> ? TIn : never,
TCmdOutput extends TCmd extends AwsCommand<any, infer TOut> ? TOut : never,
>(
n: number,
commandType: new (input: TCmdInput) => TCmd,
input?: Partial<TCmdInput>,
strict?: boolean,
): SinonSpyCall<[TCmd], Promise<TCmdOutput>> {
const calls = this.commandCalls(commandType, input, strict);
if (n < 0) {
n += calls.length;
}
if (n >= calls.length) {
// @ts-expect-error

Check failure on line 155 in packages/aws-sdk-client-mock/src/awsClientStub.ts

View workflow job for this annotation

GitHub Actions / Test and build

Include a description after the "@ts-expect-error" directive to explain why the @ts-expect-error is necessary. The description must be 3 characters or longer
return null;
}
return calls[n];
}

/**
* Allows specifying the behavior for any Command with given input (parameters).
*
Expand Down
11 changes: 11 additions & 0 deletions packages/aws-sdk-client-mock/test/mockClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,17 @@ describe('spying on the mock', () => {
expect(snsMock.commandCalls(PublishCommand, {...publishCmd1.input}, true)).toHaveLength(1);
});

it('finds nth call of given command type', async () => {
const sns = new SNSClient({});
await sns.send(publishCmd1);
await sns.send(publishCmd2);

expect(snsMock.commandCall(0, PublishCommand).args[0].input).toStrictEqual(publishCmd1.input);
expect(snsMock.commandCall(1, PublishCommand).args[0].input).toStrictEqual(publishCmd2.input);
expect(snsMock.commandCall(-1, PublishCommand).args[0].input).toStrictEqual(publishCmd2.input);
expect(snsMock.commandCall(2, PublishCommand)).toBeNull();
});

it('resets calls history', async () => {
const sns = new SNSClient({});
await sns.send(publishCmd1);
Expand Down

0 comments on commit 804139d

Please sign in to comment.