summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/node/components/webconsole-wrapper.test.js
blob: b750d43ab04ca66abcd49783166b594a9b7f14e1 (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
/* 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/. */

"use strict";

// Test utils.
const expect = require("expect");
const {
  stubPackets,
} = require("devtools/client/webconsole/test/node/fixtures/stubs/index");
const {
  clonePacket,
  getMessageAt,
  getPrivatePacket,
  getWebConsoleUiMock,
} = require("devtools/client/webconsole/test/node/helpers");

const WebConsoleWrapper = require("devtools/client/webconsole/webconsole-wrapper");
const { messagesAdd } = require("devtools/client/webconsole/actions/messages");

async function getWebConsoleWrapper() {
  const hud = {
    currentTarget: { client: {}, getFront: () => {} },
    getMappedExpression: () => {},
  };
  const webConsoleUi = getWebConsoleUiMock(hud);

  const wcow = new WebConsoleWrapper(null, webConsoleUi, null, null);
  await wcow.init();
  return wcow;
}

describe("WebConsoleWrapper", () => {
  it("clears queues when dispatchMessagesClear is called", async () => {
    const ncow = await getWebConsoleWrapper();
    ncow.queuedMessageAdds.push({ fakePacket: "message" });
    ncow.queuedMessageUpdates.push({ fakePacket: "message-update" });
    ncow.queuedRequestUpdates.push({ fakePacket: "request-update" });

    ncow.dispatchMessagesClear();

    expect(ncow.queuedMessageAdds.length).toBe(0);
    expect(ncow.queuedMessageUpdates.length).toBe(0);
    expect(ncow.queuedRequestUpdates.length).toBe(0);
  });

  it("removes private packets from message queue on dispatchPrivateMessagesClear", async () => {
    const ncow = await getWebConsoleWrapper();

    const publicLog = stubPackets.get("console.log('mymap')");
    ncow.queuedMessageAdds.push(
      getPrivatePacket("console.trace()"),
      publicLog,
      getPrivatePacket("XHR POST request")
    );

    ncow.dispatchPrivateMessagesClear();
    expect(ncow.queuedMessageAdds).toEqual([publicLog]);
  });

  it("removes private packets from network update queue on dispatchPrivateMessagesClear", async () => {
    const ncow = await getWebConsoleWrapper();
    const publicLog = stubPackets.get("console.log('mymap')");
    ncow.queuedMessageAdds.push(
      getPrivatePacket("console.trace()"),
      publicLog,
      getPrivatePacket("XHR POST request")
    );

    const postId = "pid1";
    const getId = "gid1";

    // Add messages in the store to make sure that update to private requests are
    // removed from the queue.
    ncow
      .getStore()
      .dispatch(
        messagesAdd([
          stubPackets.get("GET request"),
          { ...getPrivatePacket("XHR GET request"), actor: getId },
        ])
      );

    // Add packet to the message queue to make sure that update to private requests are
    // removed from the queue.
    ncow.queuedMessageAdds.push({
      ...getPrivatePacket("XHR POST request"),
      actor: postId,
    });

    const publicNetworkUpdate = stubPackets.get("GET request update");
    ncow.queuedMessageUpdates.push(
      publicNetworkUpdate,
      {
        ...getPrivatePacket("XHR GET request update"),
        actor: getId,
      },
      {
        ...getPrivatePacket("XHR POST request update"),
        actor: postId,
      }
    );

    ncow.dispatchPrivateMessagesClear();

    expect(ncow.queuedMessageUpdates.length).toBe(1);
    expect(ncow.queuedMessageUpdates).toEqual([publicNetworkUpdate]);
  });

  it("removes private packets from network request queue on dispatchPrivateMessagesClear", async () => {
    const ncow = await getWebConsoleWrapper();

    const packet1 = clonePacket(stubPackets.get("GET request"));
    const packet2 = clonePacket(getPrivatePacket("XHR GET request"));
    const packet3 = clonePacket(getPrivatePacket("XHR POST request"));

    // We need to reassign the timeStamp of the packet to guarantee the order.
    packet1.timeStamp = packet1.timeStamp + 1;
    packet2.timeStamp = packet2.timeStamp + 2;
    packet3.timeStamp = packet3.timeStamp + 3;

    ncow.getStore().dispatch(messagesAdd([packet1, packet2, packet3]));

    const state = ncow.getStore().getState();
    const publicId = getMessageAt(state, 0).id;
    const privateXhrGetId = getMessageAt(state, 1).id;
    const privateXhrPostId = getMessageAt(state, 2).id;
    ncow.queuedRequestUpdates.push(
      { id: publicId },
      { id: privateXhrGetId },
      { id: privateXhrPostId }
    );
    // ncow.queuedRequestUpdates.push({fakePacket: "request-update"});

    ncow.dispatchPrivateMessagesClear();

    expect(ncow.queuedRequestUpdates.length).toBe(1);
    expect(ncow.queuedRequestUpdates).toEqual([{ id: publicId }]);
  });
});