Coding task · JavaScript and React course · Module 13: Testing React
Practice: Data Table
A snapshot would only show that the table structure changed, while a behaviour test says exactly what should happen: after a click on the Moons header, the planet without moons comes first. React Testing Library sees the table through roles: table, row, columnheader and cell. aria-sort on a header tells a screen reader the sort direction. The test passes a frozen array (Object.freeze), so sorting without a copy ends with an error.
Task
Finish the DataTable table of the planet catalogue. The column headers are buttons inside th, and th has aria-sort ('ascending', 'descending' or 'none'). ___BLANK1___: before sorting, make a copy of the rows array, because sort() changes the array you call it on, and props must not be changed. ___BLANK2___: a second click on the same header changes the direction to 'descending' (the next one back to 'ascending'). ___BLANK3___: when the rows array is empty, a message with role="status" is shown instead of the table. The test clicks the headers, reads the order of the row cells (the row and cell roles) and checks aria-sort.