Skip to content

Commit

Permalink
tests: delete unnecessary nesting of describe blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Dec 20, 2024
1 parent 4c35d59 commit da5919b
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 152 deletions.
250 changes: 121 additions & 129 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,159 +18,151 @@ import {
} from './caching';
import * as cachingUtils from './utils/caching.utils';

describe('caching', (): void => {
describe('cacheRecord', (): void => {
let cacheKeySpy: MockInstance<
[prefix: string, hashData: Record<string, unknown>],
string
>;

const prefix = 'warcraft';
const cacheKey = 'ragnaros';
const hashData = { race: 'orc' };
const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' };
const targetsCache = { ragnaros: cacheItem };

beforeEach((): void => {
cacheKeySpy = vi
.spyOn(cachingUtils, 'cacheKey')
.mockReturnValue(cacheKey);
});
describe('cacheRecord', (): void => {
let cacheKeySpy: MockInstance<
[prefix: string, hashData: Record<string, unknown>],
string
>;

const prefix = 'warcraft';
const cacheKey = 'ragnaros';
const hashData = { race: 'orc' };
const cacheItem = { thunderfury: 'Blessed Blade of the Windseeker' };
const targetsCache = { ragnaros: cacheItem };

beforeEach((): void => {
cacheKeySpy = vi.spyOn(cachingUtils, 'cacheKey').mockReturnValue(cacheKey);
});

afterEach((): void => {
cacheKeySpy.mockRestore();
});
afterEach((): void => {
cacheKeySpy.mockRestore();
});

describe('getCacheRecord', (): void => {
it('should call cacheKey once with correct arguments', (): void => {
getCacheRecord(targetsCache, prefix, hashData);
expect(cacheKeySpy).toHaveBeenCalledTimes(1);
expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData);
});
describe('getCacheRecord', (): void => {
it('should call cacheKey once with correct arguments', (): void => {
getCacheRecord(targetsCache, prefix, hashData);
expect(cacheKeySpy).toHaveBeenCalledTimes(1);
expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData);
});

it('should return the correct cache record if there is a cache hit', (): void => {
expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(
cacheItem
);
});
it('should return the correct cache record if there is a cache hit', (): void => {
expect(getCacheRecord(targetsCache, prefix, hashData)).toEqual(cacheItem);
});

it('should return undefined if there is no cache hit', (): void => {
cacheKeySpy.mockReturnValue('non-existent-key');
expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined();
});
it('should return undefined if there is no cache hit', (): void => {
cacheKeySpy.mockReturnValue('non-existent-key');
expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined();
});
});

describe('setCacheRecord', (): void => {
const cacheData = { thunderfury: 'Blood of Sylvanas' };
describe('setCacheRecord', (): void => {
const cacheData = { thunderfury: 'Blood of Sylvanas' };

it('should call cacheKey once with correct arguments', (): void => {
setCacheRecord(targetsCache, prefix, hashData, cacheData);
expect(cacheKeySpy).toHaveBeenCalledTimes(1);
expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData);
});
it('should call cacheKey once with correct arguments', (): void => {
setCacheRecord(targetsCache, prefix, hashData, cacheData);
expect(cacheKeySpy).toHaveBeenCalledTimes(1);
expect(cacheKeySpy).toHaveBeenCalledWith(prefix, hashData);
});

it('should set the cache record, and return it', (): void => {
expect(setCacheRecord(targetsCache, prefix, hashData, cacheData)).toBe(
cacheData
);
expect(targetsCache).toHaveProperty(cacheKey, cacheData);
});
it('should set the cache record, and return it', (): void => {
expect(setCacheRecord(targetsCache, prefix, hashData, cacheData)).toBe(
cacheData
);
expect(targetsCache).toHaveProperty(cacheKey, cacheData);
});

it('should update existing cache data, and return it', (): void => {
const recordToUpdate = { thunderfury: 'Soul of Sylvanas' };
setCacheRecord(targetsCache, prefix, hashData, cacheData);
it('should update existing cache data, and return it', (): void => {
const recordToUpdate = { thunderfury: 'Soul of Sylvanas' };
setCacheRecord(targetsCache, prefix, hashData, cacheData);

expect(
setCacheRecord(targetsCache, prefix, hashData, recordToUpdate)
).toBe(recordToUpdate);
expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate);
});
expect(
setCacheRecord(targetsCache, prefix, hashData, recordToUpdate)
).toBe(recordToUpdate);
expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate);
});
});
});

describe('readTargetsCache', (): void => {
const path = 'azeroth';
let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>;
let readJsonFileSpy: MockInstance<
[path: string, options?: JsonReadOptions],
object
>;

beforeEach((): void => {
existsSyncSpy = vi
.spyOn(nodeFs, 'existsSync')
.mockImplementation((): boolean => true);
readJsonFileSpy = vi
.spyOn(nxDevKit, 'readJsonFile')
.mockImplementation(
(): Record<string, Partial<ProjectConfiguration>> => {
return MOCK_TARGETS_CACHE;
}
);
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
});
describe('readTargetsCache', (): void => {
const path = 'azeroth';
let existsSyncSpy: MockInstance<[path: nodeFs.PathLike], boolean>;
let readJsonFileSpy: MockInstance<
[path: string, options?: JsonReadOptions],
object
>;

beforeEach((): void => {
existsSyncSpy = vi
.spyOn(nodeFs, 'existsSync')
.mockImplementation((): boolean => true);
readJsonFileSpy = vi
.spyOn(nxDevKit, 'readJsonFile')
.mockImplementation((): Record<string, Partial<ProjectConfiguration>> => {
return MOCK_TARGETS_CACHE;
});
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
});

afterEach((): void => {
existsSyncSpy.mockRestore();
readJsonFileSpy.mockRestore();
delete process.env.NX_CACHE_PROJECT_GRAPH;
});
afterEach((): void => {
existsSyncSpy.mockRestore();
readJsonFileSpy.mockRestore();
delete process.env.NX_CACHE_PROJECT_GRAPH;
});

it('should call existSync once with correct argument', (): void => {
readTargetsCache(path);
expect(existsSyncSpy).toHaveBeenCalledWith(path);
expect(existsSyncSpy).toHaveBeenCalledTimes(1);
});
it('should call existSync once with correct argument', (): void => {
readTargetsCache(path);
expect(existsSyncSpy).toHaveBeenCalledWith(path);
expect(existsSyncSpy).toHaveBeenCalledTimes(1);
});

it('should call readJsonFile once with correct argument', (): void => {
readTargetsCache(path);
expect(readJsonFileSpy).toHaveBeenCalledWith(path);
expect(readJsonFileSpy).toHaveBeenCalledTimes(1);
});
it('should call readJsonFile once with correct argument', (): void => {
readTargetsCache(path);
expect(readJsonFileSpy).toHaveBeenCalledWith(path);
expect(readJsonFileSpy).toHaveBeenCalledTimes(1);
});

it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => {
expect(readTargetsCache(path)).toEqual(MOCK_TARGETS_CACHE);
});
it('should return target cache if existsSync returns true, and NX_CACHE_PROJECT_GRAPH = true', (): void => {
expect(readTargetsCache(path)).toEqual(MOCK_TARGETS_CACHE);
});

it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
expect(readTargetsCache(path)).toEqual({});
});
it('should return empty object if NX_CACHE_PROJECT_GRAPH = false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
expect(readTargetsCache(path)).toEqual({});
});

it('should return empty object if existsSync returns false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
expect(readTargetsCache(path)).toEqual({});
});
it('should return empty object if existsSync returns false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
expect(readTargetsCache(path)).toEqual({});
});

it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
expect(readTargetsCache(path)).toEqual({});
});
it('should return empty object if existsSync returns false, and NX_CACHE_PROJECT_GRAPH = false', (): void => {
existsSyncSpy.mockImplementation((): boolean => false);
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
expect(readTargetsCache(path)).toEqual({});
});
});

