summaryrefslogtreecommitdiffstats
path: root/devtools/shared/network-observer/test/browser/browser_networkobserver_invalid_constructor.js
blob: 76a93d938a2895114a72e02a4b356f334a978989 (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check that the NetworkObserver constructor validates its arguments.
add_task(async function testInvalidConstructorArguments() {
  Assert.throws(
    () => new NetworkObserver(),
    /Expected "ignoreChannelFunction" to be a function, got undefined/,
    "NetworkObserver constructor should throw if no argument was provided"
  );

  Assert.throws(
    () => new NetworkObserver({}),
    /Expected "ignoreChannelFunction" to be a function, got undefined/,
    "NetworkObserver constructor should throw if ignoreChannelFunction was not provided"
  );

  const invalidValues = [null, true, false, 12, "str", ["arr"], { obj: "obj" }];
  for (const invalidValue of invalidValues) {
    Assert.throws(
      () => new NetworkObserver({ ignoreChannelFunction: invalidValue }),
      /Expected "ignoreChannelFunction" to be a function, got/,
      `NetworkObserver constructor should throw if a(n) ${typeof invalidValue} was provided for ignoreChannelFunction`
    );
  }

  const EMPTY_FN = () => {};
  Assert.throws(
    () => new NetworkObserver({ ignoreChannelFunction: EMPTY_FN }),
    /Expected "onNetworkEvent" to be a function, got undefined/,
    "NetworkObserver constructor should throw if onNetworkEvent was not provided"
  );

  // Now we will pass a function for `ignoreChannelFunction`, and will do the
  // same tests for onNetworkEvent
  for (const invalidValue of invalidValues) {
    Assert.throws(
      () =>
        new NetworkObserver({
          ignoreChannelFunction: EMPTY_FN,
          onNetworkEvent: invalidValue,
        }),
      /Expected "onNetworkEvent" to be a function, got/,
      `NetworkObserver constructor should throw if a(n) ${typeof invalidValue} was provided for onNetworkEvent`
    );
  }
});