-
Notifications
You must be signed in to change notification settings - Fork 44
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
breaking: Migrate codebase to typescript #101
Changes from 1 commit
5a7af8a
7059af2
b7545bb
225d0e2
e25a27c
b6e868a
17c5539
101a212
5252b75
59c41a8
87184c0
a105a87
470b57d
34c2d0b
487079f
9928fb2
785a4e7
9a4c58a
7dd9d6d
c738e45
cf7ea72
0aa0f9d
071b059
bce1a22
fef4cfc
821615b
5cf066e
bb67f0f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,48 @@ | ||
import { StyleSheet } from 'react-native'; | ||
import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native'; | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import { matcherHint } from 'jest-matcher-utils'; | ||
import { diff } from 'jest-diff'; | ||
import chalk from 'chalk'; | ||
import type { ReactTestInstance } from 'react-test-renderer'; | ||
import type { ImageStyle, StyleProp, TextStyle, ViewStyle } from 'react-native'; | ||
import { StyleSheet } from 'react-native'; | ||
import { checkReactElement, display } from './utils'; | ||
import { checkReactElement } from './utils'; | ||
|
||
type Style = TextStyle | ViewStyle | ImageStyle; | ||
type StyleLike = Record<string, unknown>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why the styleLike? passing an undefined as a styleProp is valid but the flatten won't return a Record There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing out potential Regarding why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just FYI, that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for heads up, I think we good for now and we'll have to adjust JS implementation when something new comes to replace |
||
|
||
const getKeys = Object.keys as <T extends object>(obj: T) => Array<keyof T>; | ||
|
||
function printoutStyles(style: Style) { | ||
return getKeys(style) | ||
function printoutStyles(style: StyleLike) { | ||
return Object.keys(style) | ||
.sort() | ||
.map((prop) => `${prop}: ${display(style[prop])};`) | ||
.map((prop) => | ||
Array.isArray(style[prop]) | ||
? `${prop}: ${JSON.stringify(style[prop], null, 2)};` | ||
: `${prop}: ${style[prop]};`, | ||
) | ||
.join('\n'); | ||
} | ||
|
||
/** | ||
* Recursively narrows down the properties in received to those with counterparts in expected | ||
* Narrows down the properties in received to those with counterparts in expected | ||
*/ | ||
function narrow(expected: Style, received: Style) { | ||
return getKeys(received) | ||
function narrow(expected: StyleLike, received: StyleLike) { | ||
return Object.keys(received) | ||
.filter((prop) => expected[prop]) | ||
.reduce((obj, prop) => { | ||
if (Array.isArray(expected[prop]) && Array.isArray(received[prop])) { | ||
return Object.assign(obj, { | ||
// @ts-ignore not sure how to type it | ||
[prop]: expected[prop]?.map((_, i) => | ||
// @ts-ignore not sure how to type it | ||
narrow(expected[prop][i], received[prop][i]), | ||
), | ||
}); | ||
} | ||
|
||
return Object.assign(obj, { | ||
[prop]: received[prop], | ||
}); | ||
}, {}); | ||
.reduce( | ||
(obj, prop) => | ||
Object.assign(obj, { | ||
[prop]: received[prop], | ||
}), | ||
{}, | ||
); | ||
} | ||
|
||
// Highlights only style rules that were expected but were not found in the | ||
// received computed styles | ||
function expectedDiff(expected: Style, received: Style) { | ||
function expectedDiff(expected: StyleLike, received: StyleLike) { | ||
const receivedNarrow = narrow(expected, received); | ||
|
||
const diffOutput = diff(printoutStyles(expected), printoutStyles(receivedNarrow)); | ||
|
||
// TODO: What's this case? | ||
if (diffOutput == null) return ''; | ||
// Remove the "+ Received" annotation because this is a one-way diff | ||
return diffOutput.replace(`${chalk.red('+ Received')}\n`, ''); | ||
return diffOutput?.replace(`${chalk.red('+ Received')}\n`, '') ?? ''; | ||
} | ||
|
||
export function toHaveStyle( | ||
|
@@ -60,13 +52,11 @@ export function toHaveStyle( | |
) { | ||
checkReactElement(element, toHaveStyle, this); | ||
|
||
const expected = StyleSheet.flatten(style); | ||
const received = StyleSheet.flatten(element.props.style); | ||
const expected = StyleSheet.flatten(style) as StyleLike; | ||
const received = StyleSheet.flatten(element.props.style) as StyleLike; | ||
|
||
return { | ||
pass: Object.entries(expected).every(([prop, value]) => | ||
this.equals(received?.[prop as keyof typeof received], value), | ||
), | ||
pass: Object.entries(expected).every(([prop, value]) => this.equals(received?.[prop], value)), | ||
message: () => { | ||
const matcher = `${this.isNot ? '.not' : ''}.toHaveStyle`; | ||
return [matcherHint(matcher, 'element', ''), expectedDiff(expected, received)].join('\n\n'); | ||
|
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.
i had the doubt when doing the change, do we want to output everything or just the result which is different than the expectation?
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.
The idea behind this is that particular style attributes are not divisible, they need to match as units.
Normally this is not an issue as most style attributes are
string
,number
, etc. The only twoArray
attributes aretransform
andfontVariant
and we want to output the whole array in this case.