-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsetup_tests.ts
76 lines (63 loc) · 2.22 KB
/
setup_tests.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
import {afterAll, beforeAll, vi} from "vitest";
import '@testing-library/jest-dom/vitest';
import {resolve} from "node:path";
import {createIdentity, PocketIc} from "@hadronous/pic";
import {idlFactory} from "./src/declarations/backend";
import {_SERVICE} from ".dfx/local/canisters/backend/service.did";
import {Identity} from "@dfinity/agent";
import {RegisterUser, User} from "./src/declarations/backend/backend.did";
import {randomString} from "./src/frontend/data_processing/data_samples";
vi.stubGlobal('matchMedia', () => ({
addEventListener: () => {
}
}));
const WASM_PATH = resolve(__dirname, "target", "wasm32-unknown-unknown", "release", "backend.wasm");
const setupTestEnvironment = async () => {
const alice = createIdentity("1");
const pic = await PocketIc.create();
const fixture = await pic.setupCanister<_SERVICE>(idlFactory, WASM_PATH);
fixture.actor.setIdentity(alice);
global.pic = pic;
global.user = alice;
global.actor = fixture.actor;
global.is_global_set = true;
const loginAs = async (user: Identity) => {
fixture.actor.setIdentity(user);
return fixture.actor;
};
const register = async () => {
const input: RegisterUser = {
name: ["name1"],
description: ["Something"],
photo: [[]],
};
await global.actor.register(input);
};
await register();
const newUser = async (login: boolean = false, name = randomString()) => {
const newUser: Identity = createIdentity(randomString());
global.actor.setIdentity(newUser);
const newInput: RegisterUser = {
name: [name],
description: ["Something"],
photo: [[]],
};
const result: undefined | { Ok: User } | { Err: string } = await global.actor.register(newInput);
if (!login) {
global.actor.setIdentity(alice);
}
return newUser;
};
global.loginAs = loginAs;
global.register = register;
global.newUser = newUser;
};
beforeAll(setupTestEnvironment);
afterAll(async () => {
await global.pic.tearDown();
delete global.lol;
delete global.pic;
delete global.actor;
delete global.loginAs;
delete global.user;
});