I recently found a bug when sorting table columns, while running IE9. Ignoring the obvious fix, I wrote up the bug and then as I’m a fan of doing, I wrote a failing automated test to test it (Defect Driven Development!).
Testing sort proved a bit tricky… I thought I would share the results to perhaps save others same pain. I would also not be surprised to find a more elegant solution out there. If you have one, do feel free to share!
My example is in Sahi but it should be easy to transfer to your tool of choice. The gist is:
- Sort your column
- Collect all the elements in the column in an array
- Compare the two arrays
And here’s the Sahi code…
Name table header to get our initial sort of that column.
Then we collect each element in the
Name column, in
table0 and store them in an array,
$appSortedValues. To iterate through our loop, we get the number of rows in
table0 by counting the number of rows and subtract 1 for each table header.
Now we need a copy of our
$jsSortedValues = $appSortedValues; . This will actually create a reference to our original array; not what we want. Instead we use the
slice() method to select elements 0 through the end of the array and put them all in our new array, thus copying it.
sort() method to sort the copy of our array,
$jsSortedValues. But the
sort() method has a little wrinkle; by default, it sorts alphabetically and is case sensitive. This is unlikely to be how your application’s sort works… but luckily, you can roll your own sort filter by passing a function as an argument to the
sort() method. In our case, we want it to be case-insensitive, hence our function
Now we have two arrays and can simply use Sahi’s _assertEqual method to verify both arrays are sorted in the same order.