Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bastianjoel committed Dec 16, 2024
1 parent c3f9992 commit 26401da
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
66 changes: 65 additions & 1 deletion client/src/app/infrastructure/utils/functions.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import {
stripHtmlTags,
toBase64,
toBoolean,
toDecimal
toDecimal,
viewModelListEqual
} from './functions';
import { copy } from './transform-functions';

Expand Down Expand Up @@ -756,4 +757,67 @@ describe(`utils: functions`, () => {
expect(findIndexInSortedArray([1, 1, 1, 1, 1, 1, 1], 1, (a, b) => a - b)).toBe(0);
});
});

describe(`viewModelListEqual`, () => {
const els: any[] = [
{ viewModelUpdateTimestamp: 1234, id: 1 },
{ viewModelUpdateTimestamp: 1235, id: 1 },
{ viewModelUpdateTimestamp: 1234, id: 2 },
{ viewModelUpdateTimestamp: 1235, id: 2 },
{ viewModelUpdateTimestamp: 1234, id: 3 }
];

it(`detects change via id switch`, () => {
const l1 = [els[0], els[2]];
const l2 = [els[0], els[4]];

expect(viewModelListEqual(l1, l2)).toBeFalse();
});

it(`detects change via change date switch`, () => {
const l1 = [els[0], els[2]];
const l2 = [els[0], els[3]];

expect(viewModelListEqual(l1, l2)).toBeFalse();
});

it(`detects change via size change`, () => {
const l1 = [els[0], els[2]];
const l2 = [els[0]];

expect(viewModelListEqual(l1, l2)).toBeFalse();
});

it(`detects equality`, () => {
const l1 = [els[0], els[2], els[4]];
const l2 = [els[0], els[2], els[4]];

expect(viewModelListEqual(l1, l2)).toBeTrue();
});
});

describe(`viewModelEqual`, () => {
const els: any[] = [
{ viewModelUpdateTimestamp: 1234, id: 1 },
{ viewModelUpdateTimestamp: 1235, id: 1 },
{ viewModelUpdateTimestamp: 1234, id: 2 },
{ viewModelUpdateTimestamp: 1234, id: 1 }
];

it(`detects change via id switch`, () => {
expect(viewModelListEqual(els[0], els[2])).toBeFalse();
});

it(`detects change via change date switch`, () => {
expect(viewModelListEqual(els[0], els[1])).toBeFalse();
});

it(`detects change via empty`, () => {
expect(viewModelListEqual(els[0], null)).toBeFalse();
});

it(`detects equality`, () => {
expect(viewModelListEqual(els[0], els[3])).toBeTrue();
});
});
});
14 changes: 9 additions & 5 deletions client/src/app/infrastructure/utils/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,18 @@ export function viewModelListEqual<M extends BaseModel>(l1: BaseViewModel<M>[],
}

for (let i = 0; i < l1.length; i++) {
if (l1[i].viewModelUpdateTimestamp !== l2[i].viewModelUpdateTimestamp) {
return false;
}

if (l1[i].id !== l2[i].id) {
if (!viewModelEqual(l1[i], l2[i])) {
return false;
}
}

return true;
}

export function viewModelEqual<M extends BaseModel>(vm1: BaseViewModel<M>, vm2: BaseViewModel<M>): boolean {
if (!vm1 || !vm2) {
return false;
}

return vm1.viewModelUpdateTimestamp === vm2.viewModelUpdateTimestamp && vm1.id === vm2.id;
}

0 comments on commit 26401da

Please sign in to comment.