From 609f1a525d52f8cc0c7d198fc0d0c0ea9a82598d Mon Sep 17 00:00:00 2001 From: matstyler Date: Tue, 3 Dec 2024 11:19:24 +0100 Subject: [PATCH] fix: format --- .../test/utils/triggerCacheCreation.test.ts | 324 ++++++++---------- 1 file changed, 134 insertions(+), 190 deletions(-) diff --git a/packages/cache/test/utils/triggerCacheCreation.test.ts b/packages/cache/test/utils/triggerCacheCreation.test.ts index ed745c15..ea289b86 100644 --- a/packages/cache/test/utils/triggerCacheCreation.test.ts +++ b/packages/cache/test/utils/triggerCacheCreation.test.ts @@ -1,84 +1,73 @@ -import { fs, vol } from "memfs"; -import { - afterAll, - afterEach, - beforeEach, - describe, - expect, - it, - vi, -} from "vitest"; - -import path from "node:path"; -import fsExtra from "fs-extra"; -import type { WalletSetupFunction } from "../../src"; -import * as EnsureCacheDirExists from "../../src/ensureCacheDirExists"; -import * as CreateCacheForWalletSetupFunction from "../../src/utils/createCacheForWalletSetupFunction"; -import { triggerCacheCreation } from "../../src/utils/triggerCacheCreation"; - -const ROOT_DIR = "/tmp"; -const EXTENSION_PATH = path.join(ROOT_DIR, "extension"); - -vi.mock("fs-extra", async () => { +import { fs, vol } from 'memfs' +import { afterAll, afterEach, beforeEach, describe, expect, it, vi } from 'vitest' + +import path from 'node:path' +import fsExtra from 'fs-extra' +import type { WalletSetupFunction } from '../../src' +import * as EnsureCacheDirExists from '../../src/ensureCacheDirExists' +import * as CreateCacheForWalletSetupFunction from '../../src/utils/createCacheForWalletSetupFunction' +import { triggerCacheCreation } from '../../src/utils/triggerCacheCreation' + +const ROOT_DIR = '/tmp' +const EXTENSION_PATH = path.join(ROOT_DIR, 'extension') + +vi.mock('fs-extra', async () => { return { default: { ...fs.promises, exists: async (path: string) => { - return vol.existsSync(path); + return vol.existsSync(path) }, remove: async (path: string) => { - vol.rmdirSync(path); - }, - }, - }; -}); + vol.rmdirSync(path) + } + } + } +}) -vi.mock("../../src/ensureCacheDirExists", async () => { +vi.mock('../../src/ensureCacheDirExists', async () => { return { - ensureCacheDirExists: vi.fn(() => "/tmp"), - }; -}); + ensureCacheDirExists: vi.fn(() => '/tmp') + } +}) -vi.mock("../../src/utils/createCacheForWalletSetupFunction", async () => { +vi.mock('../../src/utils/createCacheForWalletSetupFunction', async () => { return { createCacheForWalletSetupFunction: vi.fn(async () => { - return "Resolved Quack! 🦆"; - }), - }; -}); + return 'Resolved Quack! 🦆' + }) + } +}) // We're not adding a test for code that uses `isDirEmpty` because soon it will be removed. -vi.mock("../../src/utils/isDirEmpty", async () => { +vi.mock('../../src/utils/isDirEmpty', async () => { return { isDirEmpty: vi.fn(async () => { - return false; - }), - }; -}); + return false + }) + } +}) -describe("triggerCacheCreation", () => { +describe('triggerCacheCreation', () => { const createCacheForWalletSetupFunctionSpy = vi.spyOn( CreateCacheForWalletSetupFunction, - "createCacheForWalletSetupFunction" - ); + 'createCacheForWalletSetupFunction' + ) - const downloadExtension = vi.fn(async () => EXTENSION_PATH); - const testSetupFunction = vi.fn(); + const downloadExtension = vi.fn(async () => EXTENSION_PATH) + const testSetupFunction = vi.fn() function prepareSetupFunctions(hashes: string[]) { - const setupFunctions = new Map< - string, - { fileName: string; setupFunction: WalletSetupFunction } - >(); + const setupFunctions = new Map() for (const hash of hashes) { setupFunctions.set(hash, { fileName: path.join(ROOT_DIR, `${hash}.ts`), - setupFunction: testSetupFunction, - }); + setupFunction: testSetupFunction + }) } - return setupFunctions; + return setupFunctions } function expectCreateCacheForWalletSetupFunction( @@ -86,9 +75,7 @@ describe("triggerCacheCreation", () => { setupFunctions: ReturnType, hash: string ) { - const fileNameWithCorrectExtension = setupFunctions - .get(hash) - ?.fileName?.replace(/\.(ts|js|mjs)$/, ".{ts,js,mjs}"); + const fileNameWithCorrectExtension = setupFunctions.get(hash)?.fileName?.replace(/\.(ts|js|mjs)$/, '.{ts,js,mjs}') expect(createCacheForWalletSetupFunctionSpy).toHaveBeenNthCalledWith( n, @@ -96,152 +83,109 @@ describe("triggerCacheCreation", () => { path.join(ROOT_DIR, hash), testSetupFunction, fileNameWithCorrectExtension - ); + ) } afterAll(() => { - vi.resetAllMocks(); - }); + vi.resetAllMocks() + }) beforeEach(() => { - vol.mkdirSync(ROOT_DIR); - }); + vol.mkdirSync(ROOT_DIR) + }) afterEach(() => { - vi.clearAllMocks(); - vol.reset(); // Clear the in-memory file system after each test - }); - - const hashes = ["hash1", "hash2"]; - const setupFunctions = prepareSetupFunctions(hashes); - - it("calls ensureCacheDirExists", async () => { - const ensureCacheDirExistsSpy = vi.spyOn( - EnsureCacheDirExists, - "ensureCacheDirExists" - ); - - await triggerCacheCreation( - setupFunctions, - hashes, - downloadExtension, - false - ); - - expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce(); - }); - - it("calls passed downloadExtension function", async () => { - const setupFunctions = prepareSetupFunctions(hashes); - await triggerCacheCreation( - setupFunctions, - hashes, - downloadExtension, - false - ); - - expect(downloadExtension).toHaveBeenCalledOnce(); - }); - - it.skip("calls createCacheForWalletSetupFunction with correct arguments", async () => { - await triggerCacheCreation( - setupFunctions, - hashes, - downloadExtension, - false - ); - - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); - expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); - expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); - }); - - it.skip("checks if cache already exists for each entry", async () => { - const existsSpy = vi.spyOn(fsExtra, "exists"); - await triggerCacheCreation( - setupFunctions, - hashes, - downloadExtension, - false - ); - - expect(existsSpy).toHaveBeenCalledTimes(2); - expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, "hash1")); - expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, "hash2")); - }); - - it("returns an array of createCacheForWalletSetupFunction promises", async () => { - const promises = await triggerCacheCreation( - setupFunctions, - hashes, - downloadExtension, - false - ); - - console.log(promises); - - expect(promises).toHaveLength(2); - expect(promises[0]).toBeInstanceOf(Promise); - expect(promises[1]).toBeInstanceOf(Promise); - }); - - describe("when force flag is false", () => { - it.skip("ignores setup function for which cache already exists", async () => { - const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); + vi.clearAllMocks() + vol.reset() // Clear the in-memory file system after each test + }) + + const hashes = ['hash1', 'hash2'] + const setupFunctions = prepareSetupFunctions(hashes) + + it('calls ensureCacheDirExists', async () => { + const ensureCacheDirExistsSpy = vi.spyOn(EnsureCacheDirExists, 'ensureCacheDirExists') + + await triggerCacheCreation(setupFunctions, hashes, downloadExtension, false) + + expect(ensureCacheDirExistsSpy).toHaveBeenCalledOnce() + }) + + it('calls passed downloadExtension function', async () => { + const setupFunctions = prepareSetupFunctions(hashes) + await triggerCacheCreation(setupFunctions, hashes, downloadExtension, false) + + expect(downloadExtension).toHaveBeenCalledOnce() + }) + + it.skip('calls createCacheForWalletSetupFunction with correct arguments', async () => { + await triggerCacheCreation(setupFunctions, hashes, downloadExtension, false) + + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') + }) + + it.skip('checks if cache already exists for each entry', async () => { + const existsSpy = vi.spyOn(fsExtra, 'exists') + await triggerCacheCreation(setupFunctions, hashes, downloadExtension, false) + + expect(existsSpy).toHaveBeenCalledTimes(2) + expect(existsSpy).toHaveBeenNthCalledWith(1, path.join(ROOT_DIR, 'hash1')) + expect(existsSpy).toHaveBeenNthCalledWith(2, path.join(ROOT_DIR, 'hash2')) + }) + + it('returns an array of createCacheForWalletSetupFunction promises', async () => { + const promises = await triggerCacheCreation(setupFunctions, hashes, downloadExtension, false) + + console.log(promises) + + expect(promises).toHaveLength(2) + expect(promises[0]).toBeInstanceOf(Promise) + expect(promises[1]).toBeInstanceOf(Promise) + }) + + describe('when force flag is false', () => { + it.skip('ignores setup function for which cache already exists', async () => { + const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) // Creating cache for 2nd setup function. - fs.mkdirSync(path.join(ROOT_DIR, "hash2")); - - const promises = await triggerCacheCreation( - setupFunctions, - [...hashes, "hash3"], - downloadExtension, - false - ); - - expect(promises).toHaveLength(2); - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2); - expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); - expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash3"); - }); - }); - - describe("when force flag is true", () => { - it.skip("removes cache if it already exists for given setup function", async () => { - const setupFunctions = prepareSetupFunctions(["hash1", "hash2", "hash3"]); + fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) + + const promises = await triggerCacheCreation(setupFunctions, [...hashes, 'hash3'], downloadExtension, false) + + expect(promises).toHaveLength(2) + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(2) + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash3') + }) + }) + + describe('when force flag is true', () => { + it.skip('removes cache if it already exists for given setup function', async () => { + const setupFunctions = prepareSetupFunctions(['hash1', 'hash2', 'hash3']) // Creating cache for 2nd setup function. - const pathToExistingCache = path.join(ROOT_DIR, "hash2"); - fs.mkdirSync(pathToExistingCache); + const pathToExistingCache = path.join(ROOT_DIR, 'hash2') + fs.mkdirSync(pathToExistingCache) - await triggerCacheCreation( - setupFunctions, - [...hashes, "hash3"], - downloadExtension, - true - ); + await triggerCacheCreation(setupFunctions, [...hashes, 'hash3'], downloadExtension, true) - expect(fs.existsSync(pathToExistingCache)).toBe(false); - }); + expect(fs.existsSync(pathToExistingCache)).toBe(false) + }) - it.skip("calls createCacheForWalletSetupFunction for setup functions that were previously cached", async () => { - const setupFunctions = prepareSetupFunctions([...hashes, "hash3"]); + it.skip('calls createCacheForWalletSetupFunction for setup functions that were previously cached', async () => { + const setupFunctions = prepareSetupFunctions([...hashes, 'hash3']) // Creating cache for 2nd setup function. - fs.mkdirSync(path.join(ROOT_DIR, "hash2")); - - const promises = await triggerCacheCreation( - setupFunctions, - [...hashes, "hash3"], - downloadExtension, - true - ); - - expect(promises).toHaveLength(3); - expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3); - expectCreateCacheForWalletSetupFunction(1, setupFunctions, "hash1"); - expectCreateCacheForWalletSetupFunction(2, setupFunctions, "hash2"); - expectCreateCacheForWalletSetupFunction(3, setupFunctions, "hash3"); - }); - }); -}); + fs.mkdirSync(path.join(ROOT_DIR, 'hash2')) + + const promises = await triggerCacheCreation(setupFunctions, [...hashes, 'hash3'], downloadExtension, true) + + expect(promises).toHaveLength(3) + expect(createCacheForWalletSetupFunctionSpy).toHaveBeenCalledTimes(3) + expectCreateCacheForWalletSetupFunction(1, setupFunctions, 'hash1') + expectCreateCacheForWalletSetupFunction(2, setupFunctions, 'hash2') + expectCreateCacheForWalletSetupFunction(3, setupFunctions, 'hash3') + }) + }) +})