Skip to content

Commit 0f1df5c

Browse files
committed
fix: unit tests
1 parent 135f033 commit 0f1df5c

File tree

2 files changed

+179
-133
lines changed

2 files changed

+179
-133
lines changed

packages/cache/test/defineWalletSetup.test.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { describe, expect, it } from 'vitest'
22
import { defineWalletSetup } from '../src'
33

44
const PASSWORD = 'Quack Quack! 🦆'
5-
const EXPECTED_HASH = '8a6a832d282f38a4683a'
5+
const EXPECTED_HASH = 'f9c5ea5bb2c3aac96ff4'
66

77
const testWalletSetupFunction = async (): Promise<void> => {
88
const result = 1 + 2
Original file line numberDiff line numberDiff line change
@@ -1,201 +1,247 @@
1-
import { fs, vol } from 'memfs'
2-
import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
3-
4-
import path from 'node:path'
5-
import fsExtra from 'fs-extra'
6-
import type { WalletSetupFunction } from '../../src'
7-
import * as EnsureCacheDirExists from '../../src/ensureCacheDirExists'
8-
import * as CreateCacheForWalletSetupFunction from '../../src/utils/createCacheForWalletSetupFunction'
9-
import { triggerCacheCreation } from '../../src/utils/triggerCacheCreation'
10-
11-
const ROOT_DIR = '/tmp'
12-
const EXTENSION_PATH = path.join(ROOT_DIR, 'extension')
13-
14-
vi.mock('fs-extra', async () => {
1+
import { fs, vol } from "memfs";
2+
import {
3+
afterAll,
4+
afterEach,
5+
beforeEach,
6+
describe,
7+
expect,
8+
it,
9+
vi,
10+
} from "vitest";
11+
12+
import path from "node:path";
13+
import fsExtra from "fs-extra";
14+
import type { WalletSetupFunction } from "../../src";
15+
import * as EnsureCacheDirExists from "../../src/ensureCacheDirExists";
16+
import * as CreateCacheForWalletSetupFunction from "../../src/utils/createCacheForWalletSetupFunction";
17+
import { triggerCacheCreation } from "../../src/utils/triggerCacheCreation";
18+
19+
const ROOT_DIR = "/tmp";
20+
const EXTENSION_PATH = path.join(ROOT_DIR, "extension");
21+
22+
vi.mock("fs-extra", async () => {
1523
return {
1624
default: {
1725
...fs.promises,
1826
exists: async (path: string) => {
19-
return vol.existsSync(path)
27+
return vol.existsSync(path);
2028
},
2129
remove: async (path: string) => {
22-
vol.rmdirSync(path)
23-
}
24-
}
25-
}
26-
})
30+
vol.rmdirSync(path);
31+
},
32+
},
33+
};
34+
});
2735

28-
vi.mock('../../src/ensureCacheDirExists', async () => {
36+
vi.mock("../../src/ensureCacheDirExists", async () => {
2937
return {
30-
ensureCacheDirExists: vi.fn(() => '/tmp')
31-
}
32-
})
38+
ensureCacheDirExists: vi.fn(() => "/tmp"),
39+
};
40+
});
3341

34-
vi.mock('../../src/utils/createCacheForWalletSetupFunction', async () => {
42+
vi.mock("../../src/utils/createCacheForWalletSetupFunction", async () => {
3543
return {
3644
createCacheForWalletSetupFunction: vi.fn(async () => {
37-
return 'Resolved Quack! 🦆'
38-
})
39-
}
40-
})
45+
return "Resolved Quack! 🦆";
46+
}),
47+
};
48+
});
4149

4250
// We're not adding a test for code that uses `isDirEmpty` because soon it will be removed.
43-
vi.mock('../../src/utils/isDirEmpty', async () => {
51+
vi.mock("../../src/utils/isDirEmpty", async () => {
4452
return {
4553
isDirEmpty: vi.fn(async () => {
46-
return false
47-
})
48-
}
49-
})
54+
return false;
55+
}),
56+
};
57+
});
5058

