Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
✅ Update test for tablify
Browse files Browse the repository at this point in the history
  • Loading branch information
shuse2 committed May 28, 2018
1 parent 931f363 commit 766464e
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 134 deletions.
8 changes: 0 additions & 8 deletions src/utils/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,3 @@ export class ValidationError extends Error {
this.name = 'ValidationError';
}
}

export class PrintError extends Error {
constructor(message) {
super(message);
this.message = chalk.red(message);
this.name = 'PrintError';
}
}
20 changes: 0 additions & 20 deletions test/specs/utils/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,24 +66,4 @@ describe('Custom Errors', () => {
);
},
);
Given(
'a function that throws a print error "cannot be displayed"',
given.aFunctionThatThrowsAPrintError,
() => {
When('the print error is thrown', when.thePrintErrorIsThrown, () => {
Then(
'it should print the error message in red',
then.itShouldPrintTheErrorMessageInRed,
);
Then(
'the error should have the name "PrintError"',
then.theErrorShouldHaveTheName,
);
Then(
'the error should be an instance of Node’s built-in Error',
then.theErrorShouldBeAnInstanceOfNodesBuiltInError,
);
});
},
);
});
48 changes: 20 additions & 28 deletions test/specs/utils/tablify.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,32 @@ describe('tablify util', () => {
Given('a non-empty object', given.aNonEmptyObject, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
'the returned table should have a head with the object’s keys',
then.theReturnedTableShouldHaveAHeadWithTheObjectKeys,
);
Then(
'the returned table should have a row with the object’s values',
then.theReturnedTableShouldHaveARowWithTheObjectValues,
'the returned table should have a row with the object’s key values',
then.theReturnedTableShouldHaveARowWithTheObjectKeyValues,
);
});
});
Given('a nested object', given.aNestedObject, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
'the returned table should have a head with the object’s nested keys',
then.theReturnedTableShouldHaveAHeadWithTheObjectNestedKeys,
);
Then(
'the returned table should have a row with the object’s nested values',
then.theReturnedTableShouldHaveAHeadWithTheObjectNestedValues,
'the returned table should have a row with the object key and stringified nested values',
then.theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues,
);
});
});
Given('a deeply nested object', given.aDeeplyNestedObject, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
'it should throw print error "Output cannot be displayed in table format: Maximum object depth of 3 was exceeded. Consider using JSON output format."',
then.itShouldThrowPrintError,
'the returned table should have a row with the object key and stringified nested values',
then.theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues,
);
});
});
Given('a cyclic object', given.aCyclicObject, () => {
When('the object is tablified', when.theObjectIsTablified, () => {
Then(
'it should throw print error "Output cannot be displayed in table format: Maximum object depth of 3 was exceeded. Consider using JSON output format."',
then.itShouldThrowPrintError,
'it should throw type error "Converting circular structure to JSON"',
then.itShouldThrowTypeError,
);
});
});
Expand All @@ -76,12 +68,12 @@ describe('tablify util', () => {
() => {
When('the array is tablified', when.theArrayIsTablified, () => {
Then(
'the returned table should have a head with the objects’ keys',
then.theReturnedTableShouldHaveAHeadWithTheObjectsKeys,
'the returned table should have head rows',
then.theReturnedTableShouldHaveHeaderRows,
);
Then(
'the returned table should have a row for each object with the object’s values',
then.theReturnedTableShouldHaveARowForEachObjectWithTheObjectValues,
'the returned table should have rows with the object key and stringified values',
then.theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedValues,
);
});
},
Expand All @@ -92,12 +84,12 @@ describe('tablify util', () => {
() => {
When('the array is tablified', when.theArrayIsTablified, () => {
Then(
'the returned table should have a head with every unique key',
then.theReturnedTableShouldHaveAHeadWithEveryUniqueKey,
'the returned table should have head rows',
then.theReturnedTableShouldHaveHeaderRows,
);
Then(
'the returned table should have a row for each object with the object’s values',
then.theReturnedTableShouldHaveARowForEachObjectWithTheObjectsValues,
'the returned table should have rows with the object key and stringified values',
then.theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedValues,
);
});
},
Expand All @@ -108,12 +100,12 @@ describe('tablify util', () => {
() => {
When('the array is tablified', when.theArrayIsTablified, () => {
Then(
'the returned table should have a head with the objects’ nested keys',
then.theReturnedTableShouldHaveAHeadWithTheObjectsNestedKeys,
'the returned table should have head rows',
then.theReturnedTableShouldHaveHeaderRows,
);
Then(
'the returned table should have a row for each object with the object’s values',
then.theReturnedTableShouldHaveARowForEachObjectWithTheObjectNestedValues,
'the returned table should have rows with the object key and stringified values',
then.theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedNestedValues,
);
});
},
Expand Down
12 changes: 3 additions & 9 deletions test/steps/general/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@
*
*/
import { getFirstQuotedString, getFirstNumber } from '../utils';
import {
ValidationError,
FileSystemError,
PrintError,
} from '../../../src/utils/error';
import { ValidationError, FileSystemError } from '../../../src/utils/error';

export function theErrorShouldBeAnInstanceOfNodesBuiltInError() {
const { testError } = this.test.ctx;
Expand Down Expand Up @@ -47,12 +43,10 @@ export function itShouldThrowFileSystemError() {
.and.be.customError(new FileSystemError(message));
}

export function itShouldThrowPrintError() {
export function itShouldThrowTypeError() {
const { testFunction } = this.test.ctx;
const message = getFirstQuotedString(this.test.title);
return expect(testFunction)
.to.throw()
.and.be.customError(new PrintError(message));
return expect(testFunction).to.throw(TypeError, message);
}

export function itShouldExitWithCode() {
Expand Down
116 changes: 47 additions & 69 deletions test/steps/printing/3_then.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,90 +134,68 @@ export function theReturnedTableShouldHaveNoRows() {
return expect(returnValue).to.have.length(0);
}

export function theReturnedTableShouldHaveAHeadWithTheObjectKeys() {
export function theReturnedTableShouldHaveARowWithTheObjectKeyValues() {
const { returnValue, testObject } = this.test.ctx;
const keys = Object.keys(testObject);
return expect(returnValue.options)
.to.have.property('head')
.eql(keys);
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
expect({ [key]: value }).to.eql(returnValue[arrayKey]);
});
}

export function theReturnedTableShouldHaveARowWithTheObjectValues() {
export function theReturnedTableShouldHaveARowWithTheObjectKeyAndStringifiedNestedValues() {
const { returnValue, testObject } = this.test.ctx;
const values = Object.values(testObject);
return expect(returnValue[0]).to.eql(values);
}

export function theReturnedTableShouldHaveAHeadWithTheObjectNestedKeys() {
const { returnValue } = this.test.ctx;
const keys = [
'root',
'nested.object',
'nested.testing',
'nested.nullValue',
'nested.keys.more',
];
return expect(returnValue.options)
.to.have.property('head')
.eql(keys);
}

export function theReturnedTableShouldHaveAHeadWithTheObjectNestedValues() {
const { returnValue } = this.test.ctx;
const values = ['value', 'values', 123, null, 'publicKey1\npublicKey2'];
return expect(returnValue[0]).to.eql(values);
}

export function theReturnedTableShouldHaveAHeadWithTheObjectsKeys() {
const { returnValue, testArray } = this.test.ctx;
const keys = Object.keys(testArray[0]);
return expect(returnValue.options)
.to.have.property('head')
.eql(keys);
}

export function theReturnedTableShouldHaveAHeadWithTheObjectsNestedKeys() {
const { returnValue, testArrayKeysResult } = this.test.ctx;
return expect(returnValue.options)
.to.have.property('head')
.eql(testArrayKeysResult);
return Object.entries(testObject).forEach(([key, value], arrayKey) => {
const strValue =
typeof value === 'object'
? Object.entries(value)
.map(([vKey, vValue]) => `${vKey}: ${JSON.stringify(vValue)}`)
.join('\n')
: value;
expect({ [key]: strValue }).to.eql(returnValue[arrayKey]);
});
}

export function theReturnedTableShouldHaveARowForEachObjectWithTheObjectValues() {
export function theReturnedTableShouldHaveHeaderRows() {
const { returnValue, testArray } = this.test.ctx;
return testArray.forEach((testObject, i) => {
const values = Object.values(testObject);
return expect(returnValue[i]).to.eql(values);
testArray.forEach((value, key) => {
expect(
returnValue[key * (Object.keys(value).length + 1)][0],
).to.have.property('colSpan');
});
}

export function theReturnedTableShouldHaveARowForEachObjectWithTheObjectNestedValues() {
const { returnValue, testArray, testArrayValuesResult } = this.test.ctx;
return testArray.forEach((testObject, i) => {
return expect(returnValue[i]).to.eql(testArrayValuesResult[i]);
});
export function theReturnedTableShouldHaveARowWithTheObjectValues() {
const { returnValue, testObject } = this.test.ctx;
const values = Object.values(testObject);
return expect(returnValue[0]).to.eql(values);
}

export function theReturnedTableShouldHaveAHeadWithEveryUniqueKey() {
export function theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedValues() {
const { returnValue, testArray } = this.test.ctx;
const uniqueKeys = testArray.reduce((keys, testObject) => {
const newKeys = Object.keys(testObject).filter(key => !keys.includes(key));
return [...keys, ...newKeys];
}, []);
return expect(returnValue.options)
.to.have.property('head')
.eql(uniqueKeys);
return testArray.forEach((values, i) => {
Object.keys(values).forEach((key, keyIndex) => {
expect(returnValue[i * testArray.length + 1 + keyIndex]).eql({
[key]: values[key],
});
});
});
}

export function theReturnedTableShouldHaveARowForEachObjectWithTheObjectsValues() {
export function theReturnedTableShouldHaveRowsWithTheObjectKeyAndStringifiedNestedValues() {
const { returnValue, testArray } = this.test.ctx;
return testArray.forEach((testObject, i) => {
const row = returnValue[i];
const values = Object.values(testObject);

values.forEach(value => expect(row).to.include(value));
return row
.filter(value => !values.includes(value))
.forEach(value => expect(value).to.be.undefined);
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 = Object.entries(values[key])
.map(([vKey, vValue]) => `${vKey}: ${JSON.stringify(vValue)}`)
.join('\n');
}
expect(returnValue[i * (innerObjectKeys.length + 1) + keyIndex + 1]).eql({
[key]: strValue,
});
});
});
}

0 comments on commit 766464e

Please sign in to comment.