This repository was archived by the owner on Oct 1, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathtestsHelper.ts
More file actions
138 lines (124 loc) · 3.89 KB
/
testsHelper.ts
File metadata and controls
138 lines (124 loc) · 3.89 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { BaseContract, BigNumber, constants, providers } from "ethers";
export const AddressZero = constants.AddressZero;
type defaultMockType = jest.Mock<any, any>;
type SmartContractDataTypes = BigNumber | number | boolean | string; // bytes and unused not included
type EthersResponseType = SmartContractDataTypes | Error;
export const mockResolve = (value?: EthersResponseType): defaultMockType => {
const fn = jest.fn();
if (value instanceof Error) {
fn.mockRejectedValue(value);
} else {
fn.mockResolvedValue(value);
}
return fn;
};
export interface ValidContractMockParameters {
supportInterfaceValue?: boolean | Error;
}
interface MockContractInterface {
supportInterface: jest.Mock;
callStatic: {
supportInterface: jest.Mock;
};
}
export const getMockContract = ({
supportInterfaceValue = true,
}: ValidContractMockParameters): MockContractInterface => {
const supportInterface = mockResolve(supportInterfaceValue);
return {
supportInterface,
callStatic: {
supportInterface,
},
};
};
export interface TokenRegistryMockParameters extends ValidContractMockParameters {
ownerOfValue?: string | Error;
address?: string;
titleEscrowFactoryAddress?: string;
}
interface MockTokenRegistryInterface {
ownerOf: jest.Mock;
genesis: jest.Mock;
titleEscrowFactory: jest.Mock;
supportInterfaces: jest.Mock;
callStatic: {
ownerOf: jest.Mock;
genesis: jest.Mock;
titleEscrowFactory: jest.Mock;
supportInterfaces: jest.Mock;
};
}
export const getMockTokenRegistry = ({
ownerOfValue = AddressZero,
supportInterfaceValue = true,
address = AddressZero,
titleEscrowFactoryAddress = AddressZero,
}: TokenRegistryMockParameters): MockTokenRegistryInterface => {
const validContract = getMockContract({ supportInterfaceValue });
const ownerOf = mockResolve(ownerOfValue);
const genesis = mockResolve(BigNumber.from(0));
const titleEscrowFactory = mockResolve(titleEscrowFactoryAddress);
const contractFunctions = {
ownerOf,
genesis,
titleEscrowFactory,
};
const mockTokenRegistry = {
...contractFunctions,
address: address,
callStatic: contractFunctions,
};
return mergeMockSmartContract({ base: validContract, override: mockTokenRegistry });
};
export interface TokenRegistryMockParameters extends ValidContractMockParameters {
getAddressValue?: string | Error;
}
export const initMockGetCode = (fn?: jest.Mock): void => {
if (!fn) {
const fn = jest.fn();
fn.mockResolvedValue(`0x`);
}
jest.spyOn(providers.BaseProvider.prototype, "getCode").mockImplementation(fn);
};
export interface WalletMockParameters {
codeValue?: string | Error;
}
export const getValidWalletContract = ({
codeValue = `0x`,
}: WalletMockParameters): { provider: { getCode: jest.Mock } } => {
const getCode = mockResolve(codeValue);
return {
provider: {
getCode,
},
};
};
export interface MergeObjectParameters {
base: any;
override: any;
}
export const mergeMockSmartContract = ({ base, override }: MergeObjectParameters): any => {
override = mergeMockBaseContract(base, override, "functions");
override = mergeMockBaseContract(base, override, "callStatic");
override = mergeMockBaseContract(base, override, "estimateGas");
override = mergeMockBaseContract(base, override, "populateTransaction");
override = mergeMockBaseContract(base, override, "filters");
override = mergeMockBaseContract(base, override, "_runningEvents");
override = mergeMockBaseContract(base, override, "_wrappedEmits");
return {
...base,
...override,
};
};
const mergeMockBaseContract = <key extends keyof BaseContract>(base: any, override: any, keyName: key): any => {
if (keyName in override && keyName in base) {
if (typeof base[keyName] === "object" && typeof override[keyName] === "object") {
override[keyName] = {
...base[keyName],
...override[keyName],
};
}
}
return override;
};