This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
3_then.js
205 lines (180 loc) · 7.07 KB
/
3_then.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
* LiskHQ/lisk-commander
* Copyright © 2017–2018 Lisk Foundation
*
* See the LICENSE file at the top-level directory of this distribution
* for licensing information.
*
* Unless otherwise agreed in a custom licensing agreement with the Lisk Foundation,
* no part of this software, including this file, may be copied, modified,
* propagated, or distributed except according to the terms contained in the
* LICENSE file.
*
* Removal or modification of this copyright notice is prohibited.
*
*/
import chalk from 'chalk';
import {
shouldUseJSONOutput,
shouldUsePrettyOutput,
} from '../../../src/utils/helpers';
import tablify from '../../../src/utils/tablify';
import { objectToKeyValueString } from '../utils';
export function consoleErrorShouldBeCalledWithTheFirstStringInRedAndTheOtherArguments() {
const { testArguments } = this.test.ctx;
const redArgument = chalk.red(testArguments[0]);
expect(console.error).to.be.calledWithExactly(
redArgument,
...testArguments.slice(1),
);
}
export function consoleErrorShouldBeCalledWithTheStringsInRed() {
const { testArguments } = this.test.ctx;
const redArguments = testArguments.map(arg => chalk.red(arg));
expect(console.error).to.be.calledWithExactly(...redArguments);
}
export function consoleWarnShouldBeCalledWithTheFirstStringInYellowAndTheOtherArguments() {
const { testArguments } = this.test.ctx;
const yellowArgument = chalk.yellow(testArguments[0]);
expect(console.warn).to.be.calledWithExactly(
yellowArgument,
...testArguments.slice(1),
);
}
export function consoleWarnShouldBeCalledWithTheStringsInYellow() {
const { testArguments } = this.test.ctx;
const yellowArguments = testArguments.map(arg => chalk.yellow(arg));
expect(console.warn).to.be.calledWithExactly(...yellowArguments);
}
export function itShouldPrintTheErrorMessageInRed() {
const { testError: { message }, errorMessage } = this.test.ctx;
return expect(message).to.equal(chalk.red(errorMessage));
}
export function theErrorShouldBePrintedWithThePrefix() {
const { printFunction, errorMessage, prefix } = this.test.ctx;
return expect(printFunction).to.be.calledWithExactly({
error: `${prefix}: ${errorMessage}`,
});
}
export function theActiveCommandShouldBeUsedToLog() {
const { activeCommand } = this.test.ctx;
return expect(activeCommand.log).to.be.calledOnce;
}
export function theObjectShouldBePrinted() {
const { printFunction, testObject } = this.test.ctx;
return expect(printFunction).to.be.calledWithExactly(testObject);
}
export function aTableShouldBeLogged() {
const { result, vorpal } = this.test.ctx;
const tableOutput = tablify(result).toString();
return expect(vorpal.log).to.be.calledWithExactly(tableOutput);
}
export function prettyJSONOutputShouldBeLoggedWithoutANSICodes() {
const { resultWithoutANSICodes, vorpal } = this.test.ctx;
const prettyJSONOutput = JSON.stringify(resultWithoutANSICodes, null, '\t');
return expect(vorpal.log).to.be.calledWithExactly(prettyJSONOutput);
}
export function jsonOutputShouldBeLoggedWithoutANSICodes() {
const { resultWithoutANSICodes, vorpal } = this.test.ctx;
const jsonOutput = JSON.stringify(resultWithoutANSICodes);
return expect(vorpal.log).to.be.calledWithExactly(jsonOutput);
}
export function prettyJSONOutputsShouldBeLoggedWithoutANSICodes() {
const { resultsWithoutANSICodes, vorpal } = this.test.ctx;
const prettyJSONOutput = JSON.stringify(resultsWithoutANSICodes, null, '\t');
return expect(vorpal.log).to.be.calledWithExactly(prettyJSONOutput);
}
export function jsonOutputsShouldBeLoggedWithoutANSICodes() {
const { resultsWithoutANSICodes, vorpal } = this.test.ctx;
const jsonOutput = JSON.stringify(resultsWithoutANSICodes);
return expect(vorpal.log).to.be.calledWithExactly(jsonOutput);
}
export function shouldUseJSONOutputShouldBeCalledWithTheConfigAndAnEmptyOptionsObject() {
const { config } = this.test.ctx;
return expect(shouldUseJSONOutput).to.be.calledWithExactly(config, {});
}
export function shouldUsePrettyOutputShouldBeCalledWithTheConfigAndAnEmptyOptionsObject() {
const { config } = this.test.ctx;
return expect(shouldUsePrettyOutput).to.be.calledWithExactly(config, {});
}
export function shouldUseJSONOutputShouldBeCalledWithTheConfigAndTheOptions() {
const { config, options } = this.test.ctx;
return expect(shouldUseJSONOutput).to.be.calledWithExactly(config, options);
}
export function shouldUsePrettyOutputShouldBeCalledWithTheConfigAndTheOptions() {
const { config, options } = this.test.ctx;
return expect(shouldUsePrettyOutput).to.be.calledWithExactly(config, options);
}
export function theReturnedTableShouldHaveNoHead() {
const { returnValue } = this.test.ctx;
return expect(returnValue.options)
.to.have.property('head')
.eql([]);
}
export function theReturnedTableShouldHaveNoRows() {
const { returnValue } = this.test.ctx;
return expect(returnValue).to.have.length(0);
}
export function theReturnedTableShouldHaveARowWithTheObjectKeyValues() {
const { returnValue, testObject } = this.test.ctx;
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
expect({ [key]: value }).to.eql(returnValue[arrayKey]);
});
}
export function theReturnedTableShouldHaveARow() {
const { returnValue, testObject } = this.test.ctx;
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
const strValue =
typeof value === 'object' ? objectToKeyValueString(value) : value;
expect({ [key]: strValue }).to.eql(returnValue[arrayKey]);
});
}
export function theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues() {
const { returnValue, testObject } = this.test.ctx;
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
let strValue = value;
if (Array.isArray(value)) {
strValue = value
.map(element => objectToKeyValueString(element))
.join('\n');
} else if (typeof value === 'object') {
strValue = objectToKeyValueString(value);
}
expect({ [key]: strValue }).to.eql(returnValue[arrayKey]);
});
}
export function theReturnedTableShouldHaveHeaderRows() {
const { returnValue, testArray } = this.test.ctx;
testArray.forEach((value, key) => {
expect(
returnValue[key * (Object.keys(value).length + 1)][0],
).to.have.property('colSpan');
});
}
export function theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedValues() {
const { returnValue, testArray } = this.test.ctx;
return testArray.forEach((values, i) => {
Object.keys(values).forEach((key, keyIndex) => {
expect(returnValue[i * testArray.length + 1 + keyIndex]).eql({
[key]: values[key],
});
});
});
}
export function theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedNestedValues() {
const { returnValue, testArray } = this.test.ctx;
return testArray.forEach((values, i) => {
const innerObjectKeys = Object.keys(values);
innerObjectKeys.forEach((key, keyIndex) => {
let strValue = values[key];
if (Array.isArray(values[key])) {
strValue = values[key].join('\n');
} else if (typeof values[key] === 'object') {
strValue = objectToKeyValueString(values[key]);
}
expect(returnValue[i * (innerObjectKeys.length + 1) + keyIndex + 1]).eql({
[key]: strValue,
});
});
});
}