Skip to content

Commit 2effa4a

Browse files
committed
Update tests to use mockCtx with hook for plugins
Refactored formula-plugin and validator-plugin tests to use a mockCtx object with a hook function instead of relying on the kernel's use method. This improves test accuracy for plugins that register hooks and ensures compatibility with different kernel configurations.
1 parent 4ef33a1 commit 2effa4a

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

packages/foundation/core/test/formula-plugin.test.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@ import { ObjectKernel } from '@objectstack/runtime';
1212
describe('FormulaPlugin', () => {
1313
let plugin: FormulaPlugin;
1414
let mockKernel: any;
15+
let mockCtx: any;
1516

1617
beforeEach(() => {
1718
// Create a mock kernel with middleware and formula provider support
1819
mockKernel = {
1920
use: jest.fn(),
2021
registerFormulaProvider: jest.fn(),
2122
};
23+
mockCtx = {
24+
app: mockKernel,
25+
hook: jest.fn(),
26+
};
2227
plugin = new FormulaPlugin();
2328
});
2429

@@ -55,16 +60,14 @@ describe('FormulaPlugin', () => {
5560

5661
describe('Installation', () => {
5762
it('should install successfully with mock kernel', async () => {
58-
const ctx = { app: mockKernel };
59-
await plugin.init(ctx);
63+
await plugin.init(mockCtx);
6064

6165
// Verify that formula provider was registered
6266
expect(mockKernel.registerFormulaProvider).toHaveBeenCalled();
6367
});
6468

6569
it('should register formula provider with evaluate function', async () => {
66-
const ctx = { app: mockKernel };
67-
await plugin.init(ctx);
70+
await plugin.init(mockCtx);
6871

6972
expect(mockKernel.registerFormulaProvider).toHaveBeenCalledWith(
7073
expect.objectContaining({
@@ -77,22 +80,20 @@ describe('FormulaPlugin', () => {
7780

7881
it('should register formula middleware when auto-evaluation is enabled', async () => {
7982
const pluginWithAuto = new FormulaPlugin({ autoEvaluateOnQuery: true });
80-
const ctx = { app: mockKernel };
8183

82-
await pluginWithAuto.init(ctx);
84+
await pluginWithAuto.init(mockCtx);
8385

8486
// Check that middleware was registered
85-
expect(mockKernel.use).toHaveBeenCalledWith('afterQuery', expect.any(Function));
87+
expect(mockCtx.hook).toHaveBeenCalledWith('afterQuery', expect.any(Function));
8688
});
8789

8890
it('should not register formula middleware when auto-evaluation is disabled', async () => {
8991
const pluginNoAuto = new FormulaPlugin({ autoEvaluateOnQuery: false });
90-
const ctx = { app: mockKernel };
9192

92-
await pluginNoAuto.init(ctx);
93+
await pluginNoAuto.init(mockCtx);
9394

9495
// Should not have registered afterQuery hook
95-
const afterQueryCalls = mockKernel.use.mock.calls.filter(
96+
const afterQueryCalls = mockCtx.hook.mock.calls.filter(
9697
(call: any[]) => call[0] === 'afterQuery'
9798
);
9899
expect(afterQueryCalls.length).toBe(0);
@@ -102,7 +103,7 @@ describe('FormulaPlugin', () => {
102103
const kernelNoProvider = {
103104
use: jest.fn(),
104105
};
105-
const ctx = { app: kernelNoProvider };
106+
const ctx = { app: kernelNoProvider, hook: jest.fn() };
106107

107108
// Should not throw error and should set formulaEngine property
108109
await expect(plugin.init(ctx)).resolves.not.toThrow();
@@ -113,7 +114,7 @@ describe('FormulaPlugin', () => {
113114
const kernelNoMiddleware = {
114115
registerFormulaProvider: jest.fn(),
115116
};
116-
const ctx = { app: kernelNoMiddleware };
117+
const ctx = { app: kernelNoMiddleware, hook: undefined }; // Simulate no hook support
117118

118119
// Should not throw error
119120
await expect(plugin.init(ctx)).resolves.not.toThrow();

packages/foundation/core/test/validator-plugin.test.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,17 @@ import { ObjectKernel } from '@objectstack/runtime';
1212
describe('ValidatorPlugin', () => {
1313
let plugin: ValidatorPlugin;
1414
let mockKernel: any;
15+
let mockCtx: any;
1516

1617
beforeEach(() => {
1718
// Create a mock kernel with middleware support
1819
mockKernel = {
1920
use: jest.fn(),
2021
};
22+
mockCtx = {
23+
app: mockKernel,
24+
hook: jest.fn(),
25+
};
2126
plugin = new ValidatorPlugin();
2227
});
2328

@@ -54,62 +59,57 @@ describe('ValidatorPlugin', () => {
5459

5560
describe('Installation', () => {
5661
it('should install successfully with mock kernel', async () => {
57-
const ctx = { app: mockKernel };
58-
await plugin.init(ctx);
62+
await plugin.init(mockCtx);
5963

6064
// Verify that middleware hooks were registered
61-
expect(mockKernel.use).toHaveBeenCalled();
65+
expect(mockCtx.hook).toHaveBeenCalled();
6266
});
6367

6468
it('should register query validation when enabled', async () => {
6569
const pluginWithQuery = new ValidatorPlugin({ enableQueryValidation: true });
66-
const ctx = { app: mockKernel };
6770

68-
await pluginWithQuery.init(ctx);
71+
await pluginWithQuery.init(mockCtx);
6972

7073
// Check that use was called (for query validation)
71-
expect(mockKernel.use).toHaveBeenCalledWith('beforeQuery', expect.any(Function));
74+
expect(mockCtx.hook).toHaveBeenCalledWith('beforeQuery', expect.any(Function));
7275
});
7376

7477
it('should register mutation validation when enabled', async () => {
7578
const pluginWithMutation = new ValidatorPlugin({ enableMutationValidation: true });
76-
const ctx = { app: mockKernel };
7779

78-
await pluginWithMutation.init(ctx);
80+
await pluginWithMutation.init(mockCtx);
7981

8082
// Check that use was called (for mutation validation)
81-
expect(mockKernel.use).toHaveBeenCalledWith('beforeMutation', expect.any(Function));
83+
expect(mockCtx.hook).toHaveBeenCalledWith('beforeMutation', expect.any(Function));
8284
});
8385

8486
it('should not register query validation when disabled', async () => {
8587
const pluginNoQuery = new ValidatorPlugin({ enableQueryValidation: false });
86-
const ctx = { app: mockKernel };
8788

88-
await pluginNoQuery.init(ctx);
89+
await pluginNoQuery.init(mockCtx);
8990

9091
// Should not have registered beforeQuery hook
91-
const beforeQueryCalls = mockKernel.use.mock.calls.filter(
92+
const beforeQueryCalls = mockCtx.hook.mock.calls.filter(
9293
(call: any[]) => call[0] === 'beforeQuery'
9394
);
9495
expect(beforeQueryCalls.length).toBe(0);
9596
});
9697

9798
it('should not register mutation validation when disabled', async () => {
9899
const pluginNoMutation = new ValidatorPlugin({ enableMutationValidation: false });
99-
const ctx = { app: mockKernel };
100100

101-
await pluginNoMutation.init(ctx);
101+
await pluginNoMutation.init(mockCtx);
102102

103103
// Should not have registered beforeMutation hook
104-
const beforeMutationCalls = mockKernel.use.mock.calls.filter(
104+
const beforeMutationCalls = mockCtx.hook.mock.calls.filter(
105105
(call: any[]) => call[0] === 'beforeMutation'
106106
);
107107
expect(beforeMutationCalls.length).toBe(0);
108108
});
109109

110110
it('should handle kernel without middleware support', async () => {
111111
const kernelNoMiddleware = {};
112-
const ctx = { app: kernelNoMiddleware };
112+
const ctx = { app: kernelNoMiddleware, hook: undefined }; // Simulate no hook support
113113

114114
// Should not throw error
115115
await expect(plugin.init(ctx)).resolves.not.toThrow();

0 commit comments

Comments
 (0)