forked from coveo/search-ui-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
73 lines (68 loc) · 2.09 KB
/
utils.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
import { IActionHistory } from '../src/rest/UserProfilingEndpoint';
import { SearchEndpoint } from 'coveo-search-ui';
import { SinonSandbox } from 'sinon';
import { UserProfileModel } from '../src/Index';
/**
* Create an access token from a SearchEndpoint.
* @param token A search token.
*/
export function buildAccessToken(token: string) {
return new SearchEndpoint({
accessToken: token,
renewAccessToken: () => Promise.resolve(token),
restUri: 'https://test.uri.test',
}).accessToken;
}
/**
* Create a fake response payload.
* @param actions Actions to put in response.
*/
export function buildActionHistoryResponse(actions: IActionHistory[]) {
return {
value:
actions &&
actions.map((action) => {
return {
name: action.name,
time: action.time.toString(),
value: JSON.stringify(action.value, null, 0),
};
}),
debug: false,
internalExecutionLog: [''],
executionTime: 0.949252553,
};
}
/**
* Feed a generator with value from 0 to time.
* @param time Number of time to call the generator.
* @param generator A generator function.
*/
export function generate<T>(time: number, generator: (i: number) => T): T[] {
let list = [];
for (let i = 0; i < time; i++) {
list[i] = generator(i);
}
return list;
}
/**
* Exacute a function after a certain delay.
* @param callback The callback to call.
* @param timemout The timeout to wait.
*/
export function delay(callback: () => any, timemout = 0) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(callback());
}, timemout);
});
}
/**
* Create a fake user profile model instance.
* @param root The root of the Search interface.
* @param sandbox A sinon Sandbox.
*/
export function fakeUserProfileModel(root: HTMLElement, sandbox: SinonSandbox) {
(root as any)[`Coveo${UserProfileModel.ID}`] = sandbox.createStubInstance(UserProfileModel);
return (root as any)[`Coveo${UserProfileModel.ID}`];
}