-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
164 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import { mount } from 'enzyme'; | ||
import { useState } from 'preact/hooks'; | ||
|
||
import { useOrderedRows } from '../use-ordered-rows'; | ||
|
||
const starWarsCharacters = [ | ||
{ name: 'Luck Skywalker', age: 20 }, | ||
{ name: 'Princess Leia Organa', age: 20 }, | ||
{ name: 'Han Solo', age: 25 }, | ||
]; | ||
|
||
describe('useOrderedRows', () => { | ||
function FakeComponent() { | ||
const [order, setOrder] = useState(); | ||
const orderedRows = useOrderedRows(starWarsCharacters, order); | ||
|
||
return ( | ||
<div> | ||
{orderedRows.map((character, index) => ( | ||
<div key={`${character.name}${index}`}> | ||
<span data-testid={`name-${index}`}>{character.name}</span> | ||
<span data-testid={`age-${index}`}>{character.age}</span> | ||
</div> | ||
))} | ||
<button | ||
data-testid="button-order-by-name-asc" | ||
onClick={() => setOrder({ field: 'name', direction: 'ascending' })} | ||
> | ||
By name ASC | ||
</button> | ||
<button | ||
data-testid="button-order-by-name-desc" | ||
onClick={() => setOrder({ field: 'name', direction: 'descending' })} | ||
> | ||
By name DES | ||
</button> | ||
<button | ||
data-testid="button-order-by-age-asc" | ||
onClick={() => setOrder({ field: 'age', direction: 'ascending' })} | ||
> | ||
By age ASC | ||
</button> | ||
<button | ||
data-testid="button-order-by-age-desc" | ||
onClick={() => setOrder({ field: 'age', direction: 'descending' })} | ||
> | ||
By age DES | ||
</button> | ||
<button | ||
data-testid="button-reset-order" | ||
onClick={() => setOrder(undefined)} | ||
> | ||
Reset order | ||
</button> | ||
</div> | ||
); | ||
} | ||
|
||
function createComponent() { | ||
return mount(<FakeComponent />); | ||
} | ||
|
||
function assertDefaultOrder(wrapper) { | ||
assertOrder(wrapper, starWarsCharacters); | ||
} | ||
|
||
function assertOrder(wrapper, rows) { | ||
rows.forEach((character, index) => { | ||
assert.equal( | ||
character.name, | ||
wrapper.find(`[data-testid="name-${index}"]`).text(), | ||
); | ||
assert.equal( | ||
character.age, | ||
wrapper.find(`[data-testid="age-${index}"]`).text(), | ||
); | ||
}); | ||
} | ||
|
||
[ | ||
{ | ||
orderId: 'button-order-by-name-asc', | ||
expectedRows: [ | ||
{ name: 'Han Solo', age: 25 }, | ||
{ name: 'Luck Skywalker', age: 20 }, | ||
{ name: 'Princess Leia Organa', age: 20 }, | ||
], | ||
}, | ||
{ | ||
orderId: 'button-order-by-name-desc', | ||
expectedRows: [ | ||
{ name: 'Princess Leia Organa', age: 20 }, | ||
{ name: 'Luck Skywalker', age: 20 }, | ||
{ name: 'Han Solo', age: 25 }, | ||
], | ||
}, | ||
{ | ||
orderId: 'button-order-by-age-asc', | ||
expectedRows: [ | ||
{ name: 'Luck Skywalker', age: 20 }, | ||
{ name: 'Princess Leia Organa', age: 20 }, | ||
{ name: 'Han Solo', age: 25 }, | ||
], | ||
}, | ||
{ | ||
orderId: 'button-order-by-age-desc', | ||
expectedRows: [ | ||
{ name: 'Han Solo', age: 25 }, | ||
{ name: 'Luck Skywalker', age: 20 }, | ||
{ name: 'Princess Leia Organa', age: 20 }, | ||
], | ||
}, | ||
].forEach(({ orderId, expectedRows }) => { | ||
it('orders rows based on field and direction', () => { | ||
const wrapper = createComponent(); | ||
|
||
// Rows are initially not ordered | ||
assertDefaultOrder(wrapper); | ||
|
||
// Click button to order | ||
wrapper.find(`[data-testid="${orderId}"]`).simulate('click'); | ||
assertOrder(wrapper, expectedRows); | ||
|
||
// Order can be reset | ||
wrapper.find('[data-testid="button-reset-order"]').simulate('click'); | ||
assertDefaultOrder(wrapper); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useMemo } from 'preact/hooks'; | ||
|
||
import type { Order } from '../types'; | ||
|
||
export function useOrderedRows<Row>(rows: Row[], order?: Order<keyof Row>) { | ||
return useMemo(() => { | ||
if (!order) { | ||
return rows; | ||
} | ||
|
||
return [...rows].sort((a, b) => { | ||
if (a[order.field] === b[order.field]) { | ||
return 0; | ||
} | ||
|
||
if (order.direction === 'ascending') { | ||
return a[order.field] > b[order.field] ? 1 : -1; | ||
} | ||
|
||
return a[order.field] > b[order.field] ? -1 : 1; | ||
}); | ||
}, [order, rows]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters