Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/__tests__/patternFly.helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('../server.getResources', () => ({
}
}));

const mockReadLocalFile = readLocalFileFunction.memo as jest.Mock;
const mockReadLocalFile = readLocalFileFunction.memo as any as jest.Mock;

describe('findClosestPatternFlyVersion', () => {
it('should provide a temporary closest version that always returns the latest version', async () => {
Expand Down
135 changes: 135 additions & 0 deletions src/__tests__/server.caching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -362,4 +362,139 @@ describe('memo', () => {
await expect(updateLog(log)).resolves.toMatchSnapshot('sync');
await expect(updateLog(logAsync)).resolves.toMatchSnapshot('async');
});

it.each([
{
description: 'clear all entries',
setup: (mem: any) => {
mem(1);
mem(2);
mem(3);
},
action: (mem: any) => mem.clear(),
expectedResult: true,
expectedRemainingCount: 0
},
{
description: 'clear specific key',
setup: (mem: any) => {
mem(1);
mem(2);
},
action: (mem: any) => {
const key = mem.getKey(1);

return mem.clear(key);
},
expectedResult: true,
expectedRemainingCount: 1
},
{
description: 'clear non-existent key',
setup: (mem: any) => { mem(1); },
action: (mem: any) => mem.clear('non-existent'),
expectedResult: false,
expectedRemainingCount: 1
}
])('should handle clear() without callback, $description', ({ setup, action, expectedResult, expectedRemainingCount }) => {
const memoized = memo((val: number) => val + 1, { cacheLimit: 10 });

setup(memoized);

const result = action(memoized);

expect(result).toBe(expectedResult);
expect(memoized.keys()).toHaveLength(expectedRemainingCount);
});

it.each([
{
description: 'clear all entries',
setup: (mem: any) => {
mem(7);
mem(2);
mem(9);
},
action: (mem: any) => mem.clear(),
expectedResult: true,
expectedRemainingCount: 0,
expectedRemoved: [8, 3, 10]
},
{
description: 'clear specific key',
setup: (mem: any) => {
mem(10);
mem(2);
},
action: (mem: any) => {
const key = mem.getKey(10);

return mem.clear(key);
},
expectedResult: true,
expectedRemainingCount: 1,
expectedRemoved: [11]
}
])('should fire onCacheClear callback, $description', ({ setup, action, expectedResult, expectedRemainingCount, expectedRemoved }) => {
const spy = jest.fn();
const memoized = memo((val: number) => val + 1, { cacheLimit: 10, onCacheClear: spy });

setup(memoized);
const result = action(memoized);

expect(result).toBe(expectedResult);
expect(memoized.keys()).toHaveLength(expectedRemainingCount);

expect(spy).toHaveBeenCalledTimes(1);
const payload = spy.mock.calls[0][0];

expect(payload.removed).toEqual(expect.arrayContaining(expectedRemoved));
expect(payload.removed).toHaveLength(expectedRemoved.length);
expect(payload.remaining).toHaveLength(expectedRemainingCount);
});

it.each([
{
description: 'non-existent key',
setup: (mem: any) => { mem(1); },
action: (mem: any) => mem.clear('non-existent'),
expectedResult: false,
expectedRemainingCount: 1
}
])('should NOT fire onCacheClear callback, $description', ({ setup, action, expectedResult, expectedRemainingCount }) => {
const spy = jest.fn();
const memoized = memo((val: number) => val + 1, { cacheLimit: 10, onCacheClear: spy });

setup(memoized);
const result = action(memoized);

expect(result).toBe(expectedResult);
expect(memoized.keys()).toHaveLength(expectedRemainingCount);
expect(spy).not.toHaveBeenCalled();
});

it('should return consistent structure from keys()', () => {
const memoized = memo((val: number) => val + 1, { cacheLimit: 10 });

memoized(1);
const keys = memoized.keys();

expect(keys).toHaveLength(1);
expect(keys[0]).toEqual({
key: expect.any(String),
value: 2,
index: 0
});
});

it('should return key from getKey() when found', () => {
const memoized = memo((val: number) => val + 1);

memoized(1);
const keys = memoized.keys();
const key = keys[0]?.key;

expect(memoized.getKey(1)).toBe(key);
expect(memoized.getKey(2)).toBeUndefined();
});
});
14 changes: 7 additions & 7 deletions src/__tests__/server.getResources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,8 @@ describe('loadFileFetch', () => {
expectedIsFetch: false
}
])('should attempt to load a file or fetch, $description', async ({ pathUrl, expectedIsFetch }) => {
const mockFetchCall = jest.fn().mockResolvedValue('content');
const mockReadCall = jest.fn().mockResolvedValue('content');
const mockFetchCall = jest.fn().mockResolvedValue('content') as any;
const mockReadCall = jest.fn().mockResolvedValue('content') as any;

readLocalFileFunction.memo = mockReadCall;
fetchUrlFunction.memo = mockFetchCall;
Expand All @@ -332,8 +332,8 @@ describe('promiseQueue', () => {
});

it('should execute promises in order', async () => {
readLocalFileFunction.memo = jest.fn().mockImplementation(path => Promise.resolve(path));
fetchUrlFunction.memo = jest.fn().mockImplementation(url => Promise.reject(url));
readLocalFileFunction.memo = jest.fn().mockImplementation(path => Promise.resolve(path)) as any;
fetchUrlFunction.memo = jest.fn().mockImplementation(url => Promise.reject(url)) as any;

const pathUrlQueue = ['dolor-sit.md', 'https://example.com/remote.md', 'lorem-ipsum.md'];

Expand All @@ -346,8 +346,8 @@ describe('processDocsFunction', () => {
jest.clearAllMocks();

// Mock the memo functions
readLocalFileFunction.memo = jest.fn().mockResolvedValue('local file content');
fetchUrlFunction.memo = jest.fn().mockResolvedValue('fetched content');
readLocalFileFunction.memo = jest.fn().mockResolvedValue('local file content') as any;
fetchUrlFunction.memo = jest.fn().mockResolvedValue('fetched content') as any;
});

it.each([
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('processDocsFunction', () => {
// Mock one success and one failure
readLocalFileFunction.memo = jest.fn()
.mockResolvedValueOnce('success content')
.mockRejectedValueOnce(new Error('File not found'));
.mockRejectedValueOnce(new Error('File not found')) as any;

const inputs = [
'good-file.md',
Expand Down
Loading
Loading