Skip to content

Commit 1e3510b

Browse files
authored
Increase coverage (#115)
1 parent a5d2566 commit 1e3510b

File tree

6 files changed

+295
-22
lines changed

6 files changed

+295
-22
lines changed

src/NestedListView.test.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,4 +183,47 @@ describe('NestedListView', () => {
183183
.toJSON()
184184
expect(nestedListView).toMatchSnapshot()
185185
})
186+
187+
test('renders without renderNode', () => {
188+
const data = [{title: 'child1'}, {title: 'child2'}, {title: 'child3'}]
189+
const nestedListView = renderer
190+
.create(
191+
<NestedListView
192+
getChildrenName={() => 'items'}
193+
// @ts-ignore
194+
renderNode={undefined}
195+
data={data}
196+
/>
197+
)
198+
.toJSON()
199+
expect(nestedListView).toMatchSnapshot()
200+
})
201+
202+
test('renders without data', () => {
203+
const data = [{title: 'child1'}, {title: 'child2'}, {title: 'child3'}]
204+
const nestedListView = renderer
205+
.create(
206+
<NestedListView
207+
getChildrenName={() => 'items'}
208+
renderNode={renderNode}
209+
data={null}
210+
/>
211+
)
212+
.toJSON()
213+
expect(nestedListView).toMatchSnapshot()
214+
})
215+
216+
test('renders empty data', () => {
217+
const data = [null, null, null]
218+
const nestedListView = renderer
219+
.create(
220+
<NestedListView
221+
getChildrenName={() => 'items'}
222+
renderNode={renderNode}
223+
data={data}
224+
/>
225+
)
226+
.toJSON()
227+
expect(nestedListView).toMatchSnapshot()
228+
})
186229
})

src/NestedListView.tsx

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const styles = StyleSheet.create({
2525
export interface IProps {
2626
data: any
2727
extraData?: any
28-
renderNode: (elem: any, level?: number) => any
28+
renderNode: (elem: INode, level?: number) => any
2929
onNodePressed?: (node?: INode) => void
3030
getChildrenName: (elem: any) => any
3131
style?: any
@@ -39,7 +39,9 @@ const NestedListView = React.memo(
3939
({getChildrenName, renderNode, data, onNodePressed, extraData}: IProps) => {
4040
const generateIds = (node?: INode) => {
4141
if (!node) {
42-
return
42+
return {
43+
_internalId: shortid.generate(),
44+
}
4345
}
4446

4547
const childrenName: string = getChildrenName(node) || 'items'
@@ -114,10 +116,6 @@ const NestedListView = React.memo(
114116
)
115117
}
116118

117-
if (!getChildrenName) {
118-
return renderErrorMessage('getChildrenName')
119-
}
120-
121119
if (!renderNode) {
122120
return renderErrorMessage('renderNode')
123121
}

src/NestedRow.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,31 @@ describe('NestedListView', () => {
1818
.toJSON()
1919
expect(nestedListView).toMatchSnapshot()
2020
})
21+
22+
test('renders with simple array without level', () => {
23+
const nestedListView = renderer
24+
.create(
25+
<NestedRow style={{borderColor: 'black', borderWidth: 1}}>
26+
<Text>Test</Text>
27+
</NestedRow>
28+
)
29+
.toJSON()
30+
expect(nestedListView).toMatchSnapshot()
31+
})
32+
33+
test('renders customised', () => {
34+
const nestedListView = renderer
35+
.create(
36+
<NestedRow
37+
level={1}
38+
height={60}
39+
paddingLeftIncrement={15}
40+
style={{borderColor: 'black', borderWidth: 1}}
41+
>
42+
<Text>Test</Text>
43+
</NestedRow>
44+
)
45+
.toJSON()
46+
expect(nestedListView).toMatchSnapshot()
47+
})
2148
})

src/NestedRow.tsx

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,20 @@ const NestedRow = React.memo(
2626
level = 0,
2727
paddingLeftIncrement = 10,
2828
style,
29-
}: IProps) => {
30-
return (
31-
<View
32-
style={[
33-
styles.nestedRow,
34-
{
35-
...style,
36-
height,
37-
paddingLeft: level * paddingLeftIncrement,
38-
},
39-
]}
40-
>
41-
{children}
42-
</View>
43-
)
44-
},
29+
}: IProps) => (
30+
<View
31+
style={[
32+
styles.nestedRow,
33+
{
34+
...style,
35+
height,
36+
paddingLeft: level * paddingLeftIncrement,
37+
},
38+
]}
39+
>
40+
{children}
41+
</View>
42+
),
4543
isEqual
4644
)
4745

