Skip to content

Instantly share code, notes, and snippets.

@marcelmokos
Last active March 10, 2023 01:13
Show Gist options
  • Save marcelmokos/49afd635bcb6958e95f015f7f80cd8c6 to your computer and use it in GitHub Desktop.
Save marcelmokos/49afd635bcb6958e95f015f7f80cd8c6 to your computer and use it in GitHub Desktop.
Human code
import { getChangesOnList } from './getChangesOnList';
describe('getChangesOnList', () => {
type TestItem = { id: number; value: string };
const testItemA: TestItem = { id: 1, value: 'A' };
const testItemB: TestItem = { id: 2, value: 'B' };
const testItemC: TestItem = { id: 3, value: 'C' };
const testItems: TestItem[] = [testItemA, testItemB, testItemC];
it('should return the expected changes when the lists are the same', () => {
expect(getChangesOnList(testItems, testItems)).toEqual({
add: [],
update: [],
remove: [],
});
});
it('should return the expected changes when the lists are the same but in a different order', () => {
expect(
getChangesOnList([testItemA, testItemB], [testItemB, testItemA])
).toEqual({
add: [],
update: [],
remove: [],
});
});
it('should return the expected changes when the lists are different', () => {
expect(
getChangesOnList([testItemA, testItemB], [testItemA, testItemC])
).toEqual({
add: [testItemC],
update: [],
remove: [testItemB],
});
});
it('should return the expected changes when an item has been updated', () => {
expect(
getChangesOnList(
[testItemA, testItemB],
[{ ...testItemA, value: 'A_updated' }, testItemB]
)
).toEqual({
add: [],
update: [{ ...testItemA, value: 'A_updated' }],
remove: [],
});
});
it('should return updates for nested objects', () => {
expect(
getChangesOnList(
[{ ...testItemA, nested: { value: 'A' } }],
[{ ...testItemA, nested: { value: 'A_updated' } }]
)
).toEqual({
add: [],
update: [{ ...testItemA, nested: { value: 'A_updated' } }],
remove: [],
});
});
it('should return updates for nested arrays', () => {
expect(
getChangesOnList(
[{ ...testItemA, nested: [{ value: 'A' }] }],
[{ ...testItemA, nested: [{ value: 'A_updated' }] }]
)
).toEqual({
add: [],
update: [{ ...testItemA, nested: [{ value: 'A_updated' }] }],
remove: [],
});
});
it('should return updates for nested arrays with objects', () => {
expect(
getChangesOnList(
[{ ...testItemA, nested: [{ value: 'A' }] }],
[{ ...testItemA, nested: [{ value: 'A_updated' }] }]
)
).toEqual({
add: [],
update: [{ ...testItemA, nested: [{ value: 'A_updated' }] }],
remove: [],
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment