Skip to content
This repository was archived by the owner on Jul 30, 2020. It is now read-only.

Commit 4980ac2

Browse files
committed
feat: add asFragment to the API
1 parent be20bb0 commit 4980ac2

File tree

7 files changed

+88
-6
lines changed

7 files changed

+88
-6
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@
6565
"react-navigation": "^3.5.1",
6666
"react-redux": "^7.0.3",
6767
"redux": "^4.0.1",
68-
"semantic-release": "^15.13.3"
68+
"semantic-release": "^15.13.3",
69+
"snapshot-diff": "0.5.1"
6970
},
7071
"peerDependencies": {
7172
"react": "*",
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`fragments can show diffs 1`] = `
4+
"Snapshot Diff:
5+
- First value
6+
+ Second value
7+
8+
@@ -6,8 +6,8 @@
9+
\\"flex\\": 1,
10+
}
11+
}
12+
>
13+
<Button
14+
- title=\\"Click to increase: 0\\"
15+
+ title=\\"Click to increase: 1\\"
16+
/>
17+
</View>"
18+
`;
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`supports fragments 1`] = `
4+
<View
5+
collapsable={true}
6+
pointerEvents="box-none"
7+
style={
8+
Object {
9+
"flex": 1,
10+
}
11+
}
12+
>
13+
<View>
14+
<Text>
15+
Fragments are pretty cool!
16+
</Text>
17+
</View>
18+
</View>
19+
`;

src/__tests__/misc.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import React from 'react';
2-
import { Picker } from 'react-native';
2+
import { Button, Picker } from 'react-native';
3+
import { toMatchDiffSnapshot } from 'snapshot-diff';
34

45
import { fireEvent, render } from '../';
56

6-
test('picker works', () => {
7+
test('<Picker /> works', () => {
78
function Wrapper() {
89
const [value, setValue] = React.useState('js');
910

@@ -19,3 +20,25 @@ test('picker works', () => {
1920
fireEvent.valueChange(getByDisplayValue('js'), 'java');
2021
expect(() => findByDisplayValue('js')).not.toThrow();
2122
});
23+
24+
test('fragments can show diffs', () => {
25+
function TestComponent() {
26+
const [count, setCount] = React.useState(0);
27+
28+
return (
29+
<Button onPress={() => setCount(count => count + 1)} title={`Click to increase: ${count}`} />
30+
);
31+
}
32+
33+
expect.extend({ toMatchDiffSnapshot });
34+
35+
const { getByText, asFragment } = render(<TestComponent />);
36+
const firstRender = asFragment();
37+
38+
fireEvent.press(getByText(/Click to increase/));
39+
40+
// This will snapshot only the difference between the first render, and the
41+
// state of the DOM after the click event.
42+
// See https://github.com/jest-community/snapshot-diff
43+
expect(firstRender).toMatchDiffSnapshot(asFragment());
44+
});

src/__tests__/render.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
2-
import { SafeAreaView, View } from 'react-native';
2+
import { Text, SafeAreaView, View } from 'react-native';
33

4-
import { render } from '../';
4+
import { render, toJSON } from '../';
55

66
test('renders View', () => {
77
const { container } = render(<View />);
@@ -13,6 +13,23 @@ test('returns container', () => {
1313
expect(container).toBeTruthy();
1414
});
1515

16+
it('supports fragments', () => {
17+
class Test extends React.Component {
18+
render() {
19+
return (
20+
<View>
21+
<Text>Fragments are pretty cool!</Text>
22+
</View>
23+
);
24+
}
25+
}
26+
27+
const { asFragment, unmount } = render(<Test />);
28+
expect(asFragment()).toMatchSnapshot();
29+
unmount();
30+
expect(asFragment()).toBeNull();
31+
});
32+
1633
test('renders options.wrapper around node', () => {
1734
const WrapperComponent = ({ children }) => (
1835
<SafeAreaView testID="wrapper">{children}</SafeAreaView>

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import TR from 'react-test-renderer';
33
import AppContainer from 'react-native/Libraries/ReactNative/AppContainer';
44

55
import {
6+
toJSON,
67
fireEvent as rntlFireEvent,
78
getQueriesForElement,
89
NativeTestEvent,
@@ -41,6 +42,9 @@ function render(ui, { options = {}, wrapper: WrapperComponent, queries } = {}) {
4142
testRenderer.update(wrapUiIfNeeded(rerenderUi));
4243
});
4344
},
45+
asFragment: () => {
46+
return toJSON(container);
47+
},
4448
...getQueriesForElement(baseElement, queries),
4549
};
4650
}

src/preset/serializer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { toJSON } from '../lib';
22

33
module.exports = {
44
test(value) {
5-
return value.$$typeof && Symbol.keyFor(value.$$typeof) === 'ntl.element';
5+
return value && value.$$typeof && Symbol.keyFor(value.$$typeof) === 'ntl.element';
66
},
77
print(value, serialize) {
88
return serialize(toJSON(value));

0 commit comments

Comments
 (0)