-
-
Notifications
You must be signed in to change notification settings - Fork 224
/
Copy pathtoHaveBeenCalledAfter.js
60 lines (49 loc) · 2.34 KB
/
toHaveBeenCalledAfter.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { isJestMockOrSpy } from '../utils';
export function toHaveBeenCalledAfter(actual, expected, failIfNoFirstInvocation = true) {
const { printReceived, printExpected, matcherHint } = this.utils;
if (!isJestMockOrSpy(actual)) {
return { pass: false, message: mockCheckFailMessage(this.utils, actual, true) };
}
if (!isJestMockOrSpy(expected)) {
return { pass: false, message: mockCheckFailMessage(this.utils, expected, false) };
}
const firstInvocationCallOrder = actual.mock.invocationCallOrder;
const secondInvocationCallOrder = expected.mock.invocationCallOrder;
const pass = predicate(firstInvocationCallOrder, secondInvocationCallOrder, failIfNoFirstInvocation);
return {
pass,
message: () =>
pass
? matcherHint('.not.toHaveBeenCalledAfter') +
'\n\n' +
'Expected first mock to not have been called after, invocationCallOrder:\n' +
` ${printExpected(firstInvocationCallOrder)}\n` +
'Received second mock with invocationCallOrder:\n' +
` ${printReceived(secondInvocationCallOrder)}`
: matcherHint('.toHaveBeenCalledAfter') +
'\n\n' +
'Expected first mock to have been called after, invocationCallOrder:\n' +
` ${printExpected(firstInvocationCallOrder)}\n` +
'Received second mock with invocationCallOrder:\n' +
` ${printReceived(secondInvocationCallOrder)}`,
};
}
const smallest = ns => ns.reduce((acc, n) => (acc < n ? acc : n));
const predicate = (firstInvocationCallOrder, secondInvocationCallOrder, failIfNoFirstInvocation) => {
if (firstInvocationCallOrder.length === 0) return !failIfNoFirstInvocation;
if (secondInvocationCallOrder.length === 0) return false;
const firstSmallest = smallest(firstInvocationCallOrder);
const secondSmallest = smallest(secondInvocationCallOrder);
return firstSmallest > secondSmallest;
};
const mockCheckFailMessage = (utils, value, isReceivedValue) => () => {
const valueKind = isReceivedValue ? 'Received' : 'Expected';
const valueKindPrintFunc = isReceivedValue ? utils.printReceived : utils.printExpected;
return (
utils.matcherHint('.toHaveBeenCalledAfter') +
'\n\n' +
`Matcher error: ${valueKindPrintFunc(valueKind.toLowerCase())} must be a mock or spy function` +
'\n\n' +
utils.printWithType(valueKind, value, valueKindPrintFunc)
);
};