summaryrefslogtreecommitdiffstats
path: root/devtools/client/debugger/src/actions/tests/preview.spec.js
blob: 4cb2542adfd227280c85477e6618f6c40a4e3865 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/* eslint max-nested-callbacks: ["error", 6] */
/* 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

import {
  createStore,
  selectors,
  actions,
  makeSource,
  makeFrame,
  waitForState,
  waitATick,
} from "../../utils/test-head";

function waitForPreview(store, expression) {
  return waitForState(store, state => {
    const preview = selectors.getPreview(state);
    return preview && preview.expression == expression;
  });
}

function mockThreadFront(overrides) {
  return {
    evaluateInFrame: async () => ({ result: {} }),
    getFrameScopes: async () => {},
    getFrames: async () => [],
    sourceContents: async () => ({
      source: "",
      contentType: "text/javascript",
    }),
    getSourceActorBreakpointPositions: async () => ({}),
    getSourceActorBreakableLines: async () => [],
    evaluateExpressions: async () => [],
    loadObjectProperties: async () => ({}),
    ...overrides,
  };
}

function dispatchSetPreview(dispatch, context, expression, target) {
  return dispatch(
    actions.setPreview(
      context,
      expression,
      {
        start: { url: "foo.js", line: 1, column: 2 },
        end: { url: "foo.js", line: 1, column: 5 },
      },
      { line: 2, column: 3 },
      target.getBoundingClientRect(),
      target
    )
  );
}

async function pause(store, client) {
  const { dispatch, cx } = store;
  const base = await dispatch(
    actions.newGeneratedSource(makeSource("base.js"))
  );

  await dispatch(actions.selectSource(cx, base.id));
  await waitForState(store, state => selectors.hasSymbols(state, base));

  const { thread } = cx;
  const frames = [makeFrame({ id: "frame1", sourceId: base.id, thread })];
  client.getFrames = async () => frames;

  await dispatch(
    actions.paused({
      thread,
      frame: frames[0],
      loadedObjects: [],
      why: { type: "debuggerStatement" },
    })
  );
}

describe("preview", () => {
  it("should generate previews", async () => {
    const store = createStore(mockThreadFront());
    const { dispatch, getState, cx } = store;
    const base = await dispatch(
      actions.newGeneratedSource(makeSource("base.js"))
    );

    await dispatch(actions.selectSource(cx, base.id));
    await waitForState(store, state => selectors.hasSymbols(state, base));
    const frames = [makeFrame({ id: "f1", sourceId: base.id })];

    await dispatch(
      actions.paused({
        thread: store.cx.thread,
        frame: frames[0],
        frames,
        loadedObjects: [],
        why: { type: "debuggerStatement" },
      })
    );

    const newCx = selectors.getContext(getState());
    const firstTarget = document.createElement("div");

    dispatchSetPreview(dispatch, newCx, "foo", firstTarget);

    expect(selectors.getPreview(getState())).toMatchSnapshot();
  });

  // When a 2nd setPreview is called before a 1st setPreview dispatches
  // and the 2nd setPreview has not dispatched yet,
  // the first setPreview should not finish dispatching
  it("queued previews (w/ the 1st finishing first)", async () => {
    let resolveFirst, resolveSecond;
    const promises = [
      new Promise(resolve => {
        resolveFirst = resolve;
      }),
      new Promise(resolve => {
        resolveSecond = resolve;
      }),
    ];

    const client = mockThreadFront({
      loadObjectProperties: () => promises.shift(),
    });
    const store = createStore(client);

    const { dispatch, getState } = store;
    await pause(store, client);

    const newCx = selectors.getContext(getState());
    const firstTarget = document.createElement("div");
    const secondTarget = document.createElement("div");

    // Start the dispatch of the first setPreview. At this point, it will not
    // finish execution until we resolve the firstSetPreview
    dispatchSetPreview(dispatch, newCx, "firstSetPreview", firstTarget);

    // Start the dispatch of the second setPreview. At this point, it will not
    // finish execution until we resolve the secondSetPreview
    dispatchSetPreview(dispatch, newCx, "secondSetPreview", secondTarget);

    let fail = false;

    /* $FlowIgnore[not-a-function] this is guarantied to be initialized because
       `new new Promise(foo)` calls foo synchronously */
    resolveFirst();
    waitForPreview(store, "firstSetPreview").then(() => {
      fail = true;
    });

    // $FlowIgnore[not-a-function] same as above
    resolveSecond();
    await waitForPreview(store, "secondSetPreview");
    expect(fail).toEqual(false);

    const preview = selectors.getPreview(getState());
    expect(preview && preview.expression).toEqual("secondSetPreview");
  });

  // When a 2nd setPreview is called before a 1st setPreview dispatches
  // and the 2nd setPreview has dispatched,
  // the first setPreview should not finish dispatching
  it("queued previews (w/ the 2nd finishing first)", async () => {
    let resolveFirst, resolveSecond;
    const promises = [
      new Promise(resolve => {
        resolveFirst = resolve;
      }),
      new Promise(resolve => {
        resolveSecond = resolve;
      }),
    ];

    const client = mockThreadFront({
      loadObjectProperties: () => promises.shift(),
    });
    const store = createStore(client);

    const { dispatch, getState } = store;
    await pause(store, client);

    const cx = selectors.getThreadContext(getState());
    const firstTarget = document.createElement("div");
    const secondTarget = document.createElement("div");

    // Start the dispatch of the first setPreview. At this point, it will not
    // finish execution until we resolve the firstSetPreview
    dispatchSetPreview(dispatch, cx, "firstSetPreview", firstTarget);

    // Start the dispatch of the second setPreview. At this point, it will not
    // finish execution until we resolve the secondSetPreview
    dispatchSetPreview(dispatch, cx, "secondSetPreview", secondTarget);

    let fail = false;

    /* $FlowIgnore[not-a-function] this is guarantied to be initialized because
       `new new Promise(foo)` calls foo synchronously */
    resolveSecond();
    await waitForPreview(store, "secondSetPreview");

    // $FlowIgnore[not-a-function] same as above
    resolveFirst();
    waitForPreview(store, "firstSetPreview").then(() => {
      fail = true;
    });

    await waitATick(() => expect(fail).toEqual(false));

    const preview = selectors.getPreview(getState());
    expect(preview && preview.expression).toEqual("secondSetPreview");
  });
});