-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move jest-jasemine2 each code to jest-each #6308
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
91cef9a
Add chalk and pretty-format dependenecies to jest-each
mattphillips e2748ac
Add bind logic to jest-each
mattphillips 0038a03
Add jest-each dependency to jest-jasmeine2
mattphillips fba7397
Replace jest-jasemine2 each logic with jest-each
mattphillips ae3733c
Fix broken snapshot
mattphillips f100080
Update changelog
mattphillips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright (c) 2018-present, Facebook, Inc. All rights reserved. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* @flow | ||
*/ | ||
|
||
import util from 'util'; | ||
import chalk from 'chalk'; | ||
import pretty from 'pretty-format'; | ||
|
||
type Table = Array<Array<any>>; | ||
|
||
const EXPECTED_COLOR = chalk.green; | ||
const RECEIVED_COLOR = chalk.red; | ||
|
||
export default (cb: Function) => (...args: any) => ( | ||
title: string, | ||
test: Function, | ||
): void => { | ||
if (args.length === 1) { | ||
const table: Table = args[0]; | ||
return table.forEach(row => | ||
cb(util.format(title, ...row), applyRestParams(row, test)), | ||
); | ||
} | ||
|
||
const templateStrings = args[0]; | ||
const data = args.slice(1); | ||
|
||
const keys = getHeadingKeys(templateStrings[0]); | ||
const table = buildTable(data, keys.length, keys); | ||
|
||
if (data.length % keys.length !== 0) { | ||
return cb(title, () => { | ||
throw new Error( | ||
'Not enough arguments supplied for given headings:\n' + | ||
EXPECTED_COLOR(keys.join(' | ')) + | ||
'\n\n' + | ||
'Received:\n' + | ||
RECEIVED_COLOR(pretty(data)) + | ||
'\n\n' + | ||
`Missing ${RECEIVED_COLOR(`${data.length % keys.length}`)} arguments`, | ||
); | ||
}); | ||
} | ||
|
||
return table.forEach(row => | ||
cb(interpolate(title, row), applyObjectParams(row, test)), | ||
); | ||
}; | ||
|
||
const applyRestParams = (params: Array<any>, test: Function) => { | ||
if (params.length < test.length) return done => test(...params, done); | ||
|
||
return () => test(...params); | ||
}; | ||
|
||
const getHeadingKeys = (headings: string): Array<string> => | ||
headings.replace(/\s/g, '').split('|'); | ||
|
||
const buildTable = ( | ||
data: Array<any>, | ||
rowSize: number, | ||
keys: Array<string>, | ||
): Array<any> => | ||
Array.from({length: data.length / rowSize}) | ||
.map((_, index) => data.slice(index * rowSize, index * rowSize + rowSize)) | ||
.map(row => | ||
row.reduce( | ||
(acc, value, index) => Object.assign({}, acc, {[keys[index]]: value}), | ||
{}, | ||
), | ||
); | ||
|
||
const interpolate = (title: string, data: any) => | ||
Object.keys(data).reduce( | ||
(acc, key) => acc.replace('$' + key, data[key]), | ||
title, | ||
); | ||
|
||
const applyObjectParams = (obj: any, test: Function) => { | ||
if (test.length > 1) return done => test(obj, done); | ||
|
||
return () => test(obj); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it would be good to point to user code here, but that can be fixed later