Skip to content

Commit 8183895

Browse files
authored
test(background-refresh-mock-integrations): add mock integration test suite (JhaSourav07#2921) (JhaSourav07#3069)
## Description Adds the comprehensive background-refresh.mock-integrations.test.ts test suite to verify asynchronous service layer mocking, local cache stubs, and fallback paths under the background refresh service. Fixes JhaSourav07#2921 ## Pillar - [ ] 🎨 Pillar 1 — New Theme Design - [ ] 📐 Pillar 2 — Geometric SVG Improvement - [ ] 🕐 Pillar 3 — Timezone Logic Optimization - [x] 🛠️ Other (Bug fix, refactoring, docs) ## Visual Preview N/A (Utility/Unit Tests only) ## Checklist before requesting a review: - [x] I have read the `CONTRIBUTING.md` file. - [x] I have tested these changes locally. - [x] I have run `npm run format` and `npm run lint` locally and resolved all errors. - [x] My commits follow the Conventional Commits format. - [x] I have updated `README.md` if I added a new theme or URL parameter. - [x] I have started the repo. - [x] I have made sure that i have only one commit to merge in this PR. - [ ] The SVG output matches the CommitPulse "premium quality" aesthetic standard. - [ ] (Recommended) I joined the CommitPulse Discord community.
2 parents 2075a0b + ed1a79e commit 8183895

1 file changed

Lines changed: 93 additions & 0 deletions

File tree

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
import { describe, it, expect, vi, beforeEach } from 'vitest';
2+
import { BackgroundRefresh } from './background-refresh';
3+
import { getFullDashboardData } from '../../lib/github';
4+
5+
vi.mock('../../lib/github', () => ({
6+
getFullDashboardData: vi.fn(),
7+
}));
8+
9+
describe('BackgroundRefresh Mock Integrations', () => {
10+
let service: BackgroundRefresh;
11+
12+
beforeEach(() => {
13+
service = BackgroundRefresh.getInstance();
14+
service.reset();
15+
vi.clearAllMocks();
16+
});
17+
18+
it('mocks standard asynchronous imports using stubs for background refresh', async () => {
19+
vi.mocked(getFullDashboardData).mockResolvedValue(
20+
{} as unknown as Awaited<ReturnType<typeof getFullDashboardData>>
21+
);
22+
23+
service.triggerRefresh('john_doe');
24+
25+
expect(service.isJobActive('john_doe')).toBe(true);
26+
expect(getFullDashboardData).toHaveBeenCalledWith('john_doe', { bypassCache: true });
27+
});
28+
29+
it('tests service loading paths to ensure pending state overlays render', async () => {
30+
let resolvePromise!: (val: unknown) => void;
31+
const promise = new Promise((resolve) => {
32+
resolvePromise = resolve;
33+
});
34+
vi.mocked(getFullDashboardData).mockReturnValue(
35+
promise as unknown as ReturnType<typeof getFullDashboardData>
36+
);
37+
38+
service.triggerRefresh('pending_user');
39+
expect(service.isJobActive('pending_user')).toBe(true);
40+
41+
resolvePromise({});
42+
await promise;
43+
44+
// Small microtask tick to let the finally block run
45+
await new Promise((resolve) => setTimeout(resolve, 0));
46+
47+
expect(service.isJobActive('pending_user')).toBe(false);
48+
});
49+
50+
it('asserts local cache layers are queried before triggering database retrievals', () => {
51+
// Check if cache is stale or not. If not stale, we should not trigger database retrievals.
52+
const lastSyncedTenSecsAgo = new Date(Date.now() - 10000).toISOString();
53+
const isStale = service.isStale(lastSyncedTenSecsAgo);
54+
55+
expect(isStale).toBe(false);
56+
57+
if (isStale) {
58+
service.triggerRefresh('cached_user');
59+
}
60+
61+
expect(getFullDashboardData).not.toHaveBeenCalled();
62+
});
63+
64+
it('verifies correct fallback procedures during fake endpoint timeout blocks', async () => {
65+
const consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
66+
vi.mocked(getFullDashboardData).mockRejectedValue(new Error('timeout'));
67+
68+
service.triggerRefresh('timeout_user');
69+
expect(service.isJobActive('timeout_user')).toBe(true);
70+
71+
// Wait for promise rejection to propagate
72+
await new Promise((resolve) => setTimeout(resolve, 0));
73+
74+
expect(service.isJobActive('timeout_user')).toBe(false);
75+
expect(consoleErrorSpy).toHaveBeenCalled();
76+
consoleErrorSpy.mockRestore();
77+
});
78+
79+
it('asserts complete cache sync is written on success callbacks', async () => {
80+
const consoleInfoSpy = vi.spyOn(console, 'info').mockImplementation(() => {});
81+
vi.mocked(getFullDashboardData).mockResolvedValue({ success: true } as unknown as Awaited<
82+
ReturnType<typeof getFullDashboardData>
83+
>);
84+
85+
service.triggerRefresh('sync_user');
86+
await new Promise((resolve) => setTimeout(resolve, 0));
87+
88+
expect(consoleInfoSpy).toHaveBeenCalledWith(
89+
expect.stringContaining('Successfully completed background refresh for: sync_user')
90+
);
91+
consoleInfoSpy.mockRestore();
92+
});
93+
});

0 commit comments

Comments
 (0)