Well, in actual use I expect that my post request will end up getting rate limited. I wrote some error handling for HTTP 429 responses that is getting handled within the retry delay argument (I just put it as 3000 for demo purposes). Because this is used to get an Auth token to use with other API calls, it's pretty essential that I get this function correct. So to answer you question, I'd like to test this as precisely as possible.
Here's my incomplete test so far:
it("should retry maxRetries times when failed", () => {
const delay = service.retryDelayMs;
const retries = service.maxRetries;
const totalDelay = delay * retries;
const errorStub = {
message: "Request failed with status code 4XX",
code: AxiosError.ERR_BAD_REQUEST,
config: undefined,
request: undefined,
response: {
status: HttpStatus.BAD_REQUEST,
statusText: "BAD REQUEST",
headers: {},
config: {} as any,
data: {},
},
};
scheduler.run((helpers) => {
const { cold } = helpers;
const coldPost$ = cold(" #", undefined, errorStub);
http.post.mockImplementation(() => coldPost$);
const source = service.renewToken$();
const expectedMarbles = ` ${totalDelay}ms #`;
helpers
.expectObservable(source)
.toBe(expectedMarbles, undefined, errorStub);
});
});
The test is currently passing, but again, it's incomplete (I'm not even sure if it's correct to be entirely honest). It's not yet testing that the request is attempted n times total. When I put a spy on http.post, the spy was only being called 1 time, but the subscription is definitely happening 3 times. I am a little confused, because I had thought that when retry resubscribed it should be calling http.post again, right? So really what I want is to test that http.post is being subscribed to n times, and / or invoked n times.