-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0e89b6
commit 9a7b4d2
Showing
5 changed files
with
126 additions
and
4 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import React from 'react'; | ||
import { StyleSheet, View, Text } from 'react-native'; | ||
import { render } from 'native-testing-library'; | ||
|
||
describe('.toHaveStyle', () => { | ||
test('handles positive test cases', () => { | ||
const styles = StyleSheet.create({ container: { color: 'white' } }); | ||
const { getByTestId } = render( | ||
<View | ||
testID="container" | ||
style={[{ backgroundColor: 'blue', height: '100%' }, styles.container]} | ||
> | ||
<Text>Hello World</Text> | ||
</View>, | ||
); | ||
|
||
const container = getByTestId('container'); | ||
|
||
expect(container).toHaveStyle({ backgroundColor: 'blue', height: '100%' }); | ||
expect(container).toHaveStyle([{ backgroundColor: 'blue' }, { height: '100%' }]); | ||
expect(container).toHaveStyle({ backgroundColor: 'blue' }); | ||
expect(container).toHaveStyle({ height: '100%' }); | ||
expect(container).toHaveStyle({ color: 'white' }); | ||
}); | ||
|
||
test('handles negative test cases', () => { | ||
const { getByTestId } = render( | ||
<View testID="container" style={{ backgroundColor: 'blue', color: 'black', height: '100%' }}> | ||
<Text>Hello World</Text> | ||
</View>, | ||
); | ||
|
||
const container = getByTestId('container'); | ||
|
||
expect(() => expect(container).toHaveStyle({ fontWeight: 'bold' })).toThrowError(); | ||
expect(() => expect(container).not.toHaveStyle({ color: 'black' })).toThrowError(); | ||
}); | ||
}); |
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,49 @@ | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import jestDiff from 'jest-diff'; | ||
import chalk from 'chalk'; | ||
import { all, compose, mergeAll, toPairs } from 'ramda'; | ||
|
||
import { checkReactElement } from './utils'; | ||
|
||
function isSubset(expected, received) { | ||
return compose( | ||
all(([prop, value]) => received[prop] === value), | ||
toPairs, | ||
)(expected); | ||
} | ||
|
||
function printoutStyles(styles) { | ||
return Object.keys(styles) | ||
.sort() | ||
.map(prop => `${prop}: ${styles[prop]};`) | ||
.join('\n'); | ||
} | ||
|
||
// Highlights only style rules that were expected but were not found in the | ||
// received computed styles | ||
function expectedDiff(expected, elementStyles) { | ||
const received = Object.keys(elementStyles) | ||
.filter(prop => expected[prop]) | ||
.reduce((obj, prop) => Object.assign(obj, { [prop]: elementStyles[prop] }), {}); | ||
|
||
const diffOutput = jestDiff(printoutStyles(expected), printoutStyles(received)); | ||
// Remove the "+ Received" annotation because this is a one-way diff | ||
return diffOutput.replace(`${chalk.red('+ Received')}\n`, ''); | ||
} | ||
|
||
export function toHaveStyle(element, style) { | ||
checkReactElement(element, toHaveStyle, this); | ||
|
||
const elementStyle = element.props.style; | ||
|
||
const expected = Array.isArray(style) ? mergeAll(style) : style; | ||
const received = Array.isArray(elementStyle) ? mergeAll(elementStyle) : elementStyle; | ||
|
||
return { | ||
pass: isSubset(expected, received), | ||
message: () => { | ||
const matcher = `${this.isNot ? '.not' : ''}.toHaveStyle`; | ||
return [matcherHint(matcher, 'element', ''), expectedDiff(expected, received)].join('\n\n'); | ||
}, | ||
}; | ||
} |
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