From 8c5475fd73198755bc6db5d0e7c7853115a8fe30 Mon Sep 17 00:00:00 2001 From: github-actions Date: Wed, 23 Jun 2021 16:51:48 -0700 Subject: [PATCH] Fix tests and refactor compareString --- .../Table/__tests__/Table.sort.test.ts | 22 ++++++++++++++++++ src/components/Table/__tests__/utils.test.tsx | 13 ++++++++++- .../Table/fixtures/0_sample_data.ts | 23 ++++++++++++++----- src/components/Table/utils.tsx | 12 +++++++--- 4 files changed, 60 insertions(+), 10 deletions(-) diff --git a/src/components/Table/__tests__/Table.sort.test.ts b/src/components/Table/__tests__/Table.sort.test.ts index e66a803a..0f835c01 100644 --- a/src/components/Table/__tests__/Table.sort.test.ts +++ b/src/components/Table/__tests__/Table.sort.test.ts @@ -75,6 +75,28 @@ describe('Table sort: Column type - "string', () => { ]) }) + it('sorts columns with array values in correct ascending and correct descending order', () => { + const sorter = wrapper.find('thead').find('th').at(2) + + const ascendingOrder = [ + ['apples, bananas'], + ['blueberries, peaches'], + ['mangoes, papayas'], + ['nectarines, plums, raspberries'], + ['oranges, pears'] + ] + + // ascending order + sorter.simulate('click') + expect(renderedData(wrapper, 'description')).toEqual(ascendingOrder) + + // descending order + sorter.simulate('click') + expect(renderedData(wrapper, 'description')).toEqual( + ascendingOrder.reverse() + ) + }) + it('sorts columns with missing cells in correct ascending and correct descending order', () => { wrapper = mount(createTable(mockData3)) const sorter = wrapper.find('.ant-table-column-has-sorters').first() diff --git a/src/components/Table/__tests__/utils.test.tsx b/src/components/Table/__tests__/utils.test.tsx index 8590be86..bc8afb93 100644 --- a/src/components/Table/__tests__/utils.test.tsx +++ b/src/components/Table/__tests__/utils.test.tsx @@ -64,6 +64,12 @@ describe('processColumns', () => { showSorterTooltip: false, sorter: expect.any(Function), title: 'Age' + }, + { + dataIndex: 'description', + showSorterTooltip: false, + sorter: expect.any(Function), + title: 'Description' } ] @@ -168,7 +174,12 @@ describe('processData', () => { describe('mapFilterKeys', () => { it('returns all column keys that should be searched or filtered on', () => { - const mockFilteredKeys0 = ['_FORMATTED_DATA', 'name', 'age'] + const mockFilteredKeys0 = [ + '_FORMATTED_DATA', + 'name', + 'age', + 'description' + ] const mockFilteredKeys2 = [ '_FORMATTED_DATA', 'name', diff --git a/src/components/Table/fixtures/0_sample_data.ts b/src/components/Table/fixtures/0_sample_data.ts index 96cede94..926c1ca9 100644 --- a/src/components/Table/fixtures/0_sample_data.ts +++ b/src/components/Table/fixtures/0_sample_data.ts @@ -1,9 +1,10 @@ import { ColumnType, ColumnTypes, TableProps } from '../.' export interface Person { - name: string[] + name: string id: number | string age: number + description: string[] } const { number, string } = ColumnTypes @@ -18,34 +19,44 @@ const columns: ColumnType[] = [ dataIndex: 'age', title: 'Age', type: number + }, + { + dataIndex: 'description', + title: 'Description', + type: string } ] const data: Person[] = [ { age: 36, + description: ['apples, bananas'], id: 0, - name: ['Lorem', 'Ipsum'] + name: 'Lorem' }, { age: 32, + description: ['oranges, pears'], id: 1, - name: ['Ipsum'] + name: 'Ipsum' }, { age: 45, + description: ['blueberries, peaches'], id: 2, - name: ['Amet'] + name: 'Amet' }, { age: 50, + description: ['nectarines, plums, raspberries'], id: 3, - name: ['Elit'] + name: 'Elit' }, { age: 22, + description: ['mangoes, papayas'], id: 4, - name: ['Dolor'] + name: 'Dolor' } ] diff --git a/src/components/Table/utils.tsx b/src/components/Table/utils.tsx index d8bfc23c..49e3e552 100644 --- a/src/components/Table/utils.tsx +++ b/src/components/Table/utils.tsx @@ -193,16 +193,22 @@ const compareIcons = return compareValA.localeCompare(compareValB) } +const getStrVal = (value?: string | string[]) => { + if (!value) return '' + + return Array.isArray(value) ? value.join(', ') : value +} + /* Compare functions used by applySort to pass a custom sorter based on data type and format. */ function compareStrings(column: ColumnType) { return (a: Record, b: Record) => { - const compareValA: string = a[column.dataIndex] || '' - const compareValB: string = b[column.dataIndex] || '' + const valA = getStrVal(a[column.dataIndex]) + const valB = getStrVal(b[column.dataIndex]) - return compareValA.localeCompare(compareValB) + return valA.localeCompare(valB) } }