-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreferences.test.js
144 lines (114 loc) · 4.61 KB
/
preferences.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
/* eslint-disable dot-notation, no-invalid-this */
'use strict';
const fs = require('fs');
const expect = require('chai').expect;
const mock = require('mock-fs');
const Preferences = require('../lib/preferences');
describe('testing preferences class', function () {
describe('#isInteractive', function () {
it('should be undefined', function () {
expect(Preferences.isInteractive()).to.be.false;
});
it('should be false', function () {
Preferences._preferences = { interactive: false };
expect(Preferences.isInteractive()).to.be.false;
});
});
describe('#alwaysUseHeaders', function () {
it('should be undefined', function () {
expect(Preferences.alwaysUseHeaders()).to.be.false;
});
});
describe('#file', function () {
it('should end with .gencsv', function () {
expect(Preferences.file()).to.have.string('.gencsv');
});
});
describe('#save', function () {
beforeEach(function () {
const config = {};
config[Preferences.file()] = '{"foo": true}';
mock(config);
});
afterEach(function () {
mock.restore();
});
it('should write preferences to config file', function () {
Preferences.save('always-use-headers', true);
fs.readFile(Preferences.file(), (err, data) => {
expect(String(data)).to.have.string('always-use-headers');
});
});
it('should print a message to the console after saving', function () {
let spy = this.sinon.spy(console, 'log');
Preferences.save('always-use-headers', true, () => {
console.log('Settings written');
expect(console.log.calledOnce).to.be.true;
spy.restore();
});
});
it('should read preferences written to disk', function () {
Preferences._preferences = null; // mock-fs is persisting options after it blocks; fake it
Preferences.save('bar', true);
fs.readFile(Preferences.file(), (err, data) => {
expect(data).to.deep.equal({
foo: true,
bar: true
});
});
});
it('should save when no options are saved on disk', function () {
Preferences.truncate(() => {
Preferences._preferences = null;
Preferences.save('bar', true);
fs.readFile(Preferences.file(), (err, data) => {
expect(data).to.deep.equal({ bar: true });
});
}, () => {
expect.fail('An error occurred truncating persisted settings. Unable to test properly.');
});
});
});
describe('#truncate', function () {
describe('#truncate with mock-fs', function () {
beforeEach(function () {
const config = {};
config[Preferences.file()] = '';
mock(config);
});
afterEach(function () {
mock.restore();
});
it('should remove all saved options', function () {
Preferences.save('always-use-headers', true);
Preferences.truncate();
fs.readFile(Preferences.file(), (err, data) => {
expect(err).to.not.be.null;
expect(err).to.not.be.undefined;
expect(err).to.have.own.property('code');
expect(err).to.have.own.property('code', 'ENOENT');
expect(data).to.be.undefined;
});
});
it('should execute success callback after unlinking file', function () {
let spy = this.sinon.spy(console, 'log');
Preferences.save('always-use-headers', true);
Preferences.truncate(() => console.log('foo'));
expect(console.log.calledOnce).to.be.true;
// spy.should.have.been.calledWithMatch('foo');
spy.restore();
});
});
it('should execute error callback when settings file is not defined', function () {
let spy = this.sinon.spy(console, 'log');
Preferences.truncate(null, () => {
console.log('foo');
});
expect(console.log.calledOnce).to.be.true;
spy.restore();
});
it('should not throw if no callback is defined', function () {
expect(Preferences.truncate).to.not.throw();
});
});
});