summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/new-console-output/test/mochitest/browser_webconsole_console_table.js
blob: 0aa544e13d07e0434746442415f5c5ef63690dee (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
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Check console.table calls with all the test cases shown
// in the MDN doc (https://developer.mozilla.org/en-US/docs/Web/API/Console/table)

const TEST_URI = "http://example.com/browser/devtools/client/webconsole/new-console-output/test/mochitest/test-console-table.html";

add_task(function* () {
  let toolbox = yield openNewTabAndToolbox(TEST_URI, "webconsole");
  let hud = toolbox.getCurrentPanel().hud;

  function Person(firstName, lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }

  const testCases = [{
    info: "Testing when data argument is an array",
    input: ["apples", "oranges", "bananas"],
    expected: {
      columns: ["(index)", "Values"],
      rows: [
        ["0", "apples"],
        ["1", "oranges"],
        ["2", "bananas"],
      ]
    }
  }, {
    info: "Testing when data argument is an object",
    input: new Person("John", "Smith"),
    expected: {
      columns: ["(index)", "Values"],
      rows: [
        ["firstName", "John"],
        ["lastName", "Smith"],
      ]
    }
  }, {
    info: "Testing when data argument is an array of arrays",
    input: [["Jane", "Doe"], ["Emily", "Jones"]],
    expected: {
      columns: ["(index)", "0", "1"],
      rows: [
        ["0", "Jane", "Doe"],
        ["1", "Emily", "Jones"],
      ]
    }
  }, {
    info: "Testing when data argument is an array of objects",
    input: [
      new Person("Jack", "Foo"),
      new Person("Emma", "Bar"),
      new Person("Michelle", "Rax"),
    ],
    expected: {
      columns: ["(index)", "firstName", "lastName"],
      rows: [
        ["0", "Jack", "Foo"],
        ["1", "Emma", "Bar"],
        ["2", "Michelle", "Rax"],
      ]
    }
  }, {
    info: "Testing when data argument is an object whose properties are objects",
    input: {
      father: new Person("Darth", "Vader"),
      daughter: new Person("Leia", "Organa"),
      son: new Person("Luke", "Skywalker"),
    },
    expected: {
      columns: ["(index)", "firstName", "lastName"],
      rows: [
        ["father", "Darth", "Vader"],
        ["daughter", "Leia", "Organa"],
        ["son", "Luke", "Skywalker"],
      ]
    }
  }, {
    info: "Testing when data argument is a Set",
    input: new Set(["a", "b", "c"]),
    expected: {
      columns: ["(iteration index)", "Values"],
      rows: [
        ["0", "a"],
        ["1", "b"],
        ["2", "c"],
      ]
    }
  }, {
    info: "Testing when data argument is a Map",
    input: new Map([["key-a", "value-a"], ["key-b", "value-b"]]),
    expected: {
      columns: ["(iteration index)", "Key", "Values"],
      rows: [
        ["0", "key-a", "value-a"],
        ["1", "key-b", "value-b"],
      ]
    }
  }, {
    info: "Testing restricting the columns displayed",
    input: [
      new Person("Sam", "Wright"),
      new Person("Elena", "Bartz"),
    ],
    headers: ["firstName"],
    expected: {
      columns: ["(index)", "firstName"],
      rows: [
        ["0", "Sam"],
        ["1", "Elena"],
      ]
    }
  }];

  yield ContentTask.spawn(gBrowser.selectedBrowser, testCases, function (tests) {
    tests.forEach((test) => {
      content.wrappedJSObject.doConsoleTable(test.input, test.headers);
    });
  });

  let nodes = [];
  for (let testCase of testCases) {
    let node = yield waitFor(
      () => findConsoleTable(hud.ui.experimentalOutputNode, testCases.indexOf(testCase))
    );
    nodes.push(node);
  }

  let consoleTableNodes = hud.ui.experimentalOutputNode.querySelectorAll(
    ".message .new-consoletable");

  is(consoleTableNodes.length, testCases.length,
    "console has the expected number of consoleTable items");

  testCases.forEach((testCase, index) => {
    info(testCase.info);

    let node = nodes[index];
    let columns = Array.from(node.querySelectorAll("thead th"));
    let rows = Array.from(node.querySelectorAll("tbody tr"));

    is(
      JSON.stringify(testCase.expected.columns),
      JSON.stringify(columns.map(column => column.textContent)),
      "table has the expected columns"
    );

    is(testCase.expected.rows.length, rows.length,
      "table has the expected number of rows");

    testCase.expected.rows.forEach((expectedRow, rowIndex) => {
      let row = rows[rowIndex];
      let cells = row.querySelectorAll("td");
      is(expectedRow.length, cells.length, "row has the expected number of cells");

      expectedRow.forEach((expectedCell, cellIndex) => {
        let cell = cells[cellIndex];
        is(expectedCell, cell.textContent, "cell has the expected content");
      });
    });
  });
});

function findConsoleTable(node, index) {
  let condition = node.querySelector(
    `.message:nth-of-type(${index + 1}) .new-consoletable`);
  return condition;
}