-
-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathaccount-ordering.test.ts
More file actions
45 lines (36 loc) · 1.74 KB
/
Copy pathaccount-ordering.test.ts
File metadata and controls
45 lines (36 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { describe, it, expect } from 'vitest';
import { sortDefaultFirst, reorderNonDefaultIds, type OrderableAccount } from '../account-utils';
const acct = (id: string, isDefault = false): OrderableAccount => ({ id, isDefault });
describe('sortDefaultFirst', () => {
it('pins the default account to the front, preserving the rest order', () => {
const accounts = [acct('a'), acct('b', true), acct('c')];
expect(sortDefaultFirst(accounts).map((a) => a.id)).toEqual(['b', 'a', 'c']);
});
it('is a no-op shape when the default is already first', () => {
const accounts = [acct('b', true), acct('a'), acct('c')];
expect(sortDefaultFirst(accounts).map((a) => a.id)).toEqual(['b', 'a', 'c']);
});
it('does not mutate the input array', () => {
const accounts = [acct('a'), acct('b', true)];
const snapshot = accounts.map((a) => a.id);
sortDefaultFirst(accounts);
expect(accounts.map((a) => a.id)).toEqual(snapshot);
});
});
describe('reorderNonDefaultIds', () => {
// default 'd' stays index 0; non-defaults are a, b, c
const accounts = [acct('d', true), acct('a'), acct('b'), acct('c')];
it('moves a non-default onto a later position, keeping default pinned', () => {
expect(reorderNonDefaultIds(accounts, 'a', 'c')).toEqual(['d', 'b', 'c', 'a']);
});
it('moves a non-default earlier', () => {
expect(reorderNonDefaultIds(accounts, 'c', 'a')).toEqual(['d', 'c', 'a', 'b']);
});
it('returns null for a no-op (same id)', () => {
expect(reorderNonDefaultIds(accounts, 'a', 'a')).toBeNull();
});
it('returns null when the default is dragged or targeted', () => {
expect(reorderNonDefaultIds(accounts, 'd', 'a')).toBeNull();
expect(reorderNonDefaultIds(accounts, 'a', 'd')).toBeNull();
});
});