File size: 3,335 Bytes
8a37e0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { describe, expect, it, vi } from 'vitest';

import { SyncableMap } from './SyncableMap';

describe('SyncableMap', () => {
  it('should initialize with entries', () => {
    const initialEntries = [
      ['key1', 'value1'],
      ['key2', 'value2'],
    ] as const;
    const map = new SyncableMap(initialEntries);
    expect(map.size).toBe(2);
    expect(map.get('key1')).toBe('value1');
    expect(map.get('key2')).toBe('value2');
  });

  it('should notify subscribers when a key is set', () => {
    const map = new SyncableMap<string, string>();
    const subscriber = vi.fn();
    map.subscribe(subscriber);

    map.set('key1', 'value1');

    expect(subscriber).toHaveBeenCalledTimes(1);
    expect(map.get('key1')).toBe('value1');
  });

  it('should notify subscribers when a key is deleted', () => {
    const map = new SyncableMap<string, string>([['key1', 'value1']]);
    const subscriber = vi.fn();
    map.subscribe(subscriber);

    map.delete('key1');

    expect(subscriber).toHaveBeenCalledTimes(1);
    expect(map.get('key1')).toBeUndefined();
  });

  it('should notify subscribers when the map is cleared', () => {
    const map = new SyncableMap<string, string>([
      ['key1', 'value1'],
      ['key2', 'value2'],
    ]);
    const subscriber = vi.fn();
    map.subscribe(subscriber);

    map.clear();

    expect(subscriber).toHaveBeenCalledTimes(1);
    expect(map.size).toBe(0);
  });

  it('should not notify unsubscribed callbacks', () => {
    const map = new SyncableMap<string, string>();
    const subscriber = vi.fn();
    const unsubscribe = map.subscribe(subscriber);

    unsubscribe();

    map.set('key1', 'value1');

    expect(subscriber).not.toHaveBeenCalled();
  });

  it('should return a snapshot of the current state', () => {
    const map = new SyncableMap<string, string>([['key1', 'value1']]);

    const snapshot = map.getSnapshot();

    expect(snapshot.size).toBe(1);
    expect(snapshot.get('key1')).toBe('value1');
  });

  it('should return the same snapshot if there were no changes', () => {
    const map = new SyncableMap<string, string>([['key1', 'value1']]);

    const firstSnapshot = map.getSnapshot();
    const secondSnapshot = map.getSnapshot();

    expect(firstSnapshot).toBe(secondSnapshot);
  });

  it('should return a new snapshot if changes were made', () => {
    const map = new SyncableMap<string, string>([['key1', 'value1']]);

    const firstSnapshot = map.getSnapshot();
    map.set('key2', 'value2');
    const secondSnapshot = map.getSnapshot();

    expect(firstSnapshot).not.toBe(secondSnapshot);
    expect(secondSnapshot.size).toBe(2);
  });

  it('should consider different snapshots unequal', () => {
    const map = new SyncableMap<string, string>([['key1', 'value1']]);

    const firstSnapshot = map.getSnapshot();
    map.set('key2', 'value2');
    const secondSnapshot = map.getSnapshot();

    expect(map['areSnapshotsEqual'](firstSnapshot, secondSnapshot)).toBe(false);
  });

  it('should consider identical snapshots equal', () => {
    const map = new SyncableMap<string, string>([
      ['key1', 'value1'],
      ['key2', 'value2'],
    ]);

    const firstSnapshot = map.getSnapshot();
    const secondSnapshot = map.getSnapshot();

    expect(map['areSnapshotsEqual'](firstSnapshot, secondSnapshot)).toBe(true);
  });
});