-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
168 lines (146 loc) · 5.08 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
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
'use strict';
require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var writeJson = require('./');
var del = require('delete');
describe('write-json', function() {
afterEach(function(cb) {
del('actual', cb);
});
describe('async', function() {
it('should write JSON asyncronously', function(cb) {
var expected = {foo: {bar: "baz"} };
writeJson('actual/test.json', expected, function(err) {
if (err) return cb(err);
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.deepEqual(JSON.parse(res), expected);
cb();
});
});
});
it('should take additional JSON.stringify args', function(cb) {
var data = {foo: {bar: "baz"}};
var expected = JSON.stringify({foo: {bar: "baz"}}, null, 0);
writeJson('actual/test.json', data, null, 0, function(err) {
if (err) return cb(err);
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.equal(res, expected);
cb();
});
});
});
it('should take JSON.stringify args as an options object', function(cb) {
var data = {foo: {bar: "baz"}};
var expected = JSON.stringify({foo: {bar: "baz"}}, null, 2);
writeJson('actual/test.json', data, {space: 2}, function(err) {
if (err) return cb(err);
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.equal(res, expected);
cb();
});
});
});
it('should return a promise if no callback is passed', function(cb) {
var expected = {foo: {bar: "baz"} };
writeJson('actual/test.json', expected)
.then(function() {
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.deepEqual(JSON.parse(res), expected);
cb();
});
})
.catch(cb);
});
});
describe('promise', function() {
it('should return a promise', function(cb) {
var expected = {foo: {bar: "baz"} };
writeJson.promise('actual/test.json', expected)
.then(function() {
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.deepEqual(JSON.parse(res), expected);
cb();
});
})
.catch(cb);
});
it('should take additional JSON.stringify args', function(cb) {
var data = {foo: {bar: "baz"}};
var expected = JSON.stringify({foo: {bar: "baz"}}, null, 0);
writeJson('actual/test.json', data, null, 0)
.then(function() {
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.equal(res, expected);
cb();
});
})
.catch(cb);
});
it('should take JSON.stringify args as an options object', function(cb) {
var data = {foo: {bar: "baz"}};
var expected = JSON.stringify({foo: {bar: "baz"}}, null, 2);
writeJson('actual/test.json', data, {space: 2})
.then(function() {
fs.readFile('actual/test.json', 'utf8', function(err, res) {
if (err) return cb(err);
assert.equal(res, expected);
cb();
});
})
.catch(cb);
});
});
describe('stream', function() {
it('should write a JSON file using .stream', function(cb) {
fs.createReadStream(path.resolve(__dirname, 'package.json'))
.pipe(writeJson.stream('actual/test.json'))
.on('close', cb);
});
});
describe('sync', function() {
it('should write JSON syncronously', function() {
var expected = {foo: {bar: "baz"} };
writeJson.sync('actual/test.json', expected);
var res = fs.readFileSync('actual/test.json', 'utf8');
assert.deepEqual(JSON.parse(res), expected);
});
it('should pass additional args to JSON.stringify', function() {
var expected = {foo: {bar: "baz"} };
writeJson.sync('actual/test.json', expected, null, 0);
var res = fs.readFileSync('actual/test.json', 'utf8');
assert.deepEqual(JSON.parse(res), expected);
});
it('should take a replacer function on the args', function() {
var expected = {foo: {bar: "baz"} };
var count = 0;
writeJson.sync('actual/test.json', expected, function(key, value) {
count++;
return value;
}, 0);
var res = fs.readFileSync('actual/test.json', 'utf8');
assert.deepEqual(JSON.parse(res), expected);
assert(count >= 1);
});
it('should take a replacer function on the options', function() {
var expected = {foo: {bar: "baz"} };
var count = 0;
writeJson.sync('actual/test.json', expected, {
replacer: function(key, value) {
count++;
return value;
}
}, 0);
var res = fs.readFileSync('actual/test.json', 'utf8');
assert.deepEqual(JSON.parse(res), expected);
assert(count >= 1);
});
});
});