Dashboard scenes: Allow undefined filter prop in adhoc variable when comparing two adhoc variables. (#88993)

* Allow undefined filter prop in adhoc variable when comparing two adhoc variables

* set mock implementation before each test

* mock console.warn in each test
This commit is contained in:
Oscar Kilhed 2024-06-11 11:06:19 +02:00 committed by GitHub
parent cdbc9d801f
commit 558aaf22bd
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 38 additions and 2 deletions

View File

@ -57,6 +57,35 @@ describe('adHocVariableFiltersEqual', () => {
)
).toBeFalsy();
});
describe('when filter property is undefined', () => {
afterAll(() => {
jest.clearAllMocks();
});
it('should compare two adhoc variables where both are missing the filter property and return true', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
expect(
adHocVariableFiltersEqual({} as unknown as AdHocVariableModel, {} as unknown as AdHocVariableModel)
).toBeTruthy();
expect(warnSpy).toHaveBeenCalledWith('Adhoc variable filter property is undefined');
});
it('should compare two adhoc variables where one has no filter property and return false', () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementationOnce(() => {});
expect(
adHocVariableFiltersEqual(
{} as unknown as AdHocVariableModel,
{
filters: [{ value: 'asdio', key: 'qwe', operator: 'wer' }],
} as unknown as AdHocVariableModel
)
).toBeFalsy();
expect(warnSpy).toHaveBeenCalledWith('Adhoc variable filter property is undefined');
});
});
});
describe('getDashboardChanges', () => {
@ -85,6 +114,7 @@ describe('getDashboardChanges', () => {
],
},
};
it('should return the correct result when no changes', () => {
const changed = { ...initial };

View File

@ -72,8 +72,14 @@ export function getHasTimeChanged(saveModel: Dashboard, originalSaveModel: Dashb
}
export function adHocVariableFiltersEqual(a: AdHocVariableModel, b: AdHocVariableModel) {
if (a.filters === undefined || b.filters === undefined) {
throw new Error('AdHoc variable missing filter property');
if (a.filters === undefined && b.filters === undefined) {
console.warn('Adhoc variable filter property is undefined');
return true;
}
if ((a.filters === undefined && b.filters !== undefined) || (b.filters === undefined && a.filters !== undefined)) {
console.warn('Adhoc variable filter property is undefined');
return false;
}
if (a.filters.length !== b.filters.length) {