-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathbilling.init.unit.tests.js
More file actions
84 lines (65 loc) · 2.4 KB
/
Copy pathbilling.init.unit.tests.js
File metadata and controls
84 lines (65 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* Module dependencies.
*/
import { jest, describe, test, beforeEach, afterEach, expect } from '@jest/globals';
/**
* Unit tests for billing.init ensureSeeded integration.
*/
describe('billing.init ensureSeeded unit tests:', () => {
let billingInit;
let mockBillingPlanService;
let mockConfig;
const mockApp = {};
beforeEach(async () => {
jest.resetModules();
mockConfig = {
billing: {
meterMode: false,
packs: [],
},
};
mockBillingPlanService = {
ensureSeeded: jest.fn().mockResolvedValue({ seeded: 0, skipped: 0 }),
};
jest.unstable_mockModule('../../../config/index.js', () => ({
default: mockConfig,
}));
jest.unstable_mockModule('../services/billing.plan.service.js', () => ({
default: mockBillingPlanService,
}));
// Stub analytics and events to avoid side effects
jest.unstable_mockModule('../../../lib/services/analytics.js', () => ({
default: { groupIdentify: jest.fn() },
}));
jest.unstable_mockModule('../lib/events.js', () => ({
default: { on: jest.fn(), emit: jest.fn() },
}));
const mod = await import('../billing.init.js');
billingInit = mod.default;
});
afterEach(() => {
jest.restoreAllMocks();
});
test('ensureSeeded is called at init when meterMode=false', async () => {
await billingInit(mockApp);
expect(mockBillingPlanService.ensureSeeded).toHaveBeenCalledTimes(1);
});
test('ensureSeeded failure is swallowed when meterMode=false', async () => {
mockBillingPlanService.ensureSeeded.mockRejectedValue(new Error('DB error'));
// Should not throw — meterMode=false means graceful degradation
await expect(billingInit(mockApp)).resolves.toBeUndefined();
});
test('ensureSeeded failure re-throws when meterMode=true (fail-fast)', async () => {
mockConfig.billing.meterMode = true;
mockBillingPlanService.ensureSeeded.mockRejectedValue(new Error('seed failure'));
await expect(billingInit(mockApp)).rejects.toThrow('seed failure');
});
test('ensureSeeded success with seeded>0 logs info and resolves', async () => {
mockBillingPlanService.ensureSeeded.mockResolvedValue({ seeded: 2, skipped: 1 });
const infoSpy = jest.spyOn(console, 'info').mockImplementation(() => {});
await billingInit(mockApp);
expect(infoSpy).toHaveBeenCalledWith(
expect.stringContaining('seeded 2 plan(s)'),
);
});
});