-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmock-apl.ts
69 lines (51 loc) · 1.46 KB
/
mock-apl.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
import { vi } from "vitest";
import { APL, AuthData } from "../APL";
type Options = {
workForApiUrl?: string;
savedAllAuthData?: AuthData[];
};
/**
* Test utility used across scenarios to simulate various APL behaviors
*/
export class MockAPL implements APL {
private readonly options: Options = {
workForApiUrl: "https://example.com/graphql/",
savedAllAuthData: [],
};
constructor(opts?: Options) {
this.options = {
...this.options,
...(opts ?? {}),
};
this.workingSaleorApiUrl = this.options.workForApiUrl ?? this.workingSaleorApiUrl;
}
mockJwks = "{}";
mockToken = "mock-token";
mockAppId = "mock-app-id";
workingSaleorApiUrl = "https://example.com/graphql/";
resolveDomainFromApiUrl = (apiUrl: string) =>
apiUrl.replace("/graphql/", "").replace("https://", "");
get workingSaleorDomain() {
return this.resolveDomainFromApiUrl(this.workingSaleorApiUrl);
}
async get(saleorApiUrl: string) {
if (saleorApiUrl === this.workingSaleorApiUrl) {
return {
token: this.mockToken,
saleorApiUrl,
appId: this.mockAppId,
jwks: this.mockJwks,
};
}
return undefined;
}
set = vi.fn();
delete = vi.fn();
getAll = vi.fn().mockImplementation(async () => this.options.savedAllAuthData);
isReady = vi.fn().mockImplementation(async () => ({
ready: true,
}));
isConfigured = vi.fn().mockImplementation(async () => ({
configured: true,
}));
}