-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-transform-stream.test.js
109 lines (95 loc) · 2.34 KB
/
json-transform-stream.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const { Readable } = require('stream');
const JSONTransform = require('./');
describe('json-transform-stream', () => {
const data = [
{ index: 1 },
{ index: 2 },
{ index: 3 }
];
const iterator = function* (_data = data) {
for (const d of _data) {
yield JSON.stringify(d);
}
};
describe('defaults', () => {
let streamed;
beforeEach((done) => {
const stream = Readable.from(iterator())
.pipe(JSONTransform());
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});
it('JSON stringifies the objects to an array', () => {
expect(streamed)
.toEqual(JSON.stringify(data));
});
});
describe('pre/post', () => {
let streamed;
beforeEach((done) => {
const stream = Readable.from(iterator())
.pipe(JSONTransform({
pre: '{"data":[',
post: ']}'
}));
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});
it('Allows to setup custom wrapping', () => {
expect(streamed)
.toEqual(
JSON.stringify({
data
})
);
});
describe('empty source', () => {
beforeEach((done) => {
streamed = '';
const stream = Readable.from(iterator([]))
.pipe(JSONTransform({
pre: '{"data":[',
post: ']}'
}));
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});
it('Allows to setup custom wrapping', () => {
expect(streamed)
.toEqual(
JSON.stringify({
data: []
})
);
});
});
});
describe('separator', () => {
let streamed;
beforeEach((done) => {
const stream = Readable.from(iterator())
.pipe(JSONTransform({
post: '',
pre: '',
separator: ';'
}));
stream.on('data', (data) => {
streamed = `${streamed || ''}${data}`;
});
stream.on('end', done);
});
it('Allows to setup custom wrapping', () => {
expect(streamed)
.toEqual(
data.map((d) => JSON.stringify(d))
.join(';')
);
});
});
});