describe('writeTargetsToCache', (): void => {
const writeJsonFile = vi
.spyOn(nxDevKit, 'writeJsonFile')
.mockImplementation((): string => 'dont write to file :D');
const path = 'azeroth';
describe('writeTargetsToCache', (): void => {
const writeJsonFile = vi
.spyOn(nxDevKit, 'writeJsonFile')
.mockImplementation((): string => 'dont write to file :D');
const path = 'azeroth';

afterEach((): void => {
writeJsonFile.mockRestore();
delete process.env.NX_CACHE_PROJECT_GRAPH;
});
afterEach((): void => {
writeJsonFile.mockRestore();
delete process.env.NX_CACHE_PROJECT_GRAPH;
});

it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
writeTargetsToCache(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledTimes(1);
});
it('should call writeJsonFile once with correct arguments if process.env.NX_CACHE_PROJECT_GRAPH !== false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'true';
writeTargetsToCache(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledWith(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledTimes(1);
});

it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
writeTargetsToCache(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledTimes(0);
});
it('should not call writeJsonFile if process.env.NX_CACHE_PROJECT_GRAPH == false', (): void => {
process.env.NX_CACHE_PROJECT_GRAPH = 'false';
writeTargetsToCache(path, MOCK_TARGETS_CACHE);
expect(writeJsonFile).toHaveBeenCalledTimes(0);
});
});
40 changes: 17 additions & 23 deletions projects/nx-verdaccio/src/plugin/utils/caching.utils.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('cacheKey', (): void => {
const hashData = { race: 'orc' };
const hashObjectReturnValue = '123456789';
let hashObjectSpy: MockInstance<[obj: object], string>;
const regex = /^[a-zA-Z]+-\d+$/;

beforeEach((): void => {
hashObjectSpy = vi
Expand All @@ -23,34 +24,27 @@ describe('cacheKey', (): void => {
hashObjectSpy.mockRestore();
});

describe('prefix', (): void => {
it('should return cache key with unmodified prefix', (): void => {
expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`);
});
it('should return cache key with unmodified prefix', (): void => {
expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`);
});

describe('format', (): void => {
const regex = /^[a-zA-Z]+-\d+$/;
it('should return a value in the format "string-numbers"', (): void => {
expect(cacheKey(prefix, hashData)).toMatch(regex);
});
it('should return a value in the format "string-numbers"', (): void => {
expect(cacheKey(prefix, hashData)).toMatch(regex);
});

describe('hashed object', (): void => {
it('should call hashObject once, and with correct argument', (): void => {
cacheKey(prefix, hashData);
expect(hashObjectSpy).toHaveBeenCalledTimes(1);
expect(hashObjectSpy).toHaveBeenCalledWith(hashData);
});
it('should call hashObject once, and with correct argument', (): void => {
cacheKey(prefix, hashData);
expect(hashObjectSpy).toHaveBeenCalledTimes(1);
expect(hashObjectSpy).toHaveBeenCalledWith(hashData);
});

it('should return cache key, when hashData is empty', (): void => {
expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`);
});
it('should return cache key, when hashData is empty', (): void => {
expect(cacheKey(prefix, {})).toBe(`${prefix}-${hashObjectReturnValue}`);
});

it('should return cache key, when hashData is NOT empty', (): void => {
expect(cacheKey(prefix, hashData)).toBe(
`${prefix}-${hashObjectReturnValue}`
);
});
it('should return cache key, when hashData is NOT empty', (): void => {
expect(cacheKey(prefix, hashData)).toBe(
`${prefix}-${hashObjectReturnValue}`
);
});
});

0 comments on commit da5919b

Please sign in to comment.