summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/tests/expressions.spec.js
blob: c69276fa402a057cda180d0b30024e40c726526c (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
/* 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 { actions, selectors, createStore } from "../../utils/test-head";

const mockThreadFront = {
  evaluate: (script, { frameId }) =>
    new Promise((resolve, reject) => {
      if (!frameId) {
        resolve("bla");
      } else {
        resolve("boo");
      }
    }),
  evaluateExpressions: (inputs, { frameId }) =>
    Promise.all(
      inputs.map(
        input =>
          new Promise((resolve, reject) => {
            if (!frameId) {
              resolve("bla");
            } else {
              resolve("boo");
            }
          })
      )
    ),
  getFrameScopes: async () => {},
  getFrames: async () => [],
  sourceContents: () => ({ source: "", contentType: "text/javascript" }),
  getSourceActorBreakpointPositions: async () => ({}),
  getSourceActorBreakableLines: async () => [],
  autocomplete: () => {
    return new Promise(resolve => {
      resolve({
        from: "foo",
        matches: ["toLocaleString", "toSource", "toString", "toolbar", "top"],
        matchProp: "to",
      });
    });
  },
};

describe("expressions", () => {
  it("should add an expression", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);

    await dispatch(actions.addExpression("foo"));
    expect(selectors.getExpressions(getState())).toHaveLength(1);
  });

  it("should not add empty expressions", () => {
    const { dispatch, getState } = createStore(mockThreadFront);

    dispatch(actions.addExpression(undefined));
    dispatch(actions.addExpression(""));
    expect(selectors.getExpressions(getState())).toHaveLength(0);
  });

  it("should add invalid expressions", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);
    await dispatch(actions.addExpression("foo#"));
    const state = getState();
    expect(selectors.getExpressions(state)).toHaveLength(1);
  });

  it("should update an expression", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);

    await dispatch(actions.addExpression("foo"));
    const expression = selectors.getExpression(getState(), "foo");
    if (!expression) {
      throw new Error("expression must exist");
    }

    await dispatch(actions.updateExpression("bar", expression));
    const bar = selectors.getExpression(getState(), "bar");

    expect(bar && bar.input).toBe("bar");
  });

  it("should not update an expression w/ invalid code", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);

    await dispatch(actions.addExpression("foo"));
    const expression = selectors.getExpression(getState(), "foo");
    if (!expression) {
      throw new Error("expression must exist");
    }
    await dispatch(actions.updateExpression("#bar", expression));
    expect(selectors.getExpression(getState(), "bar")).toBeUndefined();
  });

  it("should delete an expression", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);

    await dispatch(actions.addExpression("foo"));
    await dispatch(actions.addExpression("bar"));
    expect(selectors.getExpressions(getState())).toHaveLength(2);

    const expression = selectors.getExpression(getState(), "foo");

    if (!expression) {
      throw new Error("expression must exist");
    }

    const bar = selectors.getExpression(getState(), "bar");
    dispatch(actions.deleteExpression(expression));
    expect(selectors.getExpressions(getState())).toHaveLength(1);
    expect(bar && bar.input).toBe("bar");
  });

  it("should evaluate expressions global scope", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);
    await dispatch(actions.addExpression("foo"));
    await dispatch(actions.addExpression("bar"));

    let foo = selectors.getExpression(getState(), "foo");
    let bar = selectors.getExpression(getState(), "bar");
    expect(foo && foo.value).toBe("bla");
    expect(bar && bar.value).toBe("bla");

    await dispatch(actions.evaluateExpressions(null));
    foo = selectors.getExpression(getState(), "foo");
    bar = selectors.getExpression(getState(), "bar");
    expect(foo && foo.value).toBe("bla");
    expect(bar && bar.value).toBe("bla");
  });

  it("should get the autocomplete matches for the input", async () => {
    const { dispatch, getState } = createStore(mockThreadFront);
    await dispatch(actions.autocomplete("to", 2));
    expect(selectors.getAutocompleteMatchset(getState())).toMatchSnapshot();
  });
});