summaryrefslogtreecommitdiffstats
path: root/devtools/client/webconsole/test/node/components/page-error.test.js
blob: e2489a7f342cb5f8328d76fcf40d8ea7a4770d7b (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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";

// Test utils.
const expect = require("expect");
const { render, mount } = require("enzyme");
const sinon = require("sinon");

// React
const {
  createFactory,
} = require("resource://devtools/client/shared/vendor/react.js");
const Provider = createFactory(
  require("resource://devtools/client/shared/vendor/react-redux.js").Provider
);
const {
  formatErrorTextWithCausedBy,
  setupStore,
} = require("resource://devtools/client/webconsole/test/node/helpers.js");
const {
  prepareMessage,
} = require("resource://devtools/client/webconsole/utils/messages.js");

// Components under test.
const PageError = require("resource://devtools/client/webconsole/components/Output/message-types/PageError.js");
const {
  MESSAGE_OPEN,
  MESSAGE_CLOSE,
} = require("resource://devtools/client/webconsole/constants.js");
const {
  INDENT_WIDTH,
} = require("resource://devtools/client/webconsole/components/Output/MessageIndent.js");

// Test fakes.
const {
  stubPackets,
  stubPreparedMessages,
} = require("resource://devtools/client/webconsole/test/node/fixtures/stubs/index.js");
const serviceContainer = require("resource://devtools/client/webconsole/test/node/fixtures/serviceContainer.js");

describe("PageError component:", () => {
  it("renders", () => {
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const wrapper = render(
      PageError({
        message,
        serviceContainer,
        timestampsVisible: true,
      })
    );
    const {
      timestampString,
    } = require("resource://devtools/client/webconsole/utils/l10n.js");

    expect(wrapper.find(".timestamp").text()).toBe(
      timestampString(message.timeStamp)
    );

    expect(wrapper.find(".message-body").text()).toBe(
      "Uncaught ReferenceError: asdf is not defined[Learn More]"
    );

    // The stacktrace should be closed by default.
    const frameLinks = wrapper.find(`.stack-trace`);
    expect(frameLinks.length).toBe(0);

    // There should be the location.
    const locationLink = wrapper.find(`.message-location`);
    expect(locationLink.length).toBe(1);
    // @TODO Will likely change. See bug 1307952
    expect(locationLink.text()).toBe("test-console-api.html:3:5");
  });

  it("does not have a timestamp when timestampsVisible prop is falsy", () => {
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const wrapper = render(
      PageError({
        message,
        serviceContainer,
        timestampsVisible: false,
      })
    );

    expect(wrapper.find(".timestamp").length).toBe(0);
  });

  it("renders an error with a longString exception message", () => {
    const message = stubPreparedMessages.get("TypeError longString message");
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = wrapper.find(".message-body").text();
    expect(text.startsWith("Uncaught Error: Long error Long error")).toBe(true);
  });

  it("renders thrown empty string", () => {
    const message = stubPreparedMessages.get(`throw ""`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe("Uncaught <empty string>");
  });

  it("renders thrown string", () => {
    const message = stubPreparedMessages.get(`throw "tomato"`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught tomato`);
  });

  it("renders thrown boolean", () => {
    const message = stubPreparedMessages.get(`throw false`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught false`);
  });

  it("renders thrown number ", () => {
    const message = stubPreparedMessages.get(`throw 0`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught 0`);
  });

  it("renders thrown null", () => {
    const message = stubPreparedMessages.get(`throw null`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught null`);
  });

  it("renders thrown undefined", () => {
    const message = stubPreparedMessages.get(`throw undefined`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught undefined`);
  });

  it("renders thrown Symbol", () => {
    const message = stubPreparedMessages.get(`throw Symbol`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught Symbol("potato")`);
  });

  it("renders thrown object", () => {
    const message = stubPreparedMessages.get(`throw Object`);

    // We need to wrap the PageError in a Provider in order for the
    // ObjectInspector to work.
    const wrapper = render(
      Provider(
        { store: setupStore() },
        PageError({ message, serviceContainer })
      )
    );

    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught Object { vegetable: "cucumber" }`);
  });

  it("renders thrown error", () => {
    const message = stubPreparedMessages.get(`throw Error Object`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught Error: pumpkin`);
  });

  it("renders thrown Error with custom name", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with custom name`
    );
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught JuicyError: pineapple`);
  });

  it("renders thrown Error with error cause", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with error cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe(
      "Uncaught Error: something went wrong\nCaused by: SyntaxError: original error"
    );
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with error cause chain", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with cause chain`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe(
      [
        "Uncaught Error: err-d",
        "Caused by: Error: err-c",
        "Caused by: Error: err-b",
        "Caused by: Error: err-a",
      ].join("\n")
    );
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with cyclical cause chain", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with cyclical cause chain`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    // TODO: This is not how we should display cyclical cause chain, but we have it here
    // to ensure it's displaying something that makes _some_ sense.
    // This should be properly handled in Bug 1719605.
    expect(text).toBe(
      [
        "Uncaught Error: err-b",
        "Caused by: Error: err-a",
        "Caused by: Error: err-b",
        "Caused by: Error: err-a",
      ].join("\n")
    );
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with null cause", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with falsy cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe("Uncaught Error: null cause\nCaused by: null");
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with number cause", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with number cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe("Uncaught Error: number cause\nCaused by: 0");
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with string cause", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with string cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe(
      `Uncaught Error: string cause\nCaused by: "cause message"`
    );
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders thrown Error with object cause", () => {
    const message = stubPreparedMessages.get(
      `throw Error Object with object cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe("Uncaught Error: object cause\nCaused by: Object { … }");
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders uncaught rejected Promise with empty string", () => {
    const message = stubPreparedMessages.get(`Promise reject ""`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe("Uncaught (in promise) <empty string>");
  });

  it("renders uncaught rejected Promise with string", () => {
    const message = stubPreparedMessages.get(`Promise reject "tomato"`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) tomato`);
  });

  it("renders uncaught rejected Promise with boolean", () => {
    const message = stubPreparedMessages.get(`Promise reject false`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) false`);
  });

  it("renders uncaught rejected Promise with number ", () => {
    const message = stubPreparedMessages.get(`Promise reject 0`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) 0`);
  });

  it("renders uncaught rejected Promise with null", () => {
    const message = stubPreparedMessages.get(`Promise reject null`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) null`);
  });

  it("renders uncaught rejected Promise with undefined", () => {
    const message = stubPreparedMessages.get(`Promise reject undefined`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) undefined`);
  });

  it("renders uncaught rejected Promise with Symbol", () => {
    const message = stubPreparedMessages.get(`Promise reject Symbol`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) Symbol("potato")`);
  });

  it("renders uncaught rejected Promise with object", () => {
    const message = stubPreparedMessages.get(`Promise reject Object`);
    // We need to wrap the PageError in a Provider in order for the
    // ObjectInspector to work.
    const wrapper = render(
      Provider(
        { store: setupStore() },
        PageError({ message, serviceContainer })
      )
    );
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) Object { vegetable: "cucumber" }`);
  });

  it("renders uncaught rejected Promise with error", () => {
    const message = stubPreparedMessages.get(`Promise reject Error Object`);
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) Error: pumpkin`);
  });

  it("renders uncaught rejected Promise with Error with custom name", () => {
    const message = stubPreparedMessages.get(
      `Promise reject Error Object with custom name`
    );
    const wrapper = render(PageError({ message, serviceContainer }));
    const text = wrapper.find(".message-body").text();
    expect(text).toBe(`Uncaught (in promise) JuicyError: pineapple`);
  });

  it("renders uncaught rejected Promise with Error with cause", () => {
    const message = stubPreparedMessages.get(
      `Promise reject Error Object with error cause`
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = formatErrorTextWithCausedBy(
      wrapper.find(".message-body").text()
    );
    expect(text).toBe(
      [
        `Uncaught (in promise) Error: something went wrong`,
        `Caused by: ReferenceError: unknownFunc is not defined`,
      ].join("\n")
    );
    expect(wrapper.hasClass("error")).toBe(true);
  });

  it("renders URLs in message as actual, cropped, links", () => {
    // Let's replace the packet data in order to mimick a pageError.
    const packet = stubPackets.get("throw string with URL");

    const evilDomain = `https://evil.com/?`;
    const badDomain = `https://not-so-evil.com/?`;
    const paramLength = 200;
    const longParam = "a".repeat(paramLength);

    const evilURL = `${evilDomain}${longParam}`;
    const badURL = `${badDomain}${longParam}`;

    // We remove the exceptionDocURL to not have the "learn more" link.
    packet.pageError.exceptionDocURL = null;

    const message = prepareMessage(packet, { getNextId: () => "1" });
    const wrapper = render(PageError({ message, serviceContainer }));

    const text = wrapper.find(".message-body").text();
    expect(text).toBe(
      `Uncaught “${evilURL}“ is evil and “${badURL}“ is not good either`
    );

    // There should be 2 cropped links.
    const links = wrapper.find(".message-body a.cropped-url");
    expect(links.length).toBe(2);

    expect(links.eq(0).attr("href")).toBe(evilURL);
    expect(links.eq(0).attr("title")).toBe(evilURL);

    expect(links.eq(1).attr("href")).toBe(badURL);
    expect(links.eq(1).attr("title")).toBe(badURL);
  });

  it("displays a [Learn more] link", () => {
    const store = setupStore();

    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );

    serviceContainer.openLink = sinon.spy();
    const wrapper = mount(
      Provider(
        { store },
        PageError({
          message,
          serviceContainer,
          dispatch: () => {},
        })
      )
    );

    // There should be a [Learn more] link.
    const url =
      "https://developer.mozilla.org/docs/Web/JavaScript/Reference/Errors/Not_defined";
    const learnMore = wrapper.find(".learn-more-link");
    expect(learnMore.length).toBe(1);
    expect(learnMore.prop("title")).toBe(url);

    learnMore.simulate("click");
    const call = serviceContainer.openLink.getCall(0);
    expect(call.args[0]).toEqual(message.exceptionDocURL);
  });

  // Unskip will happen in Bug 1529548.
  it.skip("has a stacktrace which can be opened", () => {
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const wrapper = render(
      PageError({ message, serviceContainer, open: true })
    );

    // There should be a collapse button.
    expect(wrapper.find(".collapse-button[aria-expanded=true]").length).toBe(1);

    // There should be five stacktrace items.
    const frameLinks = wrapper.find(`.stack-trace span.frame-link`);
    expect(frameLinks.length).toBe(5);
  });

  // Unskip will happen in Bug 1529548.
  it.skip("toggle the stacktrace when the collapse button is clicked", () => {
    const store = setupStore();
    store.dispatch = sinon.spy();
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );

    let wrapper = mount(
      Provider(
        { store },
        PageError({
          message,
          open: true,
          dispatch: store.dispatch,
          serviceContainer,
        })
      )
    );

    wrapper.find(".collapse-button[aria-expanded='true']").simulate("click");
    let call = store.dispatch.getCall(0);
    expect(call.args[0]).toEqual({
      id: message.id,
      type: MESSAGE_CLOSE,
    });

    wrapper = mount(
      Provider(
        { store },
        PageError({
          message,
          open: false,
          dispatch: store.dispatch,
          serviceContainer,
        })
      )
    );
    wrapper.find(".collapse-button[aria-expanded='false']").simulate("click");
    call = store.dispatch.getCall(1);
    expect(call.args[0]).toEqual({
      id: message.id,
      type: MESSAGE_OPEN,
    });
  });

  it("has the expected indent", () => {
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const indent = 10;
    let wrapper = render(
      PageError({
        message: Object.assign({}, message, { indent }),
        serviceContainer,
      })
    );
    expect(wrapper.prop("data-indent")).toBe(`${indent}`);
    const indentEl = wrapper.find(".indent");
    expect(indentEl.prop("style").width).toBe(`${indent * INDENT_WIDTH}px`);

    wrapper = render(PageError({ message, serviceContainer }));
    expect(wrapper.prop("data-indent")).toBe(`0`);
    // there's no indent element where the indent is 0
    expect(wrapper.find(".indent").length).toBe(0);
  });

  it("has empty error notes", () => {
    const message = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const wrapper = render(PageError({ message, serviceContainer }));

    const notes = wrapper.find(".error-note");

    expect(notes.length).toBe(0);
  });

  it("can show an error note", () => {
    const origMessage = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const message = Object.assign({}, origMessage, {
      notes: [
        {
          messageBody: "test note",
          frame: {
            source: "https://example.com/test.js",
            line: 2,
            column: 6,
          },
        },
      ],
    });

    const wrapper = render(PageError({ message, serviceContainer }));

    const notes = wrapper.find(".error-note");
    expect(notes.length).toBe(1);

    const note = notes.eq(0);
    expect(note.find(".message-body").text()).toBe("note: test note");

    // There should be the location.
    const locationLink = note.find(`.message-location`);
    expect(locationLink.length).toBe(1);
    expect(locationLink.text()).toBe("test.js:2:6");
  });

  it("can show multiple error notes", () => {
    const origMessage = stubPreparedMessages.get(
      "ReferenceError: asdf is not defined"
    );
    const message = Object.assign({}, origMessage, {
      notes: [
        {
          messageBody: "test note 1",
          frame: {
            source: "https://example.com/test1.js",
            line: 2,
            column: 6,
          },
        },
        {
          messageBody: "test note 2",
          frame: {
            source: "https://example.com/test2.js",
            line: 10,
            column: 18,
          },
        },
        {
          messageBody: "test note 3",
          frame: {
            source: "https://example.com/test3.js",
            line: 9,
            column: 4,
          },
        },
      ],
    });

    const wrapper = render(PageError({ message, serviceContainer }));

    const notes = wrapper.find(".error-note");
    expect(notes.length).toBe(3);

    const note1 = notes.eq(0);
    expect(note1.find(".message-body").text()).toBe("note: test note 1");

    const locationLink1 = note1.find(`.message-location`);
    expect(locationLink1.length).toBe(1);
    expect(locationLink1.text()).toBe("test1.js:2:6");

    const note2 = notes.eq(1);
    expect(note2.find(".message-body").text()).toBe("note: test note 2");

    const locationLink2 = note2.find(`.message-location`);
    expect(locationLink2.length).toBe(1);
    expect(locationLink2.text()).toBe("test2.js:10:18");

    const note3 = notes.eq(2);
    expect(note3.find(".message-body").text()).toBe("note: test note 3");

    const locationLink3 = note3.find(`.message-location`);
    expect(locationLink3.length).toBe(1);
    expect(locationLink3.text()).toBe("test3.js:9:4");
  });

  it("displays error notes", () => {
    const message = stubPreparedMessages.get(
      "SyntaxError: redeclaration of let a"
    );

    const wrapper = render(PageError({ message, serviceContainer }));

    const notes = wrapper.find(".error-note");
    expect(notes.length).toBe(1);

    const note = notes.eq(0);
    expect(note.find(".message-body").text()).toBe(
      "note: Previously declared at line 2, column 7"
    );

    // There should be the location.
    const locationLink = note.find(`.message-location`);
    expect(locationLink.length).toBe(1);
    expect(locationLink.text()).toBe("test-console-api.html:2:7");
  });
});