Skip to content

Commit 12f787b

Browse files
committed
fix(tree): Enhance unit tests
1 parent bdd5a8e commit 12f787b

File tree

9 files changed

+2444
-48
lines changed

9 files changed

+2444
-48
lines changed
Lines changed: 53 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,80 @@
11
import React from 'react';
22
import { render } from '@testing-library/react';
3-
import { DataViewTable } from './DataViewTable';
3+
import { DataViewTable, DataViewTrTree } from './DataViewTable';
44

55
interface Repository {
66
name: string;
77
branches: string | null;
88
prs: string | null;
99
workspaces: string;
1010
lastCommit: string;
11+
children?: Repository[];
1112
}
1213

1314
const repositories: Repository[] = [
14-
{ name: 'one', branches: 'two', prs: 'three', workspaces: 'four', lastCommit: 'five' },
15-
{ name: 'one - 2', branches: null, prs: null, workspaces: 'four - 2', lastCommit: 'five - 2' },
16-
{ name: 'one - 3', branches: 'two - 3', prs: 'three - 3', workspaces: 'four - 3', lastCommit: 'five - 3' },
17-
{ name: 'one - 4', branches: 'two - 4', prs: 'null', workspaces: 'four - 4', lastCommit: 'five - 4' },
18-
{ name: 'one - 5', branches: 'two - 5', prs: 'three - 5', workspaces: 'four - 5', lastCommit: 'five - 5' },
19-
{ name: 'one - 6', branches: 'two - 6', prs: 'three - 6', workspaces: 'four - 6', lastCommit: 'five - 6' }
15+
{ name: 'Repository one', branches: 'Branch one', prs: 'Pull request one', workspaces: 'Workspace one', lastCommit: 'Timestamp one' },
16+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
17+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
18+
{ name: 'Repository four', branches: 'Branch four', prs: 'Pull request four', workspaces: 'Workspace four', lastCommit: 'Timestamp four' },
19+
{ name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' },
20+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
2021
];
2122

22-
const rows = repositories.map(repo => ({
23-
row: Object.values(repo),
23+
const rows = repositories.map(repo => ({ row: Object.values(repo) }));
24+
25+
const repositoriesTree: Repository[] = [
26+
{
27+
name: 'Repository one',
28+
branches: 'Branch one',
29+
prs: 'Pull request one',
30+
workspaces: 'Workspace one',
31+
lastCommit: 'Timestamp one',
32+
children: [
33+
{ name: 'Repository two', branches: 'Branch two', prs: 'Pull request two', workspaces: 'Workspace two', lastCommit: 'Timestamp two' },
34+
{ name: 'Repository three', branches: 'Branch three', prs: 'Pull request three', workspaces: 'Workspace three', lastCommit: 'Timestamp three' },
35+
]
36+
},
37+
{
38+
name: 'Repository four',
39+
branches: 'Branch four',
40+
prs: 'Pull request four',
41+
workspaces: 'Workspace four',
42+
lastCommit: 'Timestamp four',
43+
children: [ { name: 'Repository five', branches: 'Branch five', prs: 'Pull request five', workspaces: 'Workspace five', lastCommit: 'Timestamp five' } ]
44+
},
45+
{ name: 'Repository six', branches: 'Branch six', prs: 'Pull request six', workspaces: 'Workspace six', lastCommit: 'Timestamp six' }
46+
];
47+
48+
49+
50+
const buildRows = (repositories: Repository[]): DataViewTrTree[] => repositories.map((repo) => ({
51+
row: [ repo.name, repo.branches, repo.prs, repo.workspaces, repo.lastCommit ],
52+
id: repo.name, // unique ID for each row
53+
...(repo.children
54+
? {
55+
children: buildRows(repo.children) // build rows for children
56+
}
57+
: {})
2458
}));
2559

60+
const treeRows = buildRows(repositoriesTree);
61+
2662
const columns = [ 'Repositories', 'Branches', 'Pull requests', 'Workspaces', 'Last commit' ];
2763

2864
const ouiaId = 'TableExample';
2965

3066
describe('DataViewTable component', () => {
31-
test('should render correctly', () => {
67+
test('should render a basic table correctly', () => {
3268
const { container } = render(
3369
<DataViewTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={rows} />
3470
);
3571
expect(container).toMatchSnapshot();
3672
});
73+
74+
test('should render a tree table correctly', () => {
75+
const { container } = render(
76+
<DataViewTable isTreeTable aria-label='Repositories table' ouiaId={ouiaId} columns={columns} rows={treeRows} />
77+
);
78+
expect(container).toMatchSnapshot();
79+
});
3780
});

0 commit comments

Comments
 (0)