Skip to content

Instantly share code, notes, and snippets.

@marcelmokos
Created March 10, 2023 00:35
Show Gist options
  • Save marcelmokos/ab3b650d47401ed9decda2a562318e81 to your computer and use it in GitHub Desktop.
Save marcelmokos/ab3b650d47401ed9decda2a562318e81 to your computer and use it in GitHub Desktop.
import { getChangesOnList } from './getChangesOnList';
describe('getChangesOnList', () => {
const prevList = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'Bob' },
];
const currList = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'Robert' },
{ id: 4, name: 'Alice' },
];
it('returns the correct changes when isEqual is not provided', () => {
const changes = getChangesOnList(prevList, currList);
expect(changes.add).toEqual([{ id: 4, name: 'Alice' }]);
expect(changes.update).toEqual([{ id: 3, name: 'Robert' }]);
expect(changes.remove).toEqual([]);
});
it('returns the correct changes when isEqual is provided', () => {
const isEqual = (a: any, b: any) => a.id === b.id && a.name === b.name;
const changes = getChangesOnList(prevList, currList, isEqual);
expect(changes.add).toEqual([{ id: 4, name: 'Alice' }]);
expect(changes.update).toEqual([{ id: 3, name: 'Robert' }]);
expect(changes.remove).toEqual([]);
});
it('returns the correct changes when currList is empty', () => {
const changes = getChangesOnList(prevList, []);
expect(changes.add).toEqual([]);
expect(changes.update).toEqual([]);
expect(changes.remove).toEqual(prevList);
});
it('returns the correct changes when prevList is empty', () => {
const changes = getChangesOnList([], currList);
expect(changes.add).toEqual(currList);
expect(changes.update).toEqual([]);
expect(changes.remove).toEqual([]);
});
it('returns the correct changes when both lists are empty', () => {
const changes = getChangesOnList([], []);
expect(changes.add).toEqual([]);
expect(changes.update).toEqual([]);
expect(changes.remove).toEqual([]);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment