summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/tests/memoize.spec.js
blob: fb729585163e5979cb47ea0f9f3af7df6a274f6c (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at <http://mozilla.org/MPL/2.0/>. */

import memoize from "../memoize";

const a = { number: 3 };
const b = { number: 4 };
const c = { number: 5 };
const d = { number: 6 };

function add(...numberObjects) {
  return numberObjects.reduce((prev, cur) => prev + cur.number, 0);
}

describe("memozie", () => {
  it("should work for one arg as key", () => {
    const mAdd = memoize(add);
    mAdd(a);
    expect(mAdd(a)).toEqual(3);
    mAdd(b);
    expect(mAdd(b)).toEqual(4);
  });

  it("should only be called once", () => {
    const spy = jest.fn(() => 2);
    const mAdd = memoize(spy);

    mAdd(a);
    mAdd(a);
    mAdd(a);
    expect(spy).toHaveBeenCalledTimes(1);
  });

  it("should work for two args as key", () => {
    const mAdd = memoize(add);
    expect(mAdd(a, b)).toEqual(7);
    expect(mAdd(a, b)).toEqual(7);
    expect(mAdd(a, c)).toEqual(8);
  });

  it("should work with many args as key", () => {
    const mAdd = memoize(add);
    expect(mAdd(a, b, c)).toEqual(12);
    expect(mAdd(a, b, d)).toEqual(13);
    expect(mAdd(a, b, c)).toEqual(12);
  });
});