-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.test.js
147 lines (121 loc) · 4.64 KB
/
utils.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
/* eslint-disable dot-notation, no-invalid-this */
'use strict';
const stripAnsi = require('strip-ansi');
const expect = require('chai').expect;
const Utils = require('../lib/utils');
const mock = require('mock-fs');
require('mocha-sinon');
describe('Testing the Utils class', function () {
describe('#home', function () {
it('should contain the user\'s name', function () {
expect(Utils.home()).to.have.string(process.env[process.platform === 'win32' ? 'USERNAME' : 'USER']);
});
});
describe('#listFunctions', function () {
it('should contain name, email, and address as functions', function () {
const table = Utils.listFunctions({
name: 'This is a test',
email: 'This is a test',
address: 'This is a test'
});
expect(table).to.have.string('name');
expect(table).to.have.string('email');
expect(table).to.have.string('address');
});
it('should render variations alongside top-level options', function () {
const table = stripAnsi(Utils.listFunctions({
name: {
desc: 'Testing',
variations: {
'name(2)': 'My desc'
}
}
}));
expect(table).to.have.string('name');
expect(table).to.have.string('Testing');
expect(table).to.have.string('name(2)');
expect(table).to.have.string('My desc');
});
it('should only add strings and objects of the right format', function () {
const table = Utils.listFunctions({
name: 0,
email: 'This is a test',
address: 'This is a test'
});
expect(table).to.not.have.string('name');
expect(table).to.have.string('email');
expect(table).to.have.string('address');
});
});
describe('#functions', function () {
it('should print content to the console', function () {
let spy = this.sinon.spy(console, 'log');
stripAnsi(Utils.functions({
name: 'This is a test',
email: 'This is a test',
address: 'This is a test'
}));
expect(console.log.calledOnce).to.be.true;
spy.restore();
});
});
describe('#convertNumber', function () {
it('should throw an exception on invalid input', function () {
expect(Utils.convertNumber).to.throw(Error);
});
it('should throw an exception when conversion character is unsupported', function () {
try {
Utils.convertNumber('10T');
} catch (e) {
expect(e).to.be.an.instanceof(Error);
expect(e.message).to.equal('Invalid number');
}
});
it('should not change 100', function () {
expect(Utils.convertNumber('100')).to.equal(100);
});
it('should convert 10K to 10000', function () {
expect(Utils.convertNumber('10K')).to.equal(10000);
});
it('should convert 10M to 10000000', function () {
expect(Utils.convertNumber('10M')).to.equal(10000000);
});
it('should convert 0.8B to 800000000', function () {
expect(Utils.convertNumber('0.8B')).to.equal(800000000);
});
it('should return return an integer given a float', function () {
expect(Utils.convertNumber(3.14)).to.equal(3);
});
it('should round up when too many decimals are added', function () {
expect(Utils.convertNumber('0.1234567890K')).to.equal(124);
});
it('should throw an exception given zero', function () {
try {
Utils.convertNumber(0);
} catch (e) {
expect(e).to.be.an.instanceof(Error);
}
});
it('should throw an exception given a negative number', function () {
try {
Utils.convertNumber(-1);
} catch (e) {
expect(e).to.be.an.instanceof(Error);
}
});
});
describe('#generate', function () {
it('should call generate without an exception', function (done) {
mock();
const generate = Utils.generate('foo.csv', ['name'], {
rows: 1,
chunks: 1,
silent: true
}).then(function () {
expect(generate).to.not.throw;
mock.restore();
done();
});
});
});
});