feat(VNA-409): check chart own handler (#1847)

* feat(VNA-409): check chart own handler
This commit is contained in:
Donya MashaallahPoor
2025-11-27 09:07:25 +01:00
committed by GitHub
parent 85e869821a
commit 7f03c1cd13
3 changed files with 114 additions and 2 deletions

View File

@@ -26,6 +26,9 @@ describe('viz', () => {
let convertToMock; let convertToMock;
let createSessionObjectMock; let createSessionObjectMock;
let destroySessionObjectMock; let destroySessionObjectMock;
let mockCustomHandler;
let mockCustomDataHandler;
let mockGetHypercubePath;
let mockElement; let mockElement;
@@ -43,6 +46,9 @@ describe('viz', () => {
getImperativeHandle = jest.fn(async () => ({ getImperativeHandle = jest.fn(async () => ({
api: 'api', api: 'api',
})); }));
mockCustomHandler = { customMethod: jest.fn() };
mockCustomDataHandler = jest.fn().mockReturnValue(mockCustomHandler);
mockGetHypercubePath = jest.fn().mockReturnValue('customPath');
cellRef = { cellRef = {
current: { current: {
setSnOptions, setSnOptions,
@@ -53,6 +59,7 @@ describe('viz', () => {
takeSnapshot, takeSnapshot,
exportImage, exportImage,
getImperativeHandle, getImperativeHandle,
getHypercubePath: mockGetHypercubePath,
}, },
}; };
glue = jest.fn().mockReturnValue([unmountMock, cellRef]); glue = jest.fn().mockReturnValue([unmountMock, cellRef]);
@@ -311,4 +318,104 @@ describe('viz', () => {
expect(panelDef.name).toEqual('old'); expect(panelDef.name).toEqual('old');
}); });
}); });
describe('getHypercubePropertyHandler', () => {
test('should return default HyperCubeHandler when no custom dataHandler exists', async () => {
const opts = { onInitialRender: jest.fn() };
api.__DO_NOT_USE__.options(opts);
await mounted;
cellRef.current.getExtensionDefinition = jest.fn().mockReturnValue({
data: {
dimensions: { min: 1, max: 10 },
measures: { min: 1, max: 10 },
},
definition: {},
});
const args = cellRef.current.setSnOptions.mock.lastCall[0];
args.onInitialRender();
const handler = await api.getHypercubePropertyHandler();
expect(handler.constructor.name).toBe('HyperCubeHandler');
expect(cellRef.current.getExtensionDefinition).toHaveBeenCalled();
expect(model.getEffectiveProperties).toHaveBeenCalled();
});
test('should return custom handler when dataHandler exists in extension definition', async () => {
const opts = { onInitialRender: jest.fn() };
api.__DO_NOT_USE__.options(opts);
await mounted;
cellRef.current.getExtensionDefinition = jest.fn().mockReturnValue({
data: {
dimensions: { min: 1, max: 10 },
measures: { min: 1, max: 10 },
},
definition: {
dataHandler: mockCustomDataHandler,
},
});
const args = cellRef.current.setSnOptions.mock.lastCall[0];
args.onInitialRender();
const handler = await api.getHypercubePropertyHandler();
expect(handler).toBe(mockCustomHandler);
expect(mockCustomDataHandler).toHaveBeenCalledWith({
app: model.app,
dimensionDefinition: { min: 1, max: 10 },
measureDefinition: { min: 1, max: 10 },
dimensionProperties: expect.any(Object),
measureProperties: expect.any(Object),
globalChangeListeners: undefined,
path: 'customPath',
});
expect(cellRef.current.getHypercubePath).toHaveBeenCalled();
});
test('should return undefined when no data definition exists', async () => {
const opts = { onInitialRender: jest.fn() };
api.__DO_NOT_USE__.options(opts);
await mounted;
cellRef.current.getExtensionDefinition = jest.fn().mockReturnValue({
definition: {
dataHandler: mockCustomDataHandler,
},
});
const args = cellRef.current.setSnOptions.mock.lastCall[0];
args.onInitialRender();
const handler = await api.getHypercubePropertyHandler();
expect(handler).toBeUndefined();
expect(mockCustomDataHandler).not.toHaveBeenCalled();
});
test('should handle invalid dataHandler', async () => {
const opts = { onInitialRender: jest.fn() };
api.__DO_NOT_USE__.options(opts);
await mounted;
cellRef.current.getExtensionDefinition = jest.fn().mockReturnValue({
data: {
dimensions: { min: 1, max: 10 },
measures: { min: 1, max: 10 },
},
definition: {
dataHandler: 'not-a-function',
},
});
const args = cellRef.current.setSnOptions.mock.lastCall[0];
args.onInitialRender();
const handler = await api.getHypercubePropertyHandler();
expect(handler.constructor.name).toBe('HyperCubeHandler');
});
});
}); });

View File

@@ -121,7 +121,8 @@ export default function viz({
async getHypercubePropertyHandler() { async getHypercubePropertyHandler() {
await rendered; await rendered;
const dataDefinition = cellRef.current.getExtensionDefinition().data; const extensionDefinition = cellRef.current.getExtensionDefinition();
const dataDefinition = extensionDefinition.data;
const properties = await model.getEffectiveProperties(); const properties = await model.getEffectiveProperties();
if (dataDefinition) { if (dataDefinition) {
@@ -135,6 +136,10 @@ export default function viz({
path: cellRef.current.getHypercubePath(), path: cellRef.current.getHypercubePath(),
}; };
if (typeof extensionDefinition.definition.dataHandler === 'function') {
return extensionDefinition.definition.dataHandler(options);
}
return new HyperCubeHandler(options); return new HyperCubeHandler(options);
} }

View File

@@ -75,7 +75,7 @@ export default function generatorFn(UserSN, galaxy) {
}, },
definition: galaxy.flags.isEnabled('NEBULA_DATA_HANDLERS') definition: galaxy.flags.isEnabled('NEBULA_DATA_HANDLERS')
? { ? {
dataHandler: (opts) => new HyperCubeHandler(opts), dataHandler: sn?.dataHandler ?? ((opts) => new HyperCubeHandler(opts)),
} }
: {}, : {},
}; };