Files
Sonia Aguilar 2014d27def Alerting: Add alert rule version history - part1 (#99490)
* Add alertingRuleVersionHistory feature toggle

* WIP: Add version history tab

* revert temp change in index.ts

* wip2

* --wip--

* sync code with the BE changes in the endpoint

* add translations

* Add translations

* use ff only for restore feature

* WIP: Add tracking, make version required, and start mapping dif results
Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>

* Tweak more translations and improve types

* Add button to show/hide JSON diff

* update type for top level rule fields

* Create types

* Make updated_by/version properties optional

* Update mocks to remove updated by and version

* add comments to restore code

* rename fetature flag, as we use this one only for the restore feature

* Update version history to handle special cases

* Add diff numbers

* Fix conflicts

* Move generic computeVersionDiff to a utils file

* Update DOM structure of version summary and tidy up types

* Add tests for version comparison logic

* Lint fix utils file

* Rename props and add docs

* Change to EmptyState and log when no versions

* Remove CreatedBy component and simplify

* Add missing i18n for version history

* add test for computeVersionDiff

* update test

* fix number diff order and add a test

* fix prettier

* fix prettier

* Add promise resolve back in

* Rename to humanReadableDiff and tweak translation

* Show tab for recording rules as well

* Split components out to separate files

* Add optional interval seconds

* Update i18n

* Remove commented code

* Remove value

* Remove unneeded version

* Consistent rendering of updated by

* Mode parseVersionInfo to a separate pure function

* update invalidate/provide tags for getAlertVersionHistory

* Use checkedVersions state only in the parent component

* update getSpecialUidMap name and create an interface

* Fix prettier

* update tab description

* use set instead of map for checkedVersions

---------

Co-authored-by: Tom Ratcliffe <tom.ratcliffe@grafana.com>
2025-02-17 13:25:32 +01:00

59 lines
1.5 KiB
TypeScript

import { computeVersionDiff } from './diff';
describe('computeVersionDiff', () => {
it('should compute the correct diff for added and removed lines', () => {
const json1 = { a: 1, b: 2 };
const json2 = { a: 1, b: 3, c: 4 };
const result = computeVersionDiff(json1, json2);
expect(result.added).toBe(2);
expect(result.removed).toBe(1);
});
it('should handle empty objects', () => {
const json1 = {};
const json2 = {};
const result = computeVersionDiff(json1, json2);
expect(result.added).toBe(0);
expect(result.removed).toBe(0);
});
it('should handle nested objects', () => {
const json1 = { a: { b: 1 } };
const json2 = { a: { b: 2, c: 4 } };
const result = computeVersionDiff(json1, json2);
expect(result.added).toBe(2);
expect(result.removed).toBe(1);
});
it('should handle arrays', () => {
const json1 = { a: [1, 2, 3], b: 2 };
const json2 = { a: [1, 2, 4] };
const result = computeVersionDiff(json1, json2);
expect(result.added).toBe(1);
expect(result.removed).toBe(2);
});
it('should use normalizeFunction to normalize input objects', () => {
const json1 = { a: 1, b: 2 };
const json2 = { a: 1, b: 3, c: 4 };
const normalizeFunction = (item: typeof json1) => {
const { b, ...rest } = item;
return rest;
};
const result = computeVersionDiff(json1, json2, normalizeFunction);
expect(result.added).toBe(1);
expect(result.removed).toBe(0);
});
});