Skip to content

Commit 298e85b

Browse files
authored
Merge pull request #481 from yaob421123/yb
test(Table): add test
2 parents 4172da5 + c2875c5 commit 298e85b

File tree

2 files changed

+58
-13
lines changed

2 files changed

+58
-13
lines changed

packages/core/src/Table/index.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ const Table = ({ data, columns, rowKey, horizontal = true, style }: TableProps)
2626
};
2727

2828
return (
29-
<ScrollView style={[styles.conW, style]} horizontal={horizontal}>
29+
<ScrollView testID="RNE__Table__wrap" style={[styles.conW, style]} horizontal={horizontal}>
3030
<ScrollView horizontal={!horizontal}>
31-
<View style={styles.conTitle}>
31+
<View testID="RNE__Table__header" style={styles.conTitle}>
3232
{columns.map((itm, idx) => (
3333
<View
3434
key={itm.dataIndex + idx}
@@ -38,17 +38,19 @@ const Table = ({ data, columns, rowKey, horizontal = true, style }: TableProps)
3838
</View>
3939
))}
4040
</View>
41-
{data.map((item, idx) => {
42-
const key = getRowKey(item);
43-
return (
44-
<BodyRow
45-
key={key}
46-
columns={columns}
47-
record={item}
48-
style={{ borderBottomWidth: idx === data.length - 1 ? 1 : 1 }}
49-
/>
50-
);
51-
})}
41+
<View testID="RNE__Table__body">
42+
{data.map((item, idx) => {
43+
const key = getRowKey(item);
44+
return (
45+
<BodyRow
46+
key={key}
47+
columns={columns}
48+
record={item}
49+
style={{ borderBottomWidth: idx === data.length - 1 ? 1 : 1 }}
50+
/>
51+
);
52+
})}
53+
</View>
5254
{data.length === 0 && <Text style={styles.noDataText}>暂无数据...</Text>}
5355
</ScrollView>
5456
</ScrollView>

test-ci/src/__tests__/table.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import 'react-native';
2+
import React from 'react';
3+
import Table from '../lib/Table';
4+
import { render } from '@testing-library/react-native';
5+
import { toObject } from '../utils';
6+
7+
describe('Table', () => {
8+
it('columns', () => {
9+
const columns = [
10+
{ title: '姓名', dataIndex: 'name' },
11+
{ title: '性别', dataIndex: 'sex' },
12+
{ title: '地址', dataIndex: 'address' },
13+
];
14+
const { getByTestId } = render(<Table columns={columns} data={[]} rowKey="id" />);
15+
const component = getByTestId('RNE__Table__header');
16+
expect(component.props.children).toHaveLength(3);
17+
});
18+
19+
it('data', () => {
20+
const data = [{ id: 1 }, { id: 2 }];
21+
const { getByTestId } = render(<Table columns={[]} data={data} rowKey="id" />);
22+
const component = getByTestId('RNE__Table__body');
23+
expect(component.props.children).toHaveLength(2);
24+
});
25+
26+
it('no data', () => {
27+
const { getByText } = render(<Table columns={[]} data={[]} rowKey="id" />);
28+
expect(getByText('暂无数据...')).toBeTruthy();
29+
});
30+
31+
it('horizontal', () => {
32+
const { getByTestId } = render(<Table columns={[]} data={[]} rowKey="id" horizontal={false} />);
33+
const component = getByTestId('RNE__Table__wrap');
34+
expect(component.props.horizontal).toBe(false);
35+
});
36+
37+
it('style', () => {
38+
const { getByTestId } = render(<Table columns={[]} data={[]} rowKey="id" style={{ fontSize: 14 }} />);
39+
const component = getByTestId('RNE__Table__wrap');
40+
const styles = toObject(component.props.style);
41+
expect(styles.fontSize).toBe(14);
42+
});
43+
});

0 commit comments

Comments
 (0)