@tutao/tutanota-test-utils 3.91.2 → 3.91.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/TestUtils.d.ts +34 -0
- package/dist/TestUtils.js +114 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mocks an attribute (function or object) on an object and makes sure that it can be restored to the original attribute by calling unmockAttribute() later.
|
|
3
|
+
* Additionally creates a spy for the attribute if the attribute is a function.
|
|
4
|
+
* @param object The object on which the attribute exists.
|
|
5
|
+
* @param attributeOnObject The attribute to mock.
|
|
6
|
+
* @param attributeMock The attribute mock.
|
|
7
|
+
* @returns An object to be passed to unmockAttribute() in order to restore the original attribute.
|
|
8
|
+
*/
|
|
9
|
+
export declare function mockAttribute(object: Record<string, any>, attributeOnObject: ((...args: Array<any>) => any) | Record<string, any>, attributeMock: ((...args: Array<any>) => any) | Record<string, any>): Record<string, any>;
|
|
10
|
+
export declare function unmockAttribute(mock: Record<string, any>): void;
|
|
11
|
+
export declare type Spy = ((...args: any) => any) & {
|
|
12
|
+
invocations: any[];
|
|
13
|
+
};
|
|
14
|
+
export declare function spy(producer?: (...args: any) => any): Spy;
|
|
15
|
+
/**
|
|
16
|
+
* Create partial mock, i.e. allows mocking attributes or functions on actual instances
|
|
17
|
+
* @param obj The base mock object on which mocker may overwrite attributes or functions
|
|
18
|
+
* @param mocker This function receives obj and can overwrite attributes or functions.
|
|
19
|
+
* @returns {T}
|
|
20
|
+
*/
|
|
21
|
+
export declare const mock: <T>(obj: T, mocker: (arg0: any) => any) => T;
|
|
22
|
+
export declare function mapToObject<K extends string | number | symbol, V>(map: Map<K, V>): Record<K, V>;
|
|
23
|
+
export declare function mapObject<K extends string | number | symbol, V, R>(mapper: (arg0: V) => R, obj: Record<K, V>): Record<K, R>;
|
|
24
|
+
export declare function replaceAllMaps(toReplace: any): any;
|
|
25
|
+
/** Catch error and return either value or error */
|
|
26
|
+
export declare function asResult<T>(p: Promise<T>): Promise<T | Error>;
|
|
27
|
+
export declare function assertThrows<T extends Error>(expected: Class<T>, fn: () => Promise<unknown>): Promise<T>;
|
|
28
|
+
export declare function assertResolvedIn(ms: number, ...promises: ReadonlyArray<Promise<any>>): Promise<any>;
|
|
29
|
+
export declare function assertNotResolvedIn(ms: number, ...promises: ReadonlyArray<Promise<any>>): Promise<any>;
|
|
30
|
+
export interface TimeoutMock {
|
|
31
|
+
(fn: () => unknown, time: number): ReturnType<typeof setTimeout>;
|
|
32
|
+
next(): void;
|
|
33
|
+
}
|
|
34
|
+
export declare function makeTimeoutMock(): TimeoutMock;
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import o from "ospec";
|
|
2
|
+
/**
|
|
3
|
+
* Mocks an attribute (function or object) on an object and makes sure that it can be restored to the original attribute by calling unmockAttribute() later.
|
|
4
|
+
* Additionally creates a spy for the attribute if the attribute is a function.
|
|
5
|
+
* @param object The object on which the attribute exists.
|
|
6
|
+
* @param attributeOnObject The attribute to mock.
|
|
7
|
+
* @param attributeMock The attribute mock.
|
|
8
|
+
* @returns An object to be passed to unmockAttribute() in order to restore the original attribute.
|
|
9
|
+
*/
|
|
10
|
+
export function mockAttribute(object, attributeOnObject, attributeMock) {
|
|
11
|
+
if (attributeOnObject == null)
|
|
12
|
+
throw new Error("attributeOnObject is undefined");
|
|
13
|
+
let attributeName = Object.getOwnPropertyNames(object).find(key => object[key] === attributeOnObject);
|
|
14
|
+
if (!attributeName) {
|
|
15
|
+
attributeName = Object.getOwnPropertyNames(Object.getPrototypeOf(object)).find(key => object[key] === attributeOnObject);
|
|
16
|
+
}
|
|
17
|
+
if (!attributeName) {
|
|
18
|
+
throw new Error("attribute not found on object");
|
|
19
|
+
}
|
|
20
|
+
object[attributeName] = typeof attributeOnObject == "function" ? o.spy(attributeMock) : attributeMock;
|
|
21
|
+
return {
|
|
22
|
+
_originalObject: object,
|
|
23
|
+
_originalAttribute: attributeOnObject,
|
|
24
|
+
_attributeName: attributeName,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function unmockAttribute(mock) {
|
|
28
|
+
mock._originalObject[mock._attributeName] = mock._originalAttribute;
|
|
29
|
+
}
|
|
30
|
+
export function spy(producer) {
|
|
31
|
+
const invocations = [];
|
|
32
|
+
const s = (...args) => {
|
|
33
|
+
invocations.push(args);
|
|
34
|
+
return producer && producer(...args);
|
|
35
|
+
};
|
|
36
|
+
s.invocations = invocations;
|
|
37
|
+
return s;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Create partial mock, i.e. allows mocking attributes or functions on actual instances
|
|
41
|
+
* @param obj The base mock object on which mocker may overwrite attributes or functions
|
|
42
|
+
* @param mocker This function receives obj and can overwrite attributes or functions.
|
|
43
|
+
* @returns {T}
|
|
44
|
+
*/
|
|
45
|
+
export const mock = (obj, mocker) => {
|
|
46
|
+
mocker(obj);
|
|
47
|
+
return obj;
|
|
48
|
+
};
|
|
49
|
+
export function mapToObject(map) {
|
|
50
|
+
const obj = {};
|
|
51
|
+
map.forEach((value, key) => {
|
|
52
|
+
obj[key] = value;
|
|
53
|
+
});
|
|
54
|
+
return obj;
|
|
55
|
+
}
|
|
56
|
+
export function mapObject(mapper, obj) {
|
|
57
|
+
const newObj = {};
|
|
58
|
+
for (let key of Object.keys(obj)) {
|
|
59
|
+
newObj[key] = mapper(obj[key]);
|
|
60
|
+
}
|
|
61
|
+
return newObj;
|
|
62
|
+
}
|
|
63
|
+
export function replaceAllMaps(toReplace) {
|
|
64
|
+
return toReplace instanceof Map
|
|
65
|
+
? replaceAllMaps(mapToObject(toReplace))
|
|
66
|
+
: toReplace instanceof Array
|
|
67
|
+
? toReplace.map(replaceAllMaps)
|
|
68
|
+
: toReplace != null && Object.getPrototypeOf(toReplace) === Object.prototype // plain object
|
|
69
|
+
? mapObject(replaceAllMaps, toReplace)
|
|
70
|
+
: toReplace;
|
|
71
|
+
}
|
|
72
|
+
/** Catch error and return either value or error */
|
|
73
|
+
export async function asResult(p) {
|
|
74
|
+
return p.catch(e => e);
|
|
75
|
+
}
|
|
76
|
+
export async function assertThrows(expected, fn) {
|
|
77
|
+
try {
|
|
78
|
+
await fn();
|
|
79
|
+
}
|
|
80
|
+
catch (e) {
|
|
81
|
+
o(e instanceof expected).equals(true)("AssertThrows failed: Expected a " + expected + " to be thrown, but got a " + e.constructor);
|
|
82
|
+
return e;
|
|
83
|
+
}
|
|
84
|
+
throw new Error("AssertThrows failed: Expected a " + expected + " to be thrown, but nothing was");
|
|
85
|
+
}
|
|
86
|
+
export async function assertResolvedIn(ms, ...promises) {
|
|
87
|
+
const allP = [delay(ms).then(() => "timeout")].concat(promises.map((p, i) => p.then(() => `promise ${i} is resolved`)));
|
|
88
|
+
const result = await Promise.race(allP);
|
|
89
|
+
o(result).notEquals("timeout");
|
|
90
|
+
}
|
|
91
|
+
export async function assertNotResolvedIn(ms, ...promises) {
|
|
92
|
+
const allP = [delay(ms).then(() => "timeout")].concat(promises.map((p, i) => p.then(() => `promise ${i} is resolved`)));
|
|
93
|
+
const result = await Promise.race(allP);
|
|
94
|
+
o(result).equals("timeout");
|
|
95
|
+
}
|
|
96
|
+
export function makeTimeoutMock() {
|
|
97
|
+
let timeoutId = 1;
|
|
98
|
+
let scheduledFn;
|
|
99
|
+
const timeoutMock = function (fn) {
|
|
100
|
+
scheduledFn = fn;
|
|
101
|
+
timeoutId++;
|
|
102
|
+
return timeoutId;
|
|
103
|
+
};
|
|
104
|
+
const spiedMock = o.spy(timeoutMock);
|
|
105
|
+
spiedMock.next = function () {
|
|
106
|
+
scheduledFn && scheduledFn();
|
|
107
|
+
};
|
|
108
|
+
return spiedMock;
|
|
109
|
+
}
|
|
110
|
+
function delay(ms) {
|
|
111
|
+
return new Promise(resolve => {
|
|
112
|
+
setTimeout(resolve, ms);
|
|
113
|
+
});
|
|
114
|
+
}
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { mockAttribute, unmockAttribute, spy, mock, mapToObject, mapObject, replaceAllMaps, asResult, assertThrows, assertResolvedIn, assertNotResolvedIn, makeTimeoutMock, } from "./TestUtils.js";
|