-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
95 lines (79 loc) · 2.95 KB
/
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
93
94
95
const ndjsonParser = require('./index');
describe('Parse Ndjson', () => {
it('should parse regular json', () => {
const jsonObject = { some: 'thing' };
const parsed = ndjsonParser(JSON.stringify(jsonObject));
expect(parsed[0]).toEqual(jsonObject);
});
it('should parse \\n seperator', () => {
const jsonObject = { some: 'thing' };
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`${jsonString}\n${jsonString}`);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should parse \\r\\n seperator', () => {
const jsonObject = { some: 'thing' };
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`${jsonString}\n\r${jsonString}`);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should parse json with numbers and booleans', () => {
const jsonObjectWithNumber = { some: 1 };
const jsonObjectWithBoolean = { some: false };
const parsed = ndjsonParser(`${JSON.stringify(jsonObjectWithNumber)}\n${JSON.stringify(jsonObjectWithBoolean)}`);
expect(parsed[0]).toEqual(jsonObjectWithNumber);
expect(parsed[1]).toEqual(jsonObjectWithBoolean);
});
it('should parse json with nested objects', () => {
const jsonObject = { some: { nested: 'object' } };
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`${jsonString}\n${jsonString}`);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should parse json with arrays', () => {
const jsonObject = { some: ['a', 'r', 'r', 'a', 'y'] };
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`${jsonString}\n${jsonString}`);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should parse combinations of all primitives', () => {
const jsonObject = {
number: 1,
bool: false,
some: { nested: 'object' },
arr: ['a', 'r', 'r', 'a', 'y'],
};
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`${jsonString}\n${jsonString}`);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should parse json with empty lines', () => {
const jsonObject = { some: { nested: 'object' } };
const jsonString = JSON.stringify(jsonObject);
const parsed = ndjsonParser(`\n\r${jsonString}\n\n\n\r${jsonString}\n`);
expect(parsed.length).toEqual(2);
expect(parsed[0]).toEqual(jsonObject);
expect(parsed[1]).toEqual(jsonObject);
});
it('should throw error on invalid json', (done) => {
try {
ndjsonParser('{someBadJson,');
} catch (e) {
expect(e).toBeTruthy();
done();
}
});
it('should throw error when input is not string', (done) => {
try {
ndjsonParser({});
} catch (e) {
expect(e).toBeTruthy();
done();
}
});
});