Skip to content
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

fix: bug in sort subRows data and add test in useSortBy #2633

Merged
merged 5 commits into from Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 55 additions & 3 deletions src/plugin-hooks/tests/useSortBy.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,42 @@ const data = [
},
],
},
{
firstName: 'peter',
lastName: 'zhang',
age: 30,
visits: 82,
status: 'Married',
progress: 30,
subRows: [
{
firstName: 'linda',
lastName: 'zhang',
age: 20,
visits: 120,
status: 'Single',
progress: 60,
subRows: [
{
firstName: 'robert',
lastName: 'zhang',
age: 26,
visits: 20,
status: 'Single',
progress: 40,
},
{
firstName: 'james',
lastName: 'zhang',
age: 35,
visits: 23,
status: 'Complicated',
progress: 20,
},
],
},
],
},
]

const defaultColumn = {
Expand Down Expand Up @@ -175,6 +211,7 @@ test('renders a sortable table', () => {
'firstName: derek',
'firstName: joe',
'firstName: john',
'firstName: peter',
'firstName: tanner',
])

Expand All @@ -187,6 +224,7 @@ test('renders a sortable table', () => {
.map(d => d.children[0].textContent)
).toEqual([
'firstName: tanner',
'firstName: peter',
'firstName: john',
'firstName: joe',
'firstName: derek',
Expand All @@ -202,6 +240,7 @@ test('renders a sortable table', () => {
).toEqual([
'firstName: joe',
'firstName: john',
'firstName: peter',
'firstName: tanner',
'firstName: derek',
])
Expand All @@ -217,6 +256,7 @@ test('renders a sortable table', () => {
).toEqual([
'firstName: joe',
'firstName: john',
'firstName: peter',
'firstName: derek',
'firstName: tanner',
])
Expand All @@ -228,10 +268,20 @@ test('maintains the integrity of instance.flatRows', () => {

fireEvent.click(rendered.getByText('First Name'))
const flatRows = useTableRef.current.flatRows
expect(flatRows.length).toBe(5)
expect(flatRows.length).toBe(9)
expect(
flatRows.find(r => r.values.firstName === 'winston')
).not.toBeUndefined()
flatRows.map(r => r.values.firstName)
).toEqual([
'derek',
'joe',
'john',
'winston',
'peter',
'linda',
'james',
'robert',
'tanner',
])
})

test('Test initialState.sortBy: When clicking the last sortBy column, the sorted state will be replaced not toggled', () => {
Expand All @@ -252,6 +302,7 @@ test('Test initialState.sortBy: When clicking the last sortBy column, the sorted
.map(d => d.children[0].textContent)
).toEqual([
'firstName: tanner',
'firstName: peter',
'firstName: derek',
'firstName: joe',
'firstName: john',
Expand All @@ -268,6 +319,7 @@ test('Test initialState.sortBy: When clicking the last sortBy column, the sorted
'firstName: john',
'firstName: joe',
'firstName: derek',
'firstName: peter',
'firstName: tanner',
])

Expand Down
5 changes: 1 addition & 4 deletions src/plugin-hooks/useSortBy.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,10 +337,7 @@ function useInstance(instance) {
// If there are sub-rows, sort them
sortedData.forEach(row => {
sortedFlatRows.push(row)
if (!row.subRows) {
return
} else if (row.subRows.length === 1) {
sortedFlatRows.push(row.subRows[0])
if (!row.subRows || row.subRows.length < 1) {
tannerlinsley marked this conversation as resolved.
Show resolved Hide resolved
return
}
row.subRows = sortData(row.subRows)
Expand Down