summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/utils/tests/utils.spec.js
blob: f358ffc42a7184214ae98048a2f0ade17baca1bb (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
/* 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 { handleError, promisify, endTruncateStr, waitForMs } from "../utils";

describe("handleError()", () => {
  const testErrorText = "ERROR: ";
  const testErrorObject = { oh: "noes" };

  beforeEach(() => {
    global.console = { log: jest.fn() };
  });

  it("logs error text with error value", () => {
    handleError(testErrorObject);

    expect(console.log).toHaveBeenCalledWith(testErrorText, testErrorObject);
  });
});

describe("promisify()", () => {
  let testPromise, testContext, testMethod, testArgs;

  beforeEach(() => {
    testContext = {};
    testMethod = jest.fn();
    testArgs = [];
  });

  it("returns a Promise", () => {
    testPromise = promisify(testContext, testMethod, testArgs);

    expect(testPromise instanceof Promise).toBe(true);
  });

  it("applies promisified method", () => {
    testPromise = promisify(testContext, testMethod, testArgs);

    expect(testMethod).toHaveBeenCalledWith(testArgs, expect.anything());
  });
});

describe("endTruncateStr()", () => {
  let testString;
  const testSize = 11;

  describe("when the string is larger than the specified size", () => {
    it("returns an elipsis and characters at the end of the string", () => {
      testString = "Mozilla Firefox is my favorite web browser";

      expect(endTruncateStr(testString, testSize)).toBe("…web browser");
    });
  });

  describe("when the string is not larger than the specified size", () => {
    it("returns the string unchanged", () => {
      testString = "Firefox";

      expect(endTruncateStr(testString, testSize)).toBe(testString);
    });
  });
});

describe("waitForMs()", () => {
  let testPromise;
  const testMilliseconds = 10;

  beforeEach(() => {
    global.setTimeout = jest.fn();
  });

  it("returns a Promise", () => {
    testPromise = waitForMs(testMilliseconds);

    expect(testPromise instanceof Promise).toBe(true);
  });

  it("calls setTimeout() on the resolve of the Promise", () => {
    testPromise = waitForMs(testMilliseconds);

    expect(setTimeout).toHaveBeenCalledWith(
      expect.anything(),
      testMilliseconds
    );
  });
});