This repository has been archived by the owner on Apr 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcallbacks.ts
93 lines (87 loc) · 2.64 KB
/
callbacks.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
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
/** This module is browser compatible. */
import { MockError } from "./mock.ts";
/** Creates a function that returns the instance the method was called on. */
export function returnsThis<
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(): (this: Self, ...args: Args) => Self {
return function (this: Self): Self {
return this;
};
}
/** Creates a function that returns one of its arguments. */
// deno-lint-ignore no-explicit-any
export function returnsArg<Arg, Self = any>(
idx: number,
): (this: Self, ...args: Arg[]) => Arg {
return function (...args: Arg[]): Arg {
return args[idx];
};
}
/** Creates a function that returns its arguments or a subset of them. If end is specified, it will return arguments up to but not including the end. */
export function returnsArgs<
Args extends unknown[],
// deno-lint-ignore no-explicit-any
Self = any,
>(
start = 0,
end?: number,
): (this: Self, ...args: Args) => Args {
return function (this: Self, ...args: Args): Args {
return args.slice(start, end) as Args;
};
}
/** Creates a function that returns the iterable values. Any iterable values that are errors will be thrown. */
export function returnsNext<
Return,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(
values: Iterable<Return | Error>,
): (this: Self, ...args: Args) => Return {
const gen = (function* returnsValue() {
yield* values;
})();
let calls = 0;
return function () {
const next = gen.next();
if (next.done) {
throw new MockError(`not expected to be called more than ${calls} times`);
}
calls++;
const { value } = next;
if (value instanceof Error) throw value;
return value;
};
}
/** Creates a function that resolves the awaited iterable values. Any awaited iterable values that are errors will be thrown. */
export function resolvesNext<
Return,
// deno-lint-ignore no-explicit-any
Self = any,
// deno-lint-ignore no-explicit-any
Args extends unknown[] = any[],
>(
iterable:
| Iterable<Return | Error | Promise<Return | Error>>
| AsyncIterable<Return | Error | Promise<Return | Error>>,
): (this: Self, ...args: Args) => Promise<Return> {
const gen = (async function* returnsValue() {
yield* iterable;
})();
let calls = 0;
return async function () {
const next = await gen.next();
if (next.done) {
throw new MockError(`not expected to be called more than ${calls} times`);
}
calls++;
const { value } = next;
if (value instanceof Error) throw value;
return value;
};
}