summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/reducers/tests/sources.spec.js
blob: d0436b6f76db3fa8f0f801dfeb6c08d6fdacb944 (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
/* 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/>. */

// @flow
declare var describe: (name: string, func: () => void) => void;
declare var it: (desc: string, func: () => void) => void;
declare var expect: (value: any) => any;

import update, { initialSourcesState, getDisplayedSources } from "../sources";
import { initialThreadsState } from "../threads";
import updateSourceActors from "../source-actors";
import type { SourceActor } from "../../types";
import { prefs } from "../../utils/prefs";
import { makeMockSource, mockcx, makeMockState } from "../../utils/test-mockup";
import { getResourceIds } from "../../utils/resource";

const extensionSource = {
  ...makeMockSource(),
  id: "extensionId",
  url: "http://example.com/script.js",
};

const firefoxExtensionSource = {
  ...makeMockSource(),
  id: "firefoxExtension",
  url: "moz-extension://id/js/content.js",
  isExtension: true,
};

const chromeExtensionSource = {
  ...makeMockSource(),
  id: "chromeExtension",
  isExtension: true,
  url: "chrome-extension://id/js/content.js",
};

const mockedSources = [
  extensionSource,
  firefoxExtensionSource,
  chromeExtensionSource,
];

const mockSourceActors: Array<SourceActor> = ([
  {
    id: "extensionId-actor",
    actor: "extensionId-actor",
    source: "extensionId",
    thread: "foo",
  },
  {
    id: "firefoxExtension-actor",
    actor: "firefoxExtension-actor",
    source: "firefoxExtension",
    thread: "foo",
  },
  {
    id: "chromeExtension-actor",
    actor: "chromeExtension-actor",
    source: "chromeExtension",
    thread: "foo",
  },
]: any);

describe("sources reducer", () => {
  it("should work", () => {
    let state = initialSourcesState();
    state = update(state, {
      type: "ADD_SOURCE",
      cx: mockcx,
      source: makeMockSource(),
    });
    expect(getResourceIds(state.sources)).toHaveLength(1);
  });
});

describe("sources selectors", () => {
  it("should return all extensions when chrome preference enabled", () => {
    prefs.chromeAndExtensionsEnabled = true;
    let state = initialSourcesState();
    state = {
      sources: update(state, {
        type: "ADD_SOURCES",
        cx: mockcx,
        sources: mockedSources,
      }),
      sourceActors: undefined,
    };
    const insertAction = {
      type: "INSERT_SOURCE_ACTORS",
      items: mockSourceActors,
    };
    state = makeMockState({
      sources: update(state.sources, insertAction),
      sourceActors: updateSourceActors(state.sourceActors, insertAction),
      threads: initialThreadsState(),
    });
    const threadSources = getDisplayedSources(state);
    expect(Object.values(threadSources.foo)).toHaveLength(3);
  });

  it("should omit all extensions when chrome preference enabled", () => {
    prefs.chromeAndExtensionsEnabled = false;
    let state = initialSourcesState();
    state = {
      sources: update(state, {
        type: "ADD_SOURCES",
        cx: mockcx,
        sources: mockedSources,
      }),
      sourceActors: undefined,
    };

    const insertAction = {
      type: "INSERT_SOURCE_ACTORS",
      items: mockSourceActors,
    };

    state = makeMockState({
      sources: update(state.sources, insertAction),
      sourceActors: updateSourceActors(state.sourceActors, insertAction),
      threads: initialThreadsState(),
    });
    const threadSources = getDisplayedSources(state);
    expect(Object.values(threadSources.foo)).toHaveLength(1);
  });
});