forked from shakacode/react_on_rails
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildConsoleReplay.test.js
92 lines (79 loc) · 3.05 KB
/
buildConsoleReplay.test.js
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
import buildConsoleReplay, { consoleReplay } from '../src/buildConsoleReplay';
describe('consoleReplay', () => {
expect.assertions(8);
it('does not throw an exception if no console.history object', () => {
expect.assertions(1);
expect(() => consoleReplay()).not.toThrow(/Error/);
});
it('returns empty string if no console.history object', () => {
expect.assertions(1);
const actual = consoleReplay();
const expected = '';
expect(actual).toEqual(expected);
});
it('does not throw an exception if console.history.length is zero', () => {
expect.assertions(1);
console.history = [];
expect(() => consoleReplay()).not.toThrow(/Error/);
});
it('returns empty string if no console.history object', () => {
expect.assertions(1);
console.history = [];
const actual = consoleReplay();
const expected = '';
expect(actual).toEqual(expected);
});
it('replays multiple history messages', () => {
expect.assertions(1);
console.history = [
{ arguments: ['a', 'b'], level: 'log' },
{ arguments: ['c', 'd'], level: 'warn' },
];
const actual = consoleReplay();
const expected = 'console.log.apply(console, ["a","b"]);\nconsole.warn.apply(console, ["c","d"]);';
expect(actual).toEqual(expected);
});
it('replays converts console param objects to JSON', () => {
expect.assertions(1);
console.history = [
{ arguments: ['some message', { a: 1, b: 2 }], level: 'log' },
{ arguments: ['other message', { c: 3, d: 4 }], level: 'warn' },
];
const actual = consoleReplay();
const expected = `console.log.apply(console, ["some message","{\\"a\\":1,\\"b\\":2}"]);
console.warn.apply(console, ["other message","{\\"c\\":3,\\"d\\":4}"]);`;
expect(actual).toEqual(expected);
});
it('replays converts script tag inside of object string to be safe ', () => {
expect.assertions(1);
console.history = [
{
arguments: [
"some message </script><script>alert('WTF')</script>",
{ a: "Wow</script><script>alert('WTF')</script>", b: 2 },
],
level: 'log',
},
{ arguments: ['other message', { c: 3, d: 4 }], level: 'warn' },
];
const actual = consoleReplay();
const expected = `console.log.apply(console, ["some message (/script><script>alert('WTF')\
(/script>","{\\"a\\":\\"Wow(/script><script>alert('WTF')(/script>\\",\\"b\\":2}"]);
console.warn.apply(console, ["other message","{\\"c\\":3,\\"d\\":4}"]);`;
expect(actual).toEqual(expected);
});
it('buildConsoleReplay wraps console replay in a script tag', () => {
expect.assertions(1);
console.history = [
{ arguments: ['some message', { a: 1, b: 2 }], level: 'log' },
{ arguments: ['other message', { c: 3, d: 4 }], level: 'warn' },
];
const actual = buildConsoleReplay();
const expected = `
<script id="consoleReplayLog">
console.log.apply(console, ["some message","{\\"a\\":1,\\"b\\":2}"]);
console.warn.apply(console, ["other message","{\\"c\\":3,\\"d\\":4}"]);
</script>`;
expect(actual).toEqual(expected);
});
});