@reown/appkit-core 0.0.4 → 1.0.0
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/LICENSE +2 -2
- package/dist/esm/exports/react.js +11 -9
- package/dist/esm/exports/react.js.map +1 -1
- package/dist/esm/src/controllers/AccountController.js +10 -6
- package/dist/esm/src/controllers/AccountController.js.map +1 -1
- package/dist/esm/src/controllers/ApiController.js +7 -6
- package/dist/esm/src/controllers/ApiController.js.map +1 -1
- package/dist/esm/src/controllers/BlockchainApiController.js +9 -7
- package/dist/esm/src/controllers/BlockchainApiController.js.map +1 -1
- package/dist/esm/src/controllers/ChainController.js +34 -17
- package/dist/esm/src/controllers/ChainController.js.map +1 -1
- package/dist/esm/src/controllers/ConnectionController.js +6 -5
- package/dist/esm/src/controllers/ConnectionController.js.map +1 -1
- package/dist/esm/src/controllers/ConnectorController.js +59 -22
- package/dist/esm/src/controllers/ConnectorController.js.map +1 -1
- package/dist/esm/src/controllers/EnsController.js +9 -10
- package/dist/esm/src/controllers/EnsController.js.map +1 -1
- package/dist/esm/src/controllers/EventsController.js +2 -2
- package/dist/esm/src/controllers/EventsController.js.map +1 -1
- package/dist/esm/src/controllers/ModalController.js +4 -5
- package/dist/esm/src/controllers/ModalController.js.map +1 -1
- package/dist/esm/src/controllers/NetworkController.js +8 -17
- package/dist/esm/src/controllers/NetworkController.js.map +1 -1
- package/dist/esm/src/controllers/OptionsController.js +10 -5
- package/dist/esm/src/controllers/OptionsController.js.map +1 -1
- package/dist/esm/src/controllers/SendController.js +5 -6
- package/dist/esm/src/controllers/SendController.js.map +1 -1
- package/dist/esm/src/controllers/SwapController.js +9 -8
- package/dist/esm/src/controllers/SwapController.js.map +1 -1
- package/dist/esm/src/controllers/TransactionsController.js +3 -3
- package/dist/esm/src/controllers/TransactionsController.js.map +1 -1
- package/dist/esm/src/utils/CoreHelperUtil.js +7 -0
- package/dist/esm/src/utils/CoreHelperUtil.js.map +1 -1
- package/dist/esm/src/utils/StorageUtil.js +3 -3
- package/dist/esm/src/utils/StorageUtil.js.map +1 -1
- package/dist/esm/src/utils/SwapApiUtil.js +4 -4
- package/dist/esm/src/utils/SwapApiUtil.js.map +1 -1
- package/dist/esm/tests/controllers/AccountController.test.js +6 -1
- package/dist/esm/tests/controllers/AccountController.test.js.map +1 -1
- package/dist/esm/tests/controllers/ApiController.test.js +6 -1
- package/dist/esm/tests/controllers/ApiController.test.js.map +1 -1
- package/dist/esm/tests/controllers/ChainController.test.js +2 -1
- package/dist/esm/tests/controllers/ChainController.test.js.map +1 -1
- package/dist/esm/tests/controllers/ConnectionController.test.js +12 -3
- package/dist/esm/tests/controllers/ConnectionController.test.js.map +1 -1
- package/dist/esm/tests/controllers/ConnectorController.test.js +58 -10
- package/dist/esm/tests/controllers/ConnectorController.test.js.map +1 -1
- package/dist/esm/tests/controllers/EnsController.test.js +10 -10
- package/dist/esm/tests/controllers/EnsController.test.js.map +1 -1
- package/dist/esm/tests/controllers/NetworkController.test.js +27 -18
- package/dist/esm/tests/controllers/NetworkController.test.js.map +1 -1
- package/dist/esm/tests/controllers/OptionsController.test.js +1 -1
- package/dist/esm/tests/controllers/OptionsController.test.js.map +1 -1
- package/dist/esm/tests/controllers/SwapController.test.js +6 -2
- package/dist/esm/tests/controllers/SwapController.test.js.map +1 -1
- package/dist/esm/tests/controllers/TransactionsController.test.js +3 -3
- package/dist/esm/tests/controllers/TransactionsController.test.js.map +1 -1
- package/dist/esm/tests/hooks/react.test.js +68 -0
- package/dist/esm/tests/hooks/react.test.js.map +1 -0
- package/dist/esm/tests/utils/EnsUtil.test.js +23 -0
- package/dist/esm/tests/utils/EnsUtil.test.js.map +1 -0
- package/dist/esm/tests/utils/FetchUtil.test.js +175 -0
- package/dist/esm/tests/utils/FetchUtil.test.js.map +1 -0
- package/dist/esm/tests/utils/OptionsUtil.test.js +20 -0
- package/dist/esm/tests/utils/OptionsUtil.test.js.map +1 -0
- package/dist/esm/tests/utils/RouterUtil.test.js +62 -0
- package/dist/esm/tests/utils/RouterUtil.test.js.map +1 -0
- package/dist/esm/tests/utils/StorageUtil.test.js +145 -0
- package/dist/esm/tests/utils/StorageUtil.test.js.map +1 -0
- package/dist/esm/tests/utils/SwapApiUtil.test.js +196 -0
- package/dist/esm/tests/utils/SwapApiUtil.test.js.map +1 -0
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/types/exports/index.d.ts +1 -1
- package/dist/types/exports/react.d.ts +2 -2
- package/dist/types/src/controllers/AccountController.d.ts +1 -0
- package/dist/types/src/controllers/ApiController.d.ts +1 -1
- package/dist/types/src/controllers/ChainController.d.ts +4 -3
- package/dist/types/src/controllers/ConnectionController.d.ts +1 -1
- package/dist/types/src/controllers/ConnectorController.d.ts +2 -2
- package/dist/types/src/controllers/EnsController.d.ts +3 -2
- package/dist/types/src/controllers/EventsController.d.ts +1 -1
- package/dist/types/src/controllers/OptionsController.d.ts +95 -8
- package/dist/types/src/utils/CoreHelperUtil.d.ts +5 -2
- package/dist/types/src/utils/StorageUtil.d.ts +1 -1
- package/dist/types/src/utils/TypeUtil.d.ts +7 -5
- package/dist/types/tests/hooks/react.test.d.ts +1 -0
- package/dist/types/tests/utils/EnsUtil.test.d.ts +1 -0
- package/dist/types/tests/utils/FetchUtil.test.d.ts +1 -0
- package/dist/types/tests/utils/OptionsUtil.test.d.ts +1 -0
- package/dist/types/tests/utils/RouterUtil.test.d.ts +1 -0
- package/dist/types/tests/utils/StorageUtil.test.d.ts +1 -0
- package/dist/types/tests/utils/SwapApiUtil.test.d.ts +1 -0
- package/package.json +7 -7
- package/readme.md +4 -4
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { FetchUtil } from '../../src/utils/FetchUtil';
|
|
3
|
+
describe('FetchUtil', () => {
|
|
4
|
+
const mockBaseUrl = 'https://api.example.com';
|
|
5
|
+
const mockClientId = 'test-client-id';
|
|
6
|
+
let fetchUtil;
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
fetchUtil = new FetchUtil({ baseUrl: mockBaseUrl, clientId: mockClientId });
|
|
9
|
+
vi.stubGlobal('fetch', vi.fn());
|
|
10
|
+
});
|
|
11
|
+
afterEach(() => {
|
|
12
|
+
vi.unstubAllGlobals();
|
|
13
|
+
});
|
|
14
|
+
describe('constructor', () => {
|
|
15
|
+
it('should set baseUrl and clientId', () => {
|
|
16
|
+
expect(fetchUtil.baseUrl).toBe(mockBaseUrl);
|
|
17
|
+
expect(fetchUtil.clientId).toBe(mockClientId);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
describe('get', () => {
|
|
21
|
+
it('should make a GET request with correct URL and parameters', async () => {
|
|
22
|
+
const mockResponse = {
|
|
23
|
+
ok: true,
|
|
24
|
+
status: 200,
|
|
25
|
+
json: vi.fn().mockResolvedValue({ data: 'test' })
|
|
26
|
+
};
|
|
27
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
28
|
+
const result = await fetchUtil.get({
|
|
29
|
+
path: '/test',
|
|
30
|
+
params: { key: 'value' }
|
|
31
|
+
});
|
|
32
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
33
|
+
const mockCall = vi.mocked(global.fetch).mock.calls[0];
|
|
34
|
+
const calledUrl = mockCall?.[0];
|
|
35
|
+
const calledOptions = mockCall?.[1];
|
|
36
|
+
expect(calledUrl?.toString()).toBe('https://api.example.com/test?key=value&clientId=test-client-id');
|
|
37
|
+
expect(calledOptions).toEqual({
|
|
38
|
+
method: 'GET',
|
|
39
|
+
headers: undefined,
|
|
40
|
+
signal: undefined,
|
|
41
|
+
cache: undefined
|
|
42
|
+
});
|
|
43
|
+
expect(result).toEqual({ data: 'test' });
|
|
44
|
+
});
|
|
45
|
+
it('should handle errors', async () => {
|
|
46
|
+
vi.mocked(global.fetch).mockRejectedValue(new Error('Network error'));
|
|
47
|
+
await expect(fetchUtil.get({ path: '/test' })).rejects.toThrow('Network error');
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
describe('getBlob', () => {
|
|
51
|
+
it('should make a GET request and return a blob', async () => {
|
|
52
|
+
const mockBlob = new Blob(['test']);
|
|
53
|
+
const mockResponse = {
|
|
54
|
+
ok: true,
|
|
55
|
+
status: 200,
|
|
56
|
+
blob: vi.fn().mockResolvedValue(mockBlob)
|
|
57
|
+
};
|
|
58
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
59
|
+
const result = await fetchUtil.getBlob({ path: '/test' });
|
|
60
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
61
|
+
const mockCall = vi.mocked(global.fetch).mock.calls[0];
|
|
62
|
+
const calledUrl = mockCall?.[0];
|
|
63
|
+
const calledOptions = mockCall?.[1];
|
|
64
|
+
expect(calledUrl?.toString()).toBe('https://api.example.com/test?clientId=test-client-id');
|
|
65
|
+
expect(calledOptions).toEqual({
|
|
66
|
+
method: 'GET',
|
|
67
|
+
headers: undefined,
|
|
68
|
+
signal: undefined
|
|
69
|
+
});
|
|
70
|
+
expect(result).toBe(mockBlob);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
describe('post', () => {
|
|
74
|
+
it('should make a POST request with correct body and headers', async () => {
|
|
75
|
+
const mockResponse = {
|
|
76
|
+
ok: true,
|
|
77
|
+
status: 200,
|
|
78
|
+
json: vi.fn().mockResolvedValue({ data: 'test' })
|
|
79
|
+
};
|
|
80
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
81
|
+
const result = await fetchUtil.post({
|
|
82
|
+
path: '/test',
|
|
83
|
+
body: { key: 'value' },
|
|
84
|
+
headers: { 'Content-Type': 'application/json' }
|
|
85
|
+
});
|
|
86
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
87
|
+
const mockCall = vi.mocked(global.fetch).mock.calls[0];
|
|
88
|
+
const calledUrl = mockCall?.[0];
|
|
89
|
+
const calledOptions = mockCall?.[1];
|
|
90
|
+
expect(calledUrl?.toString()).toBe('https://api.example.com/test?clientId=test-client-id');
|
|
91
|
+
expect(calledOptions).toEqual({
|
|
92
|
+
method: 'POST',
|
|
93
|
+
headers: { 'Content-Type': 'application/json' },
|
|
94
|
+
body: JSON.stringify({ key: 'value' }),
|
|
95
|
+
signal: undefined
|
|
96
|
+
});
|
|
97
|
+
expect(result).toEqual({ data: 'test' });
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
describe('put', () => {
|
|
101
|
+
it('should make a PUT request with correct body and headers', async () => {
|
|
102
|
+
const mockResponse = {
|
|
103
|
+
ok: true,
|
|
104
|
+
status: 200,
|
|
105
|
+
json: vi.fn().mockResolvedValue({ data: 'test' })
|
|
106
|
+
};
|
|
107
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
108
|
+
const result = await fetchUtil.put({
|
|
109
|
+
path: '/test',
|
|
110
|
+
body: { key: 'value' },
|
|
111
|
+
headers: { 'Content-Type': 'application/json' }
|
|
112
|
+
});
|
|
113
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
114
|
+
const mockCall = vi.mocked(global.fetch).mock.calls[0];
|
|
115
|
+
const calledUrl = mockCall?.[0];
|
|
116
|
+
const calledOptions = mockCall?.[1];
|
|
117
|
+
expect(calledUrl?.toString()).toBe('https://api.example.com/test?clientId=test-client-id');
|
|
118
|
+
expect(calledOptions).toEqual({
|
|
119
|
+
method: 'PUT',
|
|
120
|
+
headers: { 'Content-Type': 'application/json' },
|
|
121
|
+
body: JSON.stringify({ key: 'value' }),
|
|
122
|
+
signal: undefined
|
|
123
|
+
});
|
|
124
|
+
expect(result).toEqual({ data: 'test' });
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
describe('delete', () => {
|
|
128
|
+
it('should make a DELETE request with correct body and headers', async () => {
|
|
129
|
+
const mockResponse = {
|
|
130
|
+
ok: true,
|
|
131
|
+
status: 200,
|
|
132
|
+
json: vi.fn().mockResolvedValue({ data: 'test' })
|
|
133
|
+
};
|
|
134
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
135
|
+
const result = await fetchUtil.delete({
|
|
136
|
+
path: '/test',
|
|
137
|
+
body: { key: 'value' },
|
|
138
|
+
headers: { 'Content-Type': 'application/json' }
|
|
139
|
+
});
|
|
140
|
+
expect(global.fetch).toHaveBeenCalledTimes(1);
|
|
141
|
+
const mockCall = vi.mocked(global.fetch).mock.calls[0];
|
|
142
|
+
const calledUrl = mockCall?.[0];
|
|
143
|
+
const calledOptions = mockCall?.[1];
|
|
144
|
+
expect(calledUrl?.toString()).toBe('https://api.example.com/test?clientId=test-client-id');
|
|
145
|
+
expect(calledOptions).toEqual({
|
|
146
|
+
method: 'DELETE',
|
|
147
|
+
headers: { 'Content-Type': 'application/json' },
|
|
148
|
+
body: JSON.stringify({ key: 'value' }),
|
|
149
|
+
signal: undefined
|
|
150
|
+
});
|
|
151
|
+
expect(result).toEqual({ data: 'test' });
|
|
152
|
+
});
|
|
153
|
+
});
|
|
154
|
+
describe('createUrl', () => {
|
|
155
|
+
it('should create a URL with correct path and parameters', () => {
|
|
156
|
+
const url = fetchUtil['createUrl']({
|
|
157
|
+
path: '/test',
|
|
158
|
+
params: { key: 'value', empty: undefined }
|
|
159
|
+
});
|
|
160
|
+
expect(url.toString()).toBe('https://api.example.com/test?key=value&clientId=test-client-id');
|
|
161
|
+
});
|
|
162
|
+
it('should handle paths with leading slash', () => {
|
|
163
|
+
const url = fetchUtil['createUrl']({ path: 'test' });
|
|
164
|
+
expect(url.toString()).toBe('https://api.example.com/test?clientId=test-client-id');
|
|
165
|
+
});
|
|
166
|
+
});
|
|
167
|
+
describe('error handling', () => {
|
|
168
|
+
it('should throw an error for non-2xx responses', async () => {
|
|
169
|
+
const mockResponse = { ok: false, status: 404 };
|
|
170
|
+
vi.mocked(global.fetch).mockResolvedValue(mockResponse);
|
|
171
|
+
await expect(fetchUtil.get({ path: '/test' })).rejects.toThrow('HTTP status code: 404');
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
//# sourceMappingURL=FetchUtil.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"FetchUtil.test.js","sourceRoot":"","sources":["../../../../tests/utils/FetchUtil.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACxE,OAAO,EAAE,SAAS,EAAE,MAAM,2BAA2B,CAAA;AAErD,QAAQ,CAAC,WAAW,EAAE,GAAG,EAAE;IACzB,MAAM,WAAW,GAAG,yBAAyB,CAAA;IAC7C,MAAM,YAAY,GAAG,gBAAgB,CAAA;IACrC,IAAI,SAAoB,CAAA;IAExB,UAAU,CAAC,GAAG,EAAE;QACd,SAAS,GAAG,IAAI,SAAS,CAAC,EAAE,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,CAAC,CAAA;QAC3E,EAAE,CAAC,UAAU,CAAC,OAAO,EAAE,EAAE,CAAC,EAAE,EAAE,CAAC,CAAA;IACjC,CAAC,CAAC,CAAA;IAEF,SAAS,CAAC,GAAG,EAAE;QACb,EAAE,CAAC,gBAAgB,EAAE,CAAA;IACvB,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;QAC3B,EAAE,CAAC,iCAAiC,EAAE,GAAG,EAAE;YACzC,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,WAAW,CAAC,CAAA;YAC3C,MAAM,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;QAC/C,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,KAAK,EAAE,GAAG,EAAE;QACnB,EAAE,CAAC,2DAA2D,EAAE,KAAK,IAAI,EAAE;YACzE,MAA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rF,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,gBAAgB,EAAE,GAAG,EAAE;QAC9B,EAAE,CAAC,6CAA6C,EAAE,KAAK,IAAI,EAAE;YAC3D,MAAM,YAAY,GAAG,EAAE,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,CAAA;YAC/C,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,iBAAiB,CAAC,YAAmC,CAAC,CAAA;YAE9E,MAAM,MAAM,CAAC,SAAS,CAAC,GAAG,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAA;QACzF,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { OptionsUtil } from '../../src/utils/OptionsUtil';
|
|
3
|
+
import { ConstantsUtil } from '../../src/utils/ConstantsUtil';
|
|
4
|
+
describe('OptionsUtil', () => {
|
|
5
|
+
describe('getFeatureValue', () => {
|
|
6
|
+
it('should return the default value when feature is not provided', () => {
|
|
7
|
+
const defaultValue = ConstantsUtil.DEFAULT_FEATURES.swaps;
|
|
8
|
+
const result = OptionsUtil.getFeatureValue('swaps');
|
|
9
|
+
expect(result).toBe(defaultValue);
|
|
10
|
+
});
|
|
11
|
+
it('should handle disabling feature values', () => {
|
|
12
|
+
const features = {
|
|
13
|
+
swaps: false
|
|
14
|
+
};
|
|
15
|
+
const result = OptionsUtil.getFeatureValue('swaps', features);
|
|
16
|
+
expect(result).toBe(false);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
//# sourceMappingURL=OptionsUtil.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"OptionsUtil.test.js","sourceRoot":"","sources":["../../../../tests/utils/OptionsUtil.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAA;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAG7D,QAAQ,CAAC,aAAa,EAAE,GAAG,EAAE;IAC3B,QAAQ,CAAC,iBAAiB,EAAE,GAAG,EAAE;QAC/B,EAAE,CAAC,8DAA8D,EAAE,GAAG,EAAE;YACtE,MAAM,YAAY,GAAG,aAAa,CAAC,gBAAgB,CAAC,KAAK,CAAA;YACzD,MAAM,MAAM,GAAG,WAAW,CAAC,eAAe,CAAC,OAAuB,CAAC,CAAA;YACnE,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAA;QACnC,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,wCAAwC,EAAE,GAAG,EAAE;YAChD,MAAM,QAAQ,GAAa;gBACzB,KAAK,EAAE,KAAK;aACb,CAAA;YACD,MAAM,MAAM,GAAG,WAAW,CAAC,eAAe,CAAC,OAAuB,EAAE,QAAQ,CAAC,CAAA;YAC7E,MAAM,CAAC,MAAM,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAA;QAC5B,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { RouterUtil } from '../../src/utils/RouterUtil';
|
|
3
|
+
import { RouterController } from '../../src/controllers/RouterController';
|
|
4
|
+
import { ModalController } from '../../src/controllers/ModalController';
|
|
5
|
+
// Mock the RouterController and ModalController
|
|
6
|
+
vi.mock('../../src/controllers/RouterController', () => ({
|
|
7
|
+
RouterController: {
|
|
8
|
+
state: {
|
|
9
|
+
history: []
|
|
10
|
+
},
|
|
11
|
+
goBack: vi.fn(),
|
|
12
|
+
goBackToIndex: vi.fn()
|
|
13
|
+
}
|
|
14
|
+
}));
|
|
15
|
+
vi.mock('../../src/controllers/ModalController', () => ({
|
|
16
|
+
ModalController: {
|
|
17
|
+
close: vi.fn()
|
|
18
|
+
}
|
|
19
|
+
}));
|
|
20
|
+
describe('RouterUtil', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.resetAllMocks();
|
|
23
|
+
});
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
vi.restoreAllMocks();
|
|
26
|
+
});
|
|
27
|
+
describe('goBackOrCloseModal', () => {
|
|
28
|
+
it('should call RouterController.goBack when history length is greater than 1', () => {
|
|
29
|
+
RouterController.state.history = ['Account', 'Connect'];
|
|
30
|
+
RouterUtil.goBackOrCloseModal();
|
|
31
|
+
expect(RouterController.goBack).toHaveBeenCalled();
|
|
32
|
+
expect(ModalController.close).not.toHaveBeenCalled();
|
|
33
|
+
});
|
|
34
|
+
it('should call ModalController.close when history length is 1 or less', () => {
|
|
35
|
+
RouterController.state.history = ['Account'];
|
|
36
|
+
RouterUtil.goBackOrCloseModal();
|
|
37
|
+
expect(RouterController.goBack).not.toHaveBeenCalled();
|
|
38
|
+
expect(ModalController.close).toHaveBeenCalled();
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe('navigateAfterNetworkSwitch', () => {
|
|
42
|
+
it('should call RouterController.goBackToIndex when Networks page is in history', () => {
|
|
43
|
+
RouterController.state.history = ['Account', 'Networks', 'Connect'];
|
|
44
|
+
RouterUtil.navigateAfterNetworkSwitch();
|
|
45
|
+
expect(RouterController.goBackToIndex).toHaveBeenCalledWith(0);
|
|
46
|
+
expect(ModalController.close).not.toHaveBeenCalled();
|
|
47
|
+
});
|
|
48
|
+
it('should call ModalController.close when Networks page is not in history', () => {
|
|
49
|
+
RouterController.state.history = ['Account', 'Connect'];
|
|
50
|
+
RouterUtil.navigateAfterNetworkSwitch();
|
|
51
|
+
expect(RouterController.goBackToIndex).not.toHaveBeenCalled();
|
|
52
|
+
expect(ModalController.close).toHaveBeenCalled();
|
|
53
|
+
});
|
|
54
|
+
it('should call ModalController.close when Networks is the first page in history', () => {
|
|
55
|
+
RouterController.state.history = ['Networks', 'Account', 'Connect'];
|
|
56
|
+
RouterUtil.navigateAfterNetworkSwitch();
|
|
57
|
+
expect(RouterController.goBackToIndex).not.toHaveBeenCalled();
|
|
58
|
+
expect(ModalController.close).toHaveBeenCalled();
|
|
59
|
+
});
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
//# sourceMappingURL=RouterUtil.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"RouterUtil.test.js","sourceRoot":"","sources":["../../../../tests/utils/RouterUtil.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACxE,OAAO,EAAE,UAAU,EAAE,MAAM,4BAA4B,CAAA;AACvD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wCAAwC,CAAA;AACzE,OAAO,EAAE,eAAe,EAAE,MAAM,uCAAuC,CAAA;AAEvE,gDAAgD;AAChD,EAAE,CAAC,IAAI,CAAC,wCAAwC,EAAE,GAAG,EAAE,CAAC,CAAC;IACvD,gBAAgB,EAAE;QAChB,KAAK,EAAE;YACL,OAAO,EAAE,EAAE;SACZ;QACD,MAAM,EAAE,EAAE,CAAC,EAAE,EAAE;QACf,aAAa,EAAE,EAAE,CAAC,EAAE,EAAE;KACvB;CACF,CAAC,CAAC,CAAA;AAEH,EAAE,CAAC,IAAI,CAAC,uCAAuC,EAAE,GAAG,EAAE,CAAC,CAAC;IACtD,eAAe,EAAE;QACf,KAAK,EAAE,EAAE,CAAC,EAAE,EAAE;KACf;CACF,CAAC,CAAC,CAAA;AAEH,QAAQ,CAAC,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CAAC,CAAC,CAAA;YAC9D,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;QACtD,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,wEAAwE,EAAE,GAAG,EAAE;YAChF,gBAAgB,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;YACvD,UAAU,CAAC,0BAA0B,EAAE,CAAA;YACvC,MAAM,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;YAC7D,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,gBAAgB,EAAE,CAAA;QAClD,CAAC,CAAC,CAAA;QAEF,EAAE,CAAC,8EAA8E,EAAE,GAAG,EAAE;YACtF,gBAAgB,CAAC,KAAK,CAAC,OAAO,GAAG,CAAC,UAAU,EAAE,SAAS,EAAE,SAAS,CAAC,CAAA;YACnE,UAAU,CAAC,0BAA0B,EAAE,CAAA;YACvC,MAAM,CAAC,gBAAgB,CAAC,aAAa,CAAC,CAAC,GAAG,CAAC,gBAAgB,EAAE,CAAA;YAC7D,MAAM,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,gBAAgB,EAAE,CAAA;QAClD,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { StorageUtil } from '../../src/utils/StorageUtil';
|
|
3
|
+
// Mock localStorage
|
|
4
|
+
const localStorageMock = (() => {
|
|
5
|
+
let store = {};
|
|
6
|
+
return {
|
|
7
|
+
getItem: (key) => store[key] || null,
|
|
8
|
+
setItem: (key, value) => {
|
|
9
|
+
store[key] = value.toString();
|
|
10
|
+
},
|
|
11
|
+
removeItem: (key) => {
|
|
12
|
+
delete store[key];
|
|
13
|
+
},
|
|
14
|
+
clear: () => {
|
|
15
|
+
store = {};
|
|
16
|
+
}
|
|
17
|
+
};
|
|
18
|
+
})();
|
|
19
|
+
Object.defineProperty(global, 'localStorage', { value: localStorageMock });
|
|
20
|
+
describe('StorageUtil', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
// Clear localStorage before each test
|
|
23
|
+
localStorage.clear();
|
|
24
|
+
});
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
// Restore all mocks after each test
|
|
27
|
+
vi.restoreAllMocks();
|
|
28
|
+
});
|
|
29
|
+
describe('setWalletConnectDeepLink', () => {
|
|
30
|
+
it('should set WalletConnect deep link in localStorage', () => {
|
|
31
|
+
const deepLink = { href: 'https://example.com', name: 'Example Wallet' };
|
|
32
|
+
StorageUtil.setWalletConnectDeepLink(deepLink);
|
|
33
|
+
expect(localStorage.getItem('WALLETCONNECT_DEEPLINK_CHOICE')).toBe(JSON.stringify(deepLink));
|
|
34
|
+
});
|
|
35
|
+
it('should handle errors when setting deep link', () => {
|
|
36
|
+
const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => { });
|
|
37
|
+
vi.spyOn(localStorage, 'setItem').mockImplementation(() => {
|
|
38
|
+
throw new Error('Storage error');
|
|
39
|
+
});
|
|
40
|
+
StorageUtil.setWalletConnectDeepLink({ href: 'https://example.com', name: 'Example Wallet' });
|
|
41
|
+
expect(consoleSpy).toHaveBeenCalledWith('Unable to set WalletConnect deep link');
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
describe('getWalletConnectDeepLink', () => {
|
|
45
|
+
it('should get WalletConnect deep link from localStorage', () => {
|
|
46
|
+
const deepLink = { href: 'https://example.com', name: 'Example Wallet' };
|
|
47
|
+
localStorage.setItem('WALLETCONNECT_DEEPLINK_CHOICE', JSON.stringify(deepLink));
|
|
48
|
+
expect(StorageUtil.getWalletConnectDeepLink()).toEqual(deepLink);
|
|
49
|
+
});
|
|
50
|
+
it('should return undefined if deep link is not set', () => {
|
|
51
|
+
expect(StorageUtil.getWalletConnectDeepLink()).toBeUndefined();
|
|
52
|
+
});
|
|
53
|
+
it('should handle errors when getting deep link', () => {
|
|
54
|
+
const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => { });
|
|
55
|
+
vi.spyOn(localStorage, 'getItem').mockImplementation(() => {
|
|
56
|
+
throw new Error('Storage error');
|
|
57
|
+
});
|
|
58
|
+
expect(StorageUtil.getWalletConnectDeepLink()).toBeUndefined();
|
|
59
|
+
expect(consoleSpy).toHaveBeenCalledWith('Unable to get WalletConnect deep link');
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
describe('deleteWalletConnectDeepLink', () => {
|
|
63
|
+
it('should delete WalletConnect deep link from localStorage', () => {
|
|
64
|
+
localStorage.setItem('WALLETCONNECT_DEEPLINK_CHOICE', JSON.stringify({ href: 'https://example.com', name: 'Example Wallet' }));
|
|
65
|
+
StorageUtil.deleteWalletConnectDeepLink();
|
|
66
|
+
expect(localStorage.getItem('WALLETCONNECT_DEEPLINK_CHOICE')).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
it('should handle errors when deleting deep link', () => {
|
|
69
|
+
const consoleSpy = vi.spyOn(console, 'info').mockImplementation(() => { });
|
|
70
|
+
vi.spyOn(localStorage, 'removeItem').mockImplementation(() => {
|
|
71
|
+
throw new Error('Storage error');
|
|
72
|
+
});
|
|
73
|
+
StorageUtil.deleteWalletConnectDeepLink();
|
|
74
|
+
expect(consoleSpy).toHaveBeenCalledWith('Unable to delete WalletConnect deep link');
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe('setAppKitRecent', () => {
|
|
78
|
+
it('should add a new wallet to recent wallets', () => {
|
|
79
|
+
const wallet = { id: 'wallet1', name: 'Wallet 1' };
|
|
80
|
+
StorageUtil.setAppKitRecent(wallet);
|
|
81
|
+
expect(StorageUtil.getRecentWallets()).toEqual([wallet]);
|
|
82
|
+
});
|
|
83
|
+
it('should not add duplicate wallets', () => {
|
|
84
|
+
const wallet = { id: 'wallet1', name: 'Wallet 1' };
|
|
85
|
+
StorageUtil.setAppKitRecent(wallet);
|
|
86
|
+
StorageUtil.setAppKitRecent(wallet);
|
|
87
|
+
expect(StorageUtil.getRecentWallets()).toEqual([wallet]);
|
|
88
|
+
});
|
|
89
|
+
it('should limit recent wallets to 2', () => {
|
|
90
|
+
const wallet1 = { id: 'wallet1', name: 'Wallet 1' };
|
|
91
|
+
const wallet2 = { id: 'wallet2', name: 'Wallet 2' };
|
|
92
|
+
const wallet3 = { id: 'wallet3', name: 'Wallet 3' };
|
|
93
|
+
StorageUtil.setAppKitRecent(wallet1);
|
|
94
|
+
StorageUtil.setAppKitRecent(wallet2);
|
|
95
|
+
StorageUtil.setAppKitRecent(wallet3);
|
|
96
|
+
expect(StorageUtil.getRecentWallets()).toEqual([wallet3, wallet2]);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
describe('getRecentWallets', () => {
|
|
100
|
+
it('should return an empty array if no recent wallets', () => {
|
|
101
|
+
expect(StorageUtil.getRecentWallets()).toEqual([]);
|
|
102
|
+
});
|
|
103
|
+
it('should return recent wallets', () => {
|
|
104
|
+
const wallet = { id: 'wallet1', name: 'Wallet 1' };
|
|
105
|
+
localStorage.setItem('@w3m/recent', JSON.stringify([wallet]));
|
|
106
|
+
expect(StorageUtil.getRecentWallets()).toEqual([wallet]);
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
describe('setConnectedConnector', () => {
|
|
110
|
+
it('should set connected connector', () => {
|
|
111
|
+
const connector = 'INJECTED';
|
|
112
|
+
StorageUtil.setConnectedConnector(connector);
|
|
113
|
+
expect(localStorage.getItem('@w3m/connected_connector')).toBe(connector);
|
|
114
|
+
});
|
|
115
|
+
});
|
|
116
|
+
describe('getConnectedConnector', () => {
|
|
117
|
+
it('should get connected connector', () => {
|
|
118
|
+
const connector = 'INJECTED';
|
|
119
|
+
localStorage.setItem('@w3m/connected_connector', connector);
|
|
120
|
+
expect(StorageUtil.getConnectedConnector()).toBe(connector);
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
describe('setConnectedSocialProvider', () => {
|
|
124
|
+
it('should set connected social provider', () => {
|
|
125
|
+
const provider = 'google';
|
|
126
|
+
StorageUtil.setConnectedSocialProvider(provider);
|
|
127
|
+
expect(localStorage.getItem('@w3m/connected_social')).toBe(provider);
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
describe('getConnectedSocialProvider', () => {
|
|
131
|
+
it('should get connected social provider', () => {
|
|
132
|
+
const provider = 'google';
|
|
133
|
+
localStorage.setItem('@w3m/connected_social', provider);
|
|
134
|
+
expect(StorageUtil.getConnectedSocialProvider()).toBe(provider);
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
describe('getConnectedSocialUsername', () => {
|
|
138
|
+
it('should get connected social username', () => {
|
|
139
|
+
const username = 'testuser';
|
|
140
|
+
localStorage.setItem('@w3m-storage/SOCIAL_USERNAME', username);
|
|
141
|
+
expect(StorageUtil.getConnectedSocialUsername()).toBe(username);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
//# sourceMappingURL=StorageUtil.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"StorageUtil.test.js","sourceRoot":"","sources":["../../../../tests/utils/StorageUtil.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,MAAM,EAAE,EAAE,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,QAAQ,CAAA;AACxE,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AAGzD,oBAAoB;AACpB,MAAM,gBAAgB,GAAG,CAAC,GAAG,EAAE;IAC7B,IAAI,KAAK,GAA8B,EAAE,CAAA;IACzC,OAAO;QACL,OAAO,EAAE,CAAC,GAAW,EAAE,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,IAAI,IAAI;QAC5C,OAAO,EAAE,CAAC,GAAW,EAAE,KA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,EAAE,GAAG,EAAE;YAC9C,MAAM,QAAQ,GAAmB,QAAQ,CAAA;YACzC,WAAW,CAAC,0BAA0B,CAAC,QAAQ,CAAC,CAAA;YAChD,MAAM,CAAC,YAAY,CAAC,OAAO,CAAC,uBAAuB,CAAC,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACtE,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,4BAA4B,EAAE,GAAG,EAAE;QAC1C,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;YAC9C,MAAM,QAAQ,GAAmB,QAAQ,CAAA;YACzC,YAAY,CAAC,OAAO,CAAC,uBAAuB,EAAE,QAAQ,CAAC,CAAA;YACvD,MAAM,CAAC,WAAW,CAAC,0BAA0B,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACjE,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;IAEF,QAAQ,CAAC,4BAA4B,EAAE,GAAG,EAAE;QAC1C,EAAE,CAAC,sCAAsC,EAAE,GAAG,EAAE;YAC9C,MAAM,QAAQ,GAAG,UAAU,CAAA;YAC3B,YAAY,CAAC,OAAO,CAAC,8BAA8B,EAAE,QAAQ,CAAC,CAAA;YAC9D,MAAM,CAAC,WAAW,CAAC,0BAA0B,EAAE,CAAC,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAA;QACjE,CAAC,CAAC,CAAA;IACJ,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import { SwapApiUtil } from '../../src/utils/SwapApiUtil';
|
|
3
|
+
import { ChainController } from '../../src/controllers/ChainController';
|
|
4
|
+
import { BlockchainApiController } from '../../src/controllers/BlockchainApiController';
|
|
5
|
+
import { OptionsController } from '../../src/controllers/OptionsController';
|
|
6
|
+
import { ConnectionController } from '../../src/controllers/ConnectionController';
|
|
7
|
+
import { AccountController } from '../../src/controllers/AccountController';
|
|
8
|
+
import { NetworkController } from '../../src/controllers/NetworkController';
|
|
9
|
+
// Mock the controllers
|
|
10
|
+
vi.mock('../../src/controllers/ChainController');
|
|
11
|
+
vi.mock('../../src/controllers/BlockchainApiController');
|
|
12
|
+
vi.mock('../../src/controllers/OptionsController');
|
|
13
|
+
vi.mock('../../src/controllers/ConnectionController');
|
|
14
|
+
vi.mock('../../src/controllers/AccountController');
|
|
15
|
+
vi.mock('../../src/controllers/NetworkController');
|
|
16
|
+
const mockSolanaNetwork = {
|
|
17
|
+
id: 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
18
|
+
chainNamespace: 'solana',
|
|
19
|
+
chainId: '5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp',
|
|
20
|
+
name: 'Solana',
|
|
21
|
+
explorerUrl: 'https://explorer.solana.com',
|
|
22
|
+
rpcUrl: 'https://api.mainnet-beta.solana.com',
|
|
23
|
+
currency: 'SOL'
|
|
24
|
+
};
|
|
25
|
+
const mockEthereumNetwork = {
|
|
26
|
+
id: 'eip155:1',
|
|
27
|
+
chainNamespace: 'eip155',
|
|
28
|
+
chainId: '1',
|
|
29
|
+
name: 'Ethereum',
|
|
30
|
+
explorerUrl: 'https://etherscan.io',
|
|
31
|
+
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID',
|
|
32
|
+
currency: 'ETH'
|
|
33
|
+
};
|
|
34
|
+
describe('SwapApiUtil', () => {
|
|
35
|
+
beforeEach(() => {
|
|
36
|
+
vi.resetAllMocks();
|
|
37
|
+
});
|
|
38
|
+
afterEach(() => {
|
|
39
|
+
vi.restoreAllMocks();
|
|
40
|
+
});
|
|
41
|
+
describe('getTokenList', () => {
|
|
42
|
+
it('should return a list of tokens', async () => {
|
|
43
|
+
const mockTokens = [{ address: '0x123', symbol: 'TEST', name: 'Test Token', decimals: 18 }];
|
|
44
|
+
ChainController.state.activeCaipNetwork = mockEthereumNetwork;
|
|
45
|
+
OptionsController.state.projectId = 'test-project-id';
|
|
46
|
+
BlockchainApiController.fetchSwapTokens = vi.fn().mockResolvedValue({ tokens: mockTokens });
|
|
47
|
+
const result = await SwapApiUtil.getTokenList();
|
|
48
|
+
expect(BlockchainApiController.fetchSwapTokens).toHaveBeenCalledWith({
|
|
49
|
+
chainId: 'eip155:1',
|
|
50
|
+
projectId: 'test-project-id'
|
|
51
|
+
});
|
|
52
|
+
expect(result).toEqual([
|
|
53
|
+
{
|
|
54
|
+
...mockTokens[0],
|
|
55
|
+
eip2612: false,
|
|
56
|
+
quantity: { decimals: '0', numeric: '0' },
|
|
57
|
+
price: 0,
|
|
58
|
+
value: 0
|
|
59
|
+
}
|
|
60
|
+
]);
|
|
61
|
+
});
|
|
62
|
+
it('should return an empty array if no tokens are fetched', async () => {
|
|
63
|
+
BlockchainApiController.fetchSwapTokens = vi.fn().mockResolvedValue({ tokens: null });
|
|
64
|
+
const result = await SwapApiUtil.getTokenList();
|
|
65
|
+
expect(result).toEqual([]);
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
describe('fetchGasPrice', () => {
|
|
69
|
+
it('should fetch gas price for EVM chain', async () => {
|
|
70
|
+
ChainController.state.activeCaipNetwork = mockEthereumNetwork;
|
|
71
|
+
OptionsController.state.projectId = 'test-project-id';
|
|
72
|
+
BlockchainApiController.fetchGasPrice = vi
|
|
73
|
+
.fn()
|
|
74
|
+
.mockResolvedValue({ standard: '20', fast: '25', instant: '30' });
|
|
75
|
+
const result = await SwapApiUtil.fetchGasPrice();
|
|
76
|
+
expect(BlockchainApiController.fetchGasPrice).toHaveBeenCalledWith({
|
|
77
|
+
projectId: 'test-project-id',
|
|
78
|
+
chainId: 'eip155:1'
|
|
79
|
+
});
|
|
80
|
+
expect(result).toEqual({ standard: '20', fast: '25', instant: '30' });
|
|
81
|
+
});
|
|
82
|
+
it('should fetch gas price for Solana chain', async () => {
|
|
83
|
+
ChainController.state.activeCaipNetwork = mockSolanaNetwork;
|
|
84
|
+
ConnectionController.estimateGas = vi.fn().mockResolvedValue(5000);
|
|
85
|
+
const result = await SwapApiUtil.fetchGasPrice();
|
|
86
|
+
expect(ConnectionController.estimateGas).toHaveBeenCalledWith({ chainNamespace: 'solana' });
|
|
87
|
+
expect(result).toEqual({ standard: '5000', fast: '5000', instant: '5000' });
|
|
88
|
+
});
|
|
89
|
+
it('should return null if there is an error', async () => {
|
|
90
|
+
ChainController.state.activeCaipNetwork = {
|
|
91
|
+
id: 'eip155:1',
|
|
92
|
+
chainNamespace: 'eip155',
|
|
93
|
+
chainId: '1',
|
|
94
|
+
name: 'Ethereum',
|
|
95
|
+
currency: 'ETH',
|
|
96
|
+
explorerUrl: 'https://etherscan.io',
|
|
97
|
+
rpcUrl: 'https://mainnet.infura.io/v3/YOUR-PROJECT-ID'
|
|
98
|
+
};
|
|
99
|
+
BlockchainApiController.fetchGasPrice = vi.fn().mockRejectedValue(new Error('API Error'));
|
|
100
|
+
const result = await SwapApiUtil.fetchGasPrice();
|
|
101
|
+
expect(result).toBeNull();
|
|
102
|
+
});
|
|
103
|
+
});
|
|
104
|
+
describe('fetchSwapAllowance', () => {
|
|
105
|
+
it('should fetch and return swap allowance', async () => {
|
|
106
|
+
OptionsController.state.projectId = 'test-project-id';
|
|
107
|
+
BlockchainApiController.fetchSwapAllowance = vi
|
|
108
|
+
.fn()
|
|
109
|
+
.mockResolvedValue({ allowance: '1000000000000000000' });
|
|
110
|
+
ConnectionController.parseUnits = vi.fn().mockReturnValue(BigInt('500000000000000000'));
|
|
111
|
+
const result = await SwapApiUtil.fetchSwapAllowance({
|
|
112
|
+
tokenAddress: '0x123',
|
|
113
|
+
userAddress: '0x456',
|
|
114
|
+
sourceTokenAmount: '0.5',
|
|
115
|
+
sourceTokenDecimals: 18
|
|
116
|
+
});
|
|
117
|
+
expect(BlockchainApiController.fetchSwapAllowance).toHaveBeenCalledWith({
|
|
118
|
+
projectId: 'test-project-id',
|
|
119
|
+
tokenAddress: '0x123',
|
|
120
|
+
userAddress: '0x456'
|
|
121
|
+
});
|
|
122
|
+
expect(result).toBe(true);
|
|
123
|
+
});
|
|
124
|
+
it('should return false if allowance is less than source token amount', async () => {
|
|
125
|
+
BlockchainApiController.fetchSwapAllowance = vi
|
|
126
|
+
.fn()
|
|
127
|
+
.mockResolvedValue({ allowance: '100000000000000000' });
|
|
128
|
+
ConnectionController.parseUnits = vi.fn().mockReturnValue(BigInt('500000000000000000'));
|
|
129
|
+
const result = await SwapApiUtil.fetchSwapAllowance({
|
|
130
|
+
tokenAddress: '0x123',
|
|
131
|
+
userAddress: '0x456',
|
|
132
|
+
sourceTokenAmount: '0.5',
|
|
133
|
+
sourceTokenDecimals: 18
|
|
134
|
+
});
|
|
135
|
+
expect(result).toBe(false);
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
describe('getMyTokensWithBalance', () => {
|
|
139
|
+
it('should fetch and return tokens with balance', async () => {
|
|
140
|
+
AccountController.state.address = '0x123';
|
|
141
|
+
ChainController.state.activeCaipNetwork = mockEthereumNetwork;
|
|
142
|
+
BlockchainApiController.getBalance = vi.fn().mockResolvedValue({
|
|
143
|
+
balances: [{ address: '0x456', quantity: { decimals: '18', numeric: '1.5' } }]
|
|
144
|
+
});
|
|
145
|
+
NetworkController.getActiveNetworkTokenAddress = vi.fn().mockReturnValue('0x789');
|
|
146
|
+
const result = await SwapApiUtil.getMyTokensWithBalance();
|
|
147
|
+
expect(BlockchainApiController.getBalance).toHaveBeenCalledWith('0x123', 'eip155:1', undefined);
|
|
148
|
+
expect(AccountController.setTokenBalance).toHaveBeenCalled();
|
|
149
|
+
expect(result).toEqual([
|
|
150
|
+
{
|
|
151
|
+
address: '0x456',
|
|
152
|
+
quantity: { decimals: '18', numeric: '1.5' },
|
|
153
|
+
decimals: 18,
|
|
154
|
+
logoUri: undefined,
|
|
155
|
+
eip2612: false
|
|
156
|
+
}
|
|
157
|
+
]);
|
|
158
|
+
});
|
|
159
|
+
it('should return an empty array if no address or active network', async () => {
|
|
160
|
+
AccountController.state.address = undefined;
|
|
161
|
+
ChainController.state.activeCaipNetwork = undefined;
|
|
162
|
+
const result = await SwapApiUtil.getMyTokensWithBalance();
|
|
163
|
+
expect(result).toEqual([]);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
describe('mapBalancesToSwapTokens', () => {
|
|
167
|
+
it('should map balances to swap tokens', () => {
|
|
168
|
+
const balances = [
|
|
169
|
+
{
|
|
170
|
+
address: '0x123',
|
|
171
|
+
quantity: { decimals: '18', numeric: '1.5' },
|
|
172
|
+
iconUrl: 'https://example.com/icon.png'
|
|
173
|
+
}
|
|
174
|
+
];
|
|
175
|
+
NetworkController.getActiveNetworkTokenAddress = vi.fn().mockReturnValue('0x789');
|
|
176
|
+
const result = SwapApiUtil.mapBalancesToSwapTokens(balances);
|
|
177
|
+
expect(result).toEqual([
|
|
178
|
+
{
|
|
179
|
+
address: '0x123',
|
|
180
|
+
quantity: { decimals: '18', numeric: '1.5' },
|
|
181
|
+
decimals: 18,
|
|
182
|
+
logoUri: 'https://example.com/icon.png',
|
|
183
|
+
eip2612: false,
|
|
184
|
+
iconUrl: 'https://example.com/icon.png'
|
|
185
|
+
}
|
|
186
|
+
]);
|
|
187
|
+
});
|
|
188
|
+
it('should use network token address if balance address is undefined', () => {
|
|
189
|
+
const balances = [{ address: undefined, quantity: { decimals: '18', numeric: '1.5' } }];
|
|
190
|
+
NetworkController.getActiveNetworkTokenAddress = vi.fn().mockReturnValue('0x789');
|
|
191
|
+
const result = SwapApiUtil.mapBalancesToSwapTokens(balances);
|
|
192
|
+
expect(result[0]?.address).toBe('0x789');
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
//# sourceMappingURL=SwapApiUtil.test.js.map
|