Skip to content

Commit

Permalink
tests: remove unnecessary describe nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianRomanski committed Jan 3, 2025
1 parent a78f6c4 commit 31e094b
Showing 1 changed file with 37 additions and 43 deletions.
80 changes: 37 additions & 43 deletions projects/nx-verdaccio/src/plugin/caching.unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,48 +38,42 @@ describe('cacheRecord', (): 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);
});

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();
});
});

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 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);

expect(
setCacheRecord(targetsCache, prefix, hashData, recordToUpdate)
).toBe(recordToUpdate);
expect(targetsCache).toHaveProperty(cacheKey, recordToUpdate);
});
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 undefined if there is no cache hit', (): void => {
cacheKeySpy.mockReturnValue('non-existent-key');
expect(getCacheRecord(targetsCache, prefix, hashData)).toBeUndefined();
});

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

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

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

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

Expand All @@ -100,7 +94,7 @@ describe('readTargetsCache', (): void => {
.mockImplementation((): Record<string, Partial<ProjectConfiguration>> => {
return MOCK_TARGETS_CACHE;
});
vi.stubEnv("NX_CACHE_PROJECT_GRAPH", 'true')
vi.stubEnv('NX_CACHE_PROJECT_GRAPH', 'true');
});

afterEach((): void => {
Expand Down

0 comments on commit 31e094b

Please sign in to comment.