summaryrefslogtreecommitdiff
path: root/tst/collections_jsonds.test.ts
blob: 17b23a68b282f8c116618fced47ba75a79e7bb54 (plain) (blame)
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
import { JSONHashMap, JSONSet } from '../lib/index';

describe('types/collections/jsonds', () => {
    test('JSONSet uses stable JSON identity', () => {
        const set = new JSONSet<{ a: number; b: number }>();
        set.add({ a: 1, b: 2 });

        expect(set.has({ b: 2, a: 1 })).toBe(true);
        expect(set.size()).toBe(1);

        expect(set.delete({ b: 2, a: 1 })).toBe(true);
        expect(set.has({ a: 1, b: 2 })).toBe(false);

        set.add({ a: 1, b: 2 });
        set.clear();
        expect(set.size()).toBe(0);
    });

    test('JSONHashMap stable keying and keys()', () => {
        const map = new JSONHashMap<{ a: number; b: number }, { v: string }>();

        map.set({ a: 1, b: 2 }, { v: 'x' });
        expect(map.get({ b: 2, a: 1 })).toEqual({ v: 'x' });
        expect(map.has({ b: 2, a: 1 })).toBe(true);
        expect(map.size()).toBe(1);

        const keys = map.keys();
        expect(keys).toEqual([{ a: 1, b: 2 }]);

        expect(map.delete({ b: 2, a: 1 })).toBe(true);
        expect(map.size()).toBe(0);

        map.set({ a: 1, b: 2 }, { v: 'x' });
        map.clear();
        expect(map.size()).toBe(0);
    });
});