-
Notifications
You must be signed in to change notification settings - Fork 42
/
cancel.spec.ts
35 lines (30 loc) · 1.15 KB
/
cancel.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import {describe, expect, it, jest} from '@jest/globals';
import MockAxios from "../lib/index";
import { getData } from "./cancel";
import Cancel from "../lib/cancel/Cancel";
describe("cancel", () => {
it("cancels request (shouldCancel = true)", (done) => {
const thenFn = jest.fn();
const catchFn = jest.fn();
const promise = getData(true).then(thenFn).catch(catchFn)
setTimeout(async () => {
MockAxios.mockResponse({data: "example response"}, undefined, true);
await promise;
expect(thenFn).not.toHaveBeenCalled()
expect(catchFn).toHaveBeenCalledWith(expect.any(Cancel))
done()
})
})
it("does not cancel request (shouldCancel = false)", (done) => {
const thenFn = jest.fn();
const catchFn = jest.fn();
const promise = getData(false).then(thenFn).catch(catchFn)
setTimeout(async () => {
MockAxios.mockResponse({data: "example response"}, undefined, true);
await promise;
expect(thenFn).toHaveBeenCalled()
expect(catchFn).not.toHaveBeenCalled()
done()
})
})
})