51-
describe('triggerCacheCreation', () => {
59+
describe("triggerCacheCreation", () => {
5260
const createCacheForWalletSetupFunctionSpy = vi.spyOn(
5361
CreateCacheForWalletSetupFunction,
54-
'createCacheForWalletSetupFunction'
55-
)
62+
"createCacheForWalletSetupFunction"
63+
);
5664

57-
const downloadExtension = vi.fn(async () => EXTENSION_PATH)
58-
const testSetupFunction = vi.fn()
65+
const downloadExtension = vi.fn(async () => EXTENSION_PATH);
66+
const testSetupFunction = vi.fn();
5967

6068
function prepareSetupFunctions(hashes: string[]) {
61-
const setupFunctions = new Map<string, { fileName: string; setupFunction: WalletSetupFunction }>()
69+
const setupFunctions = new Map<
70+
string,
71+
{ fileName: string; setupFunction: WalletSetupFunction }
72+
>();
6273

6374
for (const hash of hashes) {
6475
setupFunctions.set(hash, {
6576
fileName: path.join(ROOT_DIR, `${hash}.ts`),
66-
setupFunction: testSetupFunction
67-
})
77+
setupFunction: testSetupFunction,
78+
});
6879
}
6980

70-
return setupFunctions
81+
return setupFunctions;
7182
}
7283

7384
function expectCreateCacheForWalletSetupFunction(
7485
n: number,
7586
setupFunctions: ReturnType<typeof prepareSetupFunctions>,
7687
hash: string
7788
) {
78-
const fileNameWithCorrectExtension = setupFunctions.get(hash)?.fileName?.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}')
89+
const fileNameWithCorrectExtension = setupFunctions
90+
.get(hash)
91+
?.fileName?.replace(/\.(ts|js|mjs)$/, ".{ts,js,mjs}");
7992

8093
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenNthCalledWith(
8194
n,
8295
EXTENSION_PATH,
8396
path.join(ROOT_DIR, hash),
8497
testSetupFunction,
8598
fileNameWithCorrectExtension
86-
)
99+
);
87100
}
88101

89102
afterAll(() => {
90-
vi.resetAllMocks()
91-
})
103+
vi.resetAllMocks();
104+
});
92105

93106
beforeEach(() => {
94-
vol.mkdirSync(ROOT_DIR)
95-
})
107+
vol.mkdirSync(ROOT_DIR);
108+
});
96109

97110
afterEach(() => {
98-
vi.clearAllMocks()
99-
vol.reset() // Clear the in-memory file system after each test
100-
})
101-
102-
const setupFunctions = prepareSetupFunctions(['hash1', 'hash2'])
103-
const functionStrings = ['function1', 'function2']
104-
105-
it('calls ensureCacheDirExists', async () => {
106-
const ensureCacheDirExistsSpy = vi.spyOn(EnsureCacheDirExists, 'ensureCacheDirExists')
107-
108-
await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false)
109-
110-
expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce()
111-
})
112-
113-
it('calls passed downloadExtension function', async () => {
114-
const setupFunctions = prepareSetupFunctions(['hash1', 'hash2'])
115-
await triggerCacheCreation(setupFunctions, [], downloadExtension, false)
116-
117-
expect(downloadExtension).toHaveBeenCalledOnce()
118-
})
119-
120-
it.skip('calls createCacheForWalletSetupFunction with correct arguments', async () => {
121-
await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false)
122-
123-
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2)
124-
expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1')
125-
expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2')
126-
})
127-
128-
it.skip('checks if cache already exists for each entry', async () => {
129-
const existsSpy = vi.spyOn(fsExtra, 'exists')
130-
await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false)
131-
132-
expect(existsSpy).toHaveBeenCalledTimes(2)
133-
expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, 'hash1'))
134-
expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, 'hash2'))
135-
})
136-
137-
it('returns an array of createCacheForWalletSetupFunction promises', async () => {
138-
const promises = await triggerCacheCreation(setupFunctions, functionStrings, downloadExtension, false)
139-
140-
console.log(promises)
141-
142-
expect(promises).toHaveLength(2)
143-
expect(promises[0]).toBeInstanceOf(Promise)
144-
expect(promises[1]).toBeInstanceOf(Promise)
145-
})
146-
147-
describe('when force flag is false', () => {
148-
it.skip('ignores setup function for which cache already exists', async () => {
149-
const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3'])
111+
vi.clearAllMocks();
112+
vol.reset(); // Clear the in-memory file system after each test
113+
});
114+
115+
const hashes = ["hash1", "hash2"];
116+
const setupFunctions = prepareSetupFunctions(hashes);
117+
118+
it("calls ensureCacheDirExists", async () => {
119+
const ensureCacheDirExistsSpy = vi.spyOn(
120+
EnsureCacheDirExists,
121+
"ensureCacheDirExists"
122+
);
123+
124+
await triggerCacheCreation(
125+
setupFunctions,
126+
hashes,
127+
downloadExtension,
128+
false
129+
);
130+
131+
expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce();
132+
});
133+
134+
it("calls passed downloadExtension function", async () => {
135+
const setupFunctions = prepareSetupFunctions(hashes);
136+
await triggerCacheCreation(
137+
setupFunctions,
138+
hashes,
139+
downloadExtension,
140+
false
141+
);
142+
143+
expect(downloadExtension).toHaveBeenCalledOnce();
144+
});
145+
146+
it.skip("calls createCacheForWalletSetupFunction with correct arguments", async () => {
147+
await triggerCacheCreation(
148+
setupFunctions,
149+
hashes,
150+
downloadExtension,
151+
false
152+
);
153+
154+
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2);
155+
expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1");
156+
expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2");
157+
});
158+
159+
it.skip("checks if cache already exists for each entry", async () => {
160+
const existsSpy = vi.spyOn(fsExtra, "exists");
161+
await triggerCacheCreation(
162+
setupFunctions,
163+
hashes,
164+
downloadExtension,
165+
false
166+
);
167+
168+
expect(existsSpy).toHaveBeenCalledTimes(2);
169+
expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, "hash1"));
170+
expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, "hash2"));
171+
});
172+
173+
it("returns an array of createCacheForWalletSetupFunction promises", async () => {
174+
const promises = await triggerCacheCreation(
175+
setupFunctions,
176+
hashes,
177+
downloadExtension,
178+
false
179+
);
180+
181+
console.log(promises);
182+
183+
expect(promises).toHaveLength(2);
184+
expect(promises[0]).toBeInstanceOf(Promise);
185+
expect(promises[1]).toBeInstanceOf(Promise);
186+
});
187+
188+
describe("when force flag is false", () => {
189+
it.skip("ignores setup function for which cache already exists", async () => {
190+
const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]);
150191

151192
// Creating cache for 2nd setup function.
152-
fs.mkdirSync(path.join(ROOT_DIR, 'hash2'))
193+
fs.mkdirSync(path.join(ROOT_DIR, "hash2"));
153194

154195
const promises = await triggerCacheCreation(
155196
setupFunctions,
156-
[...functionStrings, 'function3'],
197+
[...hashes, "hash3"],
157198
downloadExtension,
158199
false
159-
)
200+
);
160201

161-
expect(promises).toHaveLength(2)
162-
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2)
163-
expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1')
164-
expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash3')
165-
})
166-
})
202+
expect(promises).toHaveLength(2);
203+
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2);
204+
expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1");
205+
expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash3");
206+
});
207+
});
167208

168-
describe('when force flag is true', () => {
169-
it.skip('removes cache if it already exists for given setup function', async () => {
170-
const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3'])
209+
describe("when force flag is true", () => {
210+
it.skip("removes cache if it already exists for given setup function", async () => {
211+
const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]);
171212

172213
// Creating cache for 2nd setup function.
173-
const pathToExistingCache = path.join(ROOT_DIR, 'hash2')
174-
fs.mkdirSync(pathToExistingCache)
214+
const pathToExistingCache = path.join(ROOT_DIR, "hash2");
215+
fs.mkdirSync(pathToExistingCache);
175216

176-
await triggerCacheCreation(setupFunctions, [...functionStrings, 'function3'], downloadExtension, true)
217+
await triggerCacheCreation(
218+
setupFunctions,
219+
[...hashes, "hash3"],
220+
downloadExtension,
221+
true
222+
);
177223

178-
expect(fs.existsSync(pathToExistingCache)).toBe(false)
179-
})
224+
expect(fs.existsSync(pathToExistingCache)).toBe(false);
225+
});
180226

181-
it.skip('calls createCacheForWalletSetupFunction for setup functions that were previously cached', async () => {
182-
const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3'])
227+
it.skip("calls createCacheForWalletSetupFunction for setup functions that were previously cached", async () => {
228+
const setupFunctions = prepareSetupFunctions([...hashes, "hash3"]);
183229

184230
// Creating cache for 2nd setup function.
185-
fs.mkdirSync(path.join(ROOT_DIR, 'hash2'))
231+
fs.mkdirSync(path.join(ROOT_DIR, "hash2"));
186232

187233
const promises = await triggerCacheCreation(
188234
setupFunctions,
189-
[...functionStrings, 'function3'],
235+
[...hashes, "hash3"],
190236
downloadExtension,
191237
true
192-
)
193-
194-
expect(promises).toHaveLength(3)
195-
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3)
196-
expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1')
197-
expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2')
198-
expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3')
199-
})
200-
})
201-
})
238+
);
239+
240+
expect(promises).toHaveLength(3);
241+
expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3);
242+
expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1");
243+
expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2");
244+
expectCreateCacheForWalletSetupFunction(3, setupFunctions, "hash3");
245+
});
246+
});
247+
});

0 commit comments

Comments
 (0)