diff --git a/projects/nx-verdaccio/src/plugin/caching.ts b/projects/nx-verdaccio/src/plugin/caching.ts index bef0bb1..8bec1df 100644 --- a/projects/nx-verdaccio/src/plugin/caching.ts +++ b/projects/nx-verdaccio/src/plugin/caching.ts @@ -1,14 +1,10 @@ -import { hashObject } from 'nx/src/hasher/file-hasher'; import { type ProjectConfiguration, readJsonFile, writeJsonFile, } from '@nx/devkit'; import { existsSync } from 'node:fs'; - -export function cacheKey(prefix: string, hashData: Record) { - return `${prefix}-${hashObject(hashData)}`; -} +import { cacheKey } from './utils/caching.utils'; export function getCacheRecord( targetsCache: Record, diff --git a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts index a6b75e1..7bc5eb2 100644 --- a/projects/nx-verdaccio/src/plugin/caching.unit-test.ts +++ b/projects/nx-verdaccio/src/plugin/caching.unit-test.ts @@ -1,59 +1,46 @@ -import { afterEach, describe, expect, type MockInstance } from 'vitest'; -import { cacheKey } from './caching'; -import * as fileHasher from 'nx/src/hasher/file-hasher'; +import { afterEach, beforeEach, describe, expect } from 'vitest'; +import * as moduleUnderTest from './caching'; +import * as cachingUtils from './utils/caching.utils'; - -describe('cacheKey', (): void => { +describe('getCacheRecord', () => { const prefix = 'warcraft'; const hashData = { race: 'orc' }; - let hashObjectSpy: MockInstance<[obj: object], string>; - - describe('hashed object', (): void => { - beforeEach((): void => { - hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - }); - afterEach((): void => { - hashObjectSpy.mockRestore(); - }); - it('should return cache key with hashed object when it is empty', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); - }); - it('should return cache key with hashed object when it is NOT empty', (): void => { - // { race: 'orc' } = 9075797468634534731 - expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); - }); - it('should call hashObject with the correct data', () => { - const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); - - const result = cacheKey(prefix, hashData); - - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - expect(result).toContain(prefix); - }); - it('should return unmodified hashObject return value', (): void => { - const hashObjectSpy = vi - .spyOn(fileHasher, 'hashObject') - .mockImplementation((): string => 'mocked-hash'); - const result = cacheKey(prefix, hashData); - - expect(result).toBe(`${prefix}-mocked-hash`); - expect(hashObjectSpy).toHaveBeenCalledTimes(1); - expect(hashObjectSpy).toHaveBeenCalledWith(hashData); - }); + let cacheKeySpy: ReturnType; + const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' }; + const targetsCache = { 'ragnaros': cacheItem }; + + beforeEach((): void => { + cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey'); + + }); + afterEach((): void => { + cacheKeySpy.mockRestore(); + }); + + it('should call cacheKey with the correct arguments', () => { + + cacheKeySpy.mockReturnValue('ragnaros'); + moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData); + expect(cacheKeySpy).toHaveBeenCalledTimes(1); }); - describe('prefix', (): void => { - it('should return cache key with unmodified prefix', (): void => { - // {} = 3244421341483603138 - expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); - }); + + it('should return the correct record if cacheKey matches', () => { + cacheKeySpy.mockReturnValue('ragnaros'); + + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(result).toEqual(cacheItem); }); - describe('format', (): void => { - const regex = /^[a-zA-Z]+-\d+$/; - it('should return a value in the format "string-numbers"', (): void => { - const result = cacheKey(prefix, hashData); - expect(result).toMatch(regex); - }); + + it('should return undefined if no matching key exists in the cache', () => { + cacheKeySpy.mockReturnValue('non-existent-key'); + + const result = moduleUnderTest.getCacheRecord(targetsCache, prefix, hashData); + + expect(result).toBeUndefined(); }); }); + + diff --git a/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts b/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts new file mode 100644 index 0000000..34386ca --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/utils/caching.utils.ts @@ -0,0 +1,5 @@ +import { hashObject } from 'nx/src/hasher/file-hasher'; + +export function cacheKey(prefix: string, hashData: Record) { + return `${prefix}-${hashObject(hashData)}`; +} diff --git a/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts b/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts new file mode 100644 index 0000000..13aa627 --- /dev/null +++ b/projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts @@ -0,0 +1,60 @@ +import { afterEach, describe, expect, type MockInstance } from 'vitest'; +import * as fileHasher from 'nx/src/hasher/file-hasher'; +import { cacheKey } from './caching.utils'; + +describe('cacheKey', (): void => { + const prefix = 'warcraft'; + const hashData = { race: 'orc' }; + let hashObjectSpy: MockInstance<[obj: object], string>; + + describe('hashed object', (): void => { + beforeEach((): void => { + hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + }); + afterEach((): void => { + hashObjectSpy.mockRestore(); + }); + it('should return cache key with hashed object when it is empty', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + it('should return cache key with hashed object when it is NOT empty', (): void => { + // { race: 'orc' } = 9075797468634534731 + expect(cacheKey(prefix, hashData)).toBe(`${prefix}-5048043832971198124`); + }); + it('should call hashObject with the correct data', () => { + const hashObjectSpy = vi.spyOn(fileHasher, 'hashObject'); + + const result = cacheKey(prefix, hashData); + + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + expect(result).toContain(prefix); + }); + it('should return unmodified hashObject return value', (): void => { + const hashObjectSpy = vi + .spyOn(fileHasher, 'hashObject') + .mockImplementation((): string => 'mocked-hash'); + const result = cacheKey(prefix, hashData); + + expect(result).toBe(`${prefix}-mocked-hash`); + expect(hashObjectSpy).toHaveBeenCalledTimes(1); + expect(hashObjectSpy).toHaveBeenCalledWith(hashData); + }); + }); + describe('prefix', (): void => { + it('should return cache key with unmodified prefix', (): void => { + // {} = 3244421341483603138 + expect(cacheKey(prefix, {})).toBe(`${prefix}-3244421341483603138`); + }); + }); + describe('format', (): void => { + const regex = /^[a-zA-Z]+-\d+$/; + it('should return a value in the format "string-numbers"', (): void => { + const result = cacheKey(prefix, hashData); + expect(result).toMatch(regex); + }); + }); +}); + +