src/__snapshots__/NestedListView.test.tsx.snap

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,108 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`NestedListView renders empty data 1`] = `
4+
<RCTScrollView
5+
data={
6+
Array [
7+
Object {
8+
"_internalId": "1",
9+
},
10+
Object {
11+
"_internalId": "1",
12+
},
13+
Object {
14+
"_internalId": "1",
15+
},
16+
]
17+
}
18+
disableVirtualization={false}
19+
getItem={[Function]}
20+
getItemCount={[Function]}
21+
horizontal={false}
22+
initialNumToRender={10}
23+
keyExtractor={[Function]}
24+
maxToRenderPerBatch={10}
25+
numColumns={1}
26+
onContentSizeChange={[Function]}
27+
onEndReachedThreshold={2}
28+
onLayout={[Function]}
29+
onMomentumScrollEnd={[Function]}
30+
onScroll={[Function]}
31+
onScrollBeginDrag={[Function]}
32+
onScrollEndDrag={[Function]}
33+
removeClippedSubviews={false}
34+
renderItem={[Function]}
35+
scrollEventThrottle={50}
36+
stickyHeaderIndices={Array []}
37+
updateCellsBatchingPeriod={50}
38+
viewabilityConfigCallbackPairs={Array []}
39+
windowSize={21}
40+
>
41+
<View>
42+
<View
43+
onLayout={[Function]}
44+
style={null}
45+
>
46+
<View
47+
accessible={true}
48+
clickable={true}
49+
onClick={[Function]}
50+
onResponderGrant={[Function]}
51+
onResponderMove={[Function]}
52+
onResponderRelease={[Function]}
53+
onResponderTerminate={[Function]}
54+
onResponderTerminationRequest={[Function]}
55+
onStartShouldSetResponder={[Function]}
56+
>
57+
<View>
58+
<Text />
59+
</View>
60+
</View>
61+
</View>
62+
<View
63+
onLayout={[Function]}
64+
style={null}
65+
>
66+
<View
67+
accessible={true}
68+
clickable={true}
69+
onClick={[Function]}
70+
onResponderGrant={[Function]}
71+
onResponderMove={[Function]}
72+
onResponderRelease={[Function]}
73+
onResponderTerminate={[Function]}
74+
onResponderTerminationRequest={[Function]}
75+
onStartShouldSetResponder={[Function]}
76+
>
77+
<View>
78+
<Text />
79+
</View>
80+
</View>
81+
</View>
82+
<View
83+
onLayout={[Function]}
84+
style={null}
85+
>
86+
<View
87+
accessible={true}
88+
clickable={true}
89+
onClick={[Function]}
90+
onResponderGrant={[Function]}
91+
onResponderMove={[Function]}
92+
onResponderRelease={[Function]}
93+
onResponderTerminate={[Function]}
94+
onResponderTerminationRequest={[Function]}
95+
onStartShouldSetResponder={[Function]}
96+
>
97+
<View>
98+
<Text />
99+
</View>
100+
</View>
101+
</View>
102+
</View>
103+
</RCTScrollView>
104+
`;
105+
3106
exports[`NestedListView renders with NestedRow 1`] = `
4107
<RCTScrollView
5108
data={
@@ -815,3 +918,61 @@ exports[`NestedListView renders with simple array 1`] = `
815918
</View>
816919
</RCTScrollView>
817920
`;
921+
922+
exports[`NestedListView renders without data 1`] = `
923+
<View
924+
style={
925+
Object {
926+
"alignItems": "center",
927+
"backgroundColor": "rgb(237, 57, 40)",
928+
"borderColor": "rgb(84, 85, 86)",
929+
"borderWidth": 1,
930+
"height": 60,
931+
"justifyContent": "center",
932+
}
933+
}
934+
>
935+
<Text
936+
style={
937+
Object {
938+
"color": "rgb(255, 255, 255)",
939+
"fontSize": 17,
940+
"fontWeight": "bold",
941+
}
942+
}
943+
>
944+
prop
945+
data
946+
has not been passed
947+
</Text>
948+
</View>
949+
`;
950+
951+
exports[`NestedListView renders without renderNode 1`] = `
952+
<View
953+
style={
954+
Object {
955+
"alignItems": "center",
956+
"backgroundColor": "rgb(237, 57, 40)",
957+
"borderColor": "rgb(84, 85, 86)",
958+
"borderWidth": 1,
959+
"height": 60,
960+
"justifyContent": "center",
961+
}
962+
}
963+
>
964+
<Text
965+
style={
966+
Object {
967+
"color": "rgb(255, 255, 255)",
968+
"fontSize": 17,
969+
"fontWeight": "bold",
970+
}
971+
}
972+
>
973+
prop
974+
renderNode
975+
has not been passed
976+
</Text>
977+
</View>
978+
`;

src/__snapshots__/NestedRow.test.tsx.snap

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

3+
exports[`NestedListView renders customised 1`] = `
4+
<View
5+
style={
6+
Array [
7+
Object {
8+
"flex": 1,
9+
"justifyContent": "center",
10+
},
11+
Object {
12+
"borderColor": "black",
13+
"borderWidth": 1,
14+
"height": 60,
15+
"paddingLeft": 15,
16+
},
17+
]
18+
}
19+
>
20+
<Text>
21+
Test
22+
</Text>
23+
</View>
24+
`;
25+
326
exports[`NestedListView renders with simple array 1`] = `
427
<View
528
style={
@@ -22,3 +45,26 @@ exports[`NestedListView renders with simple array 1`] = `
2245
</Text>
2346
</View>
2447
`;
48+
49+
exports[`NestedListView renders with simple array without level 1`] = `
50+
<View
51+
style={
52+
Array [
53+
Object {
54+
"flex": 1,
55+
"justifyContent": "center",
56+
},
57+
Object {
58+
"borderColor": "black",
59+
"borderWidth": 1,
60+
"height": 50,
61+
"paddingLeft": 0,
62+
},
63+
]
64+
}
65+
>
66+
<Text>
67+
Test
68+
</Text>
69+
</View>
70+
`;

0 commit comments

Comments
 (0)