summaryrefslogtreecommitdiff
path: root/spec/frontend/lib/utils/recurrence_spec.js
blob: 8bf3ea4e25a434905dce2b7f59ad108962a3b30f (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
import { create, free, recall } from '~/lib/utils/recurrence';

const HEX = /[a-f0-9]/i;
const HEX_RE = HEX.source;
const UUIDV4 = new RegExp(
  `${HEX_RE}{8}-${HEX_RE}{4}-4${HEX_RE}{3}-[89ab]${HEX_RE}{3}-${HEX_RE}{12}`,
  'i',
);

describe('recurrence', () => {
  let recurInstance;
  let id;

  beforeEach(() => {
    recurInstance = create();
    id = recurInstance.id;
  });

  afterEach(() => {
    id = null;
    recurInstance.free();
  });

  describe('create', () => {
    it('returns an object with the correct external api', () => {
      expect(recurInstance).toMatchObject(
        expect.objectContaining({
          id: expect.stringMatching(UUIDV4),
          count: 0,
          handlers: {},
          free: expect.any(Function),
          handle: expect.any(Function),
          eject: expect.any(Function),
          occur: expect.any(Function),
          reset: expect.any(Function),
        }),
      );
    });
  });

  describe('recall', () => {
    it('returns a previously created RecurInstance', () => {
      expect(recall(id).id).toBe(id);
    });

    it("returns undefined if the provided UUID doesn't refer to a stored RecurInstance", () => {
      expect(recall('1234')).toBeUndefined();
    });
  });

  describe('free', () => {
    it('returns true when the RecurInstance exists', () => {
      expect(free(id)).toBe(true);
    });

    it("returns false when the ID doesn't refer to a known RecurInstance", () => {
      expect(free('1234')).toBe(false);
    });

    it('removes the correct RecurInstance from the list of references', () => {
      const anotherInstance = create();

      expect(recall(id)).toEqual(recurInstance);
      expect(recall(anotherInstance.id)).toEqual(anotherInstance);

      free(id);

      expect(recall(id)).toBeUndefined();
      expect(recall(anotherInstance.id)).toEqual(anotherInstance);

      anotherInstance.free();
    });
  });

  describe('RecurInstance (`create()` return value)', () => {
    it.each`
      property      | value                            | alias
      ${'id'}       | ${expect.stringMatching(UUIDV4)} | ${'[a string matching the UUIDv4 specification]'}
      ${'count'}    | ${0}                             | ${0}
      ${'handlers'} | ${{}}                            | ${{}}
    `(
      'has the correct primitive value $alias for the member `$property` to start',
      ({ property, value }) => {
        expect(recurInstance[property]).toEqual(value);
      },
    );

    describe('id', () => {
      it('cannot be changed manually', () => {
        expect(() => {
          recurInstance.id = 'new-id';
        }).toThrow(TypeError);

        expect(recurInstance.id).toBe(id);
      });

      it.each`
        method
        ${'free'}
        ${'handle'}
        ${'eject'}
        ${'occur'}
        ${'reset'}
      `('does not change across any method call - like after `$method`', ({ method }) => {
        recurInstance[method]();

        expect(recurInstance.id).toBe(id);
      });
    });

    describe('count', () => {
      it('cannot be changed manually', () => {
        expect(() => {
          recurInstance.count = 9999;
        }).toThrow(TypeError);

        expect(recurInstance.count).toBe(0);
      });

      it.each`
        method
        ${'free'}
        ${'handle'}
        ${'eject'}
        ${'reset'}
      `("doesn't change in unexpected scenarios - like after a call to `$method`", ({ method }) => {
        recurInstance[method]();

        expect(recurInstance.count).toBe(0);
      });

      it('increments by one each time `.occur()` is called', () => {
        expect(recurInstance.count).toBe(0);
        recurInstance.occur();
        expect(recurInstance.count).toBe(1);
        recurInstance.occur();
        expect(recurInstance.count).toBe(2);
      });
    });

    describe('handlers', () => {
      it('cannot be changed manually', () => {
        const fn = jest.fn();

        recurInstance.handle(1, fn);
        expect(() => {
          recurInstance.handlers = {};
        }).toThrow(TypeError);

        expect(recurInstance.handlers).toStrictEqual({
          1: fn,
        });
      });

      it.each`
        method
        ${'free'}
        ${'occur'}
        ${'eject'}
        ${'reset'}
      `("doesn't change in unexpected scenarios - like after a call to `$method`", ({ method }) => {
        recurInstance[method]();

        expect(recurInstance.handlers).toEqual({});
      });

      it('adds handlers to the correct slots', () => {
        const fn1 = jest.fn();
        const fn2 = jest.fn();

        recurInstance.handle(100, fn1);
        recurInstance.handle(1000, fn2);

        expect(recurInstance.handlers).toMatchObject({
          100: fn1,
          1000: fn2,
        });
      });
    });

    describe('free', () => {
      it('removes itself from recallable memory', () => {
        expect(recall(id)).toEqual(recurInstance);

        recurInstance.free();

        expect(recall(id)).toBeUndefined();
      });
    });

    describe('handle', () => {
      it('adds a handler for the provided count', () => {
        const fn = jest.fn();

        recurInstance.handle(5, fn);

        expect(recurInstance.handlers[5]).toEqual(fn);
      });

      it("doesn't add any handlers if either the count or behavior aren't provided", () => {
        const fn = jest.fn();

        recurInstance.handle(null, fn);
        // Note that it's not possible to react to something not happening (without timers)
        recurInstance.handle(0, fn);
        recurInstance.handle(5, null);

        expect(recurInstance.handlers).toEqual({});
      });
    });

    describe('eject', () => {
      it('removes the handler assigned to the particular count slot', () => {
        const func = jest.fn();
        recurInstance.handle(1, func);

        expect(recurInstance.handlers[1]).toStrictEqual(func);

        recurInstance.eject(1);

        expect(recurInstance.handlers).toEqual({});
      });

      it("succeeds (or fails gracefully) when the count provided doesn't have a handler assigned", () => {
        recurInstance.eject('abc');
        recurInstance.eject(1);

        expect(recurInstance.handlers).toEqual({});
      });

      it('makes no changes if no count is provided', () => {
        const fn = jest.fn();

        recurInstance.handle(1, fn);

        recurInstance.eject();

        expect(recurInstance.handlers[1]).toStrictEqual(fn);
      });
    });

    describe('occur', () => {
      it('increments the .count property by 1', () => {
        expect(recurInstance.count).toBe(0);

        recurInstance.occur();

        expect(recurInstance.count).toBe(1);
      });

      it('calls the appropriate handlers', () => {
        const fn1 = jest.fn();
        const fn5 = jest.fn();
        const fn10 = jest.fn();

        recurInstance.handle(1, fn1);
        recurInstance.handle(5, fn5);
        recurInstance.handle(10, fn10);

        expect(fn1).not.toHaveBeenCalled();
        expect(fn5).not.toHaveBeenCalled();
        expect(fn10).not.toHaveBeenCalled();

        recurInstance.occur();

        expect(fn1).toHaveBeenCalledTimes(1);
        expect(fn5).not.toHaveBeenCalled();
        expect(fn10).not.toHaveBeenCalled();

        recurInstance.occur();
        recurInstance.occur();
        recurInstance.occur();
        recurInstance.occur();

        expect(fn1).toHaveBeenCalledTimes(1);
        expect(fn5).toHaveBeenCalledTimes(1);
        expect(fn10).not.toHaveBeenCalled();

        recurInstance.occur();
        recurInstance.occur();
        recurInstance.occur();
        recurInstance.occur();
        recurInstance.occur();

        expect(fn1).toHaveBeenCalledTimes(1);
        expect(fn5).toHaveBeenCalledTimes(1);
        expect(fn10).toHaveBeenCalledTimes(1);
      });
    });

    describe('reset', () => {
      it('resets the count only, by default', () => {
        const fn = jest.fn();

        recurInstance.handle(3, fn);
        recurInstance.occur();
        recurInstance.occur();

        expect(recurInstance.count).toBe(2);

        recurInstance.reset();

        expect(recurInstance.count).toBe(0);
        expect(recurInstance.handlers).toEqual({ 3: fn });
      });

      it('also resets the handlers, by specific request', () => {
        const fn = jest.fn();

        recurInstance.handle(3, fn);
        recurInstance.occur();
        recurInstance.occur();

        expect(recurInstance.count).toBe(2);

        recurInstance.reset({ handlersList: true });

        expect(recurInstance.count).toBe(0);
        expect(recurInstance.handlers).toEqual({});
      });

      it('leaves the count in place, by request', () => {
        recurInstance.occur();
        recurInstance.occur();

        expect(recurInstance.count).toBe(2);

        recurInstance.reset({ currentCount: false });

        expect(recurInstance.count).toBe(2);
      });
    });
  });
});