useSort.spec.tsx 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
  2. import {act, render} from 'sentry-test/reactTestingLibrary';
  3. import {useSort} from 'sentry/views/explore/hooks/useSort';
  4. import {RouteContext} from 'sentry/views/routeContext';
  5. describe('useSort', function () {
  6. it('allows changing sort', function () {
  7. let sort, setSort;
  8. const fields = ['id', 'timestamp'];
  9. function TestPage() {
  10. [sort, setSort] = useSort({fields});
  11. return null;
  12. }
  13. const memoryHistory = createMemoryHistory();
  14. render(
  15. <Router
  16. history={memoryHistory}
  17. render={props => {
  18. return (
  19. <RouteContext.Provider value={props}>
  20. <RouterContext {...props} />
  21. </RouteContext.Provider>
  22. );
  23. }}
  24. >
  25. <Route path="/" component={TestPage} />
  26. </Router>
  27. );
  28. expect(sort).toEqual({
  29. direction: 'desc',
  30. field: 'timestamp',
  31. }); // default
  32. act(() =>
  33. setSort({
  34. direction: 'asc',
  35. field: 'timestamp',
  36. })
  37. );
  38. expect(sort).toEqual({
  39. direction: 'asc',
  40. field: 'timestamp',
  41. });
  42. act(() =>
  43. setSort({
  44. direction: 'desc',
  45. field: 'id',
  46. })
  47. );
  48. expect(sort).toEqual({
  49. direction: 'desc',
  50. field: 'id',
  51. });
  52. act(() =>
  53. setSort({
  54. direction: 'asc',
  55. field: 'foo',
  56. })
  57. );
  58. expect(sort).toEqual({
  59. direction: 'asc',
  60. field: 'id',
  61. });
  62. });
  63. });