-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.js
118 lines (97 loc) · 2.89 KB
/
operations.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
export const hasBeenCalled = (espionage) => espionage.report.calls.length > 0;
export const calls = (espionage) => espionage.report.calls;
export const results = (espionage) => espionage.report.results;
export const instances = (espionage) => espionage.report.instances;
export const spyName = (espionage) => espionage.report.spyName;
export const getReadCount = (espionage) => (key) =>
espionage.report.readCount[key] || 0;
export function setSpyName({ report }, spy) {
return (givenName) => {
report.spyName = givenName;
return spy;
};
}
export function fakeValue(espionage, spy) {
return (implementation) => {
espionage.update((report) => {
report.returnValue = () => implementation;
});
return spy;
};
}
export function fakeFunction(espionage, spy) {
return (implementation) => {
espionage.update((report) => {
report.returnValue = implementation;
});
return spy;
};
}
export function throws(espionage, spy) {
return (err) => {
espionage.update((report) => {
report.returnValue = () => {
if (err instanceof Error) throw err;
throw new Error(err);
};
});
return spy;
};
}
export function fakeResolvedValue(espionage, spy) {
return (givenValue) => {
espionage.update((report) => {
report.returnValue = async () => givenValue;
});
return spy;
};
}
export function fakeRejectedValue({ update }, spy) {
return (givenValue) => {
update((report) => {
report.returnValue = () => Promise.reject(givenValue);
});
return spy;
};
}
export function fakeFunctionOnce({ update }, spy) {
return (implementation) => {
update((report) => report.oneTime.push(implementation));
return spy;
};
}
export function fakeValueOnce({ update }, spy) {
return (givenValue) => {
update((report) => report.oneTime.push(() => givenValue));
return spy;
};
}
export function fakeResolvedValueOnce({ update }, spy) {
return (givenValue) => {
update(({ oneTime }) => oneTime.push(() => Promise.resolve(givenValue)));
return spy;
};
}
export function fakeRejectedValueOnce({ update }, spy) {
return (givenValue) => {
update(({ oneTime }) => oneTime.push(() => Promise.reject(givenValue)));
return spy;
};
}
export function planRehearsals({ update }, spy) {
return (givenRehearsals) => {
if (!Array.isArray(givenRehearsals)) givenRehearsals = [givenRehearsals];
const refinedRehearsals = givenRehearsals.map((rehearsal) => {
let finalValue = rehearsal.returns || rehearsal.resolves;
const isAFunction = typeof finalValue === "function";
if (!isAFunction && "resolves" in rehearsal)
finalValue = Promise.resolve(finalValue);
return {
given: rehearsal.given,
finalValue: isAFunction ? finalValue : () => finalValue,
};
});
update(({ rehearsals }) => rehearsals.push(...refinedRehearsals));
return spy;
};
}