import { shallow } from 'enzyme';
import * as React from 'react';
import JSONPretty from '../src/JSONPretty';
test('simple', () => {
const box = shallow();
expect(box.html()).toEqual('
');
});
test('complex object', () => {
const box = shallow();
`{
"aaaa": 1,
"bb:bb": true,
"cccc": "ab\\"cd\\\\",
":i\\"iii:": ":ii:ii",
"dddd": {
"eeee": 1,
"ffff": [
{
"gggg": 3
},
2,
"str:ing"
]
}
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('complex string', () => {
const box = shallow();
`{
"aaaa": 1,
"bb:bb": true,
"cccc": "ab\\"cd\\\\",
":i\\"iii:": ":ii:ii",
"dddd": {
"eeee": 1,
"ffff": [
{
"gggg": 3
},
2,
"str:ing"
]
}
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('complex string with theme', () => {
const box = shallow();
`{
"aaaa": 1,
"bb:bb": true,
"cccc": "ab\\"cd\\\\",
":i\\"iii:": ":ii:ii",
"dddd": {
"eeee": 1,
"ffff": [
{
"gggg": 3
},
2,
"str:ing"
]
}
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('complex string with theme missing boolean theme', () => {
const box = shallow();
`{
"aaaa": 1,
"bb:bb": true,
"cccc": "ab\\"cd\\\\",
":i\\"iii:": ":ii:ii",
"dddd": {
"eeee": 1,
"ffff": [
{
"gggg": 3
},
2,
"str:ing"
]
}
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('invalid', () => {
const box = shallow();
expect(box.html()).toEqual('');
});
test('invalid json', () => {
const box = shallow();
``
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('invalid space', () => {
const box = shallow();
``
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('complex object with format', () => {
const box = shallow();
`{
"aaaa": 1,
"bb:bb": true,
"cccc": "ab\\"cd\\\\~~~abc",
":i\\"iii:": ":ii:ii",
"dddd": {
"eeee": 1,
"ffff": [
{
"gggg": 30
},
2,
"str:ing"
]
}
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box.html()).toMatch(line);
});
});
test('xss pretection', () => {
expect.assertions(2);
function onError(e: Error) {
expect(e).toBeInstanceOf(SyntaxError);
}
const data = `{data: \"
}`;
const box = shallow();
expect(box.html()).toEqual('{data: "<img onerror='alert(document.cookie)' src='invalid-image' />} ');
});
test('onError deprecated', () => {
expect.assertions(2);
function onError(e: Error) {
expect(e).toBeInstanceOf(SyntaxError);
}
const data = `{data: \"
}`;
const J = JSONPretty as any;
const box = shallow();
expect(box.html()).toEqual('{data: "<img onerror='alert(document.cookie)' src='invalid-image' />} ');
});
test('extra style', () => {
const box1 = shallow();
expect(box1.html()).toEqual('');
const box2 = shallow();
expect(box2.html()).toMatch('');
const box3 = shallow();
`{
"a": 1,
"b": true,
"c": "2222"
} `
.split('\n')
.map((line) => line.trim())
.forEach((line) => {
expect(box3.html()).toMatch(line);
});
});