From 558aaf22bd927d3c48be326788027a4fc0127f50 Mon Sep 17 00:00:00 2001 From: Oscar Kilhed Date: Tue, 11 Jun 2024 11:06:19 +0200 Subject: [PATCH] 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 --- .../saving/getDashboardChanges.test.ts | 30 +++++++++++++++++++ .../saving/getDashboardChanges.ts | 10 +++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts b/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts index 23eddf6ced6..e8635add8ed 100644 --- a/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts +++ b/public/app/features/dashboard-scene/saving/getDashboardChanges.test.ts @@ -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 }; diff --git a/public/app/features/dashboard-scene/saving/getDashboardChanges.ts b/public/app/features/dashboard-scene/saving/getDashboardChanges.ts index 3d651cf30ee..3a65b20e53c 100644 --- a/public/app/features/dashboard-scene/saving/getDashboardChanges.ts +++ b/public/app/features/dashboard-scene/saving/getDashboardChanges.ts @@ -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) {