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) {