useSorts.spec.tsx 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import {act, render} from 'sentry-test/reactTestingLibrary';
  2. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  3. describe('useSorts', function () {
  4. it('allows changing sorts', function () {
  5. let sorts, setSorts;
  6. const fields = ['id', 'timestamp'];
  7. function TestPage() {
  8. [sorts, setSorts] = useSorts({fields});
  9. return null;
  10. }
  11. render(<TestPage />, {disableRouterMocks: true});
  12. expect(sorts).toEqual([
  13. {
  14. kind: 'desc',
  15. field: 'timestamp',
  16. },
  17. ]); // default
  18. act(() =>
  19. setSorts([
  20. {
  21. kind: 'asc',
  22. field: 'timestamp',
  23. },
  24. ])
  25. );
  26. expect(sorts).toEqual([
  27. {
  28. kind: 'asc',
  29. field: 'timestamp',
  30. },
  31. ]);
  32. act(() =>
  33. setSorts([
  34. {
  35. kind: 'desc',
  36. field: 'id',
  37. },
  38. ])
  39. );
  40. expect(sorts).toEqual([
  41. {
  42. kind: 'desc',
  43. field: 'id',
  44. },
  45. ]);
  46. });
  47. it('falls back to timestamp desc if possible', function () {
  48. let sorts, setSorts;
  49. const fields = ['id', 'timestamp'];
  50. function TestPage() {
  51. [sorts, setSorts] = useSorts({fields});
  52. return null;
  53. }
  54. render(<TestPage />, {disableRouterMocks: true});
  55. act(() =>
  56. setSorts([
  57. {
  58. kind: 'asc',
  59. field: 'foo',
  60. },
  61. ])
  62. );
  63. expect(sorts).toEqual([
  64. {
  65. kind: 'desc',
  66. field: 'timestamp',
  67. },
  68. ]);
  69. });
  70. it('falls back to first column desc if timestamp is not available', function () {
  71. let sorts, setSorts;
  72. const fields = ['id'];
  73. function TestPage() {
  74. [sorts, setSorts] = useSorts({fields});
  75. return null;
  76. }
  77. render(<TestPage />, {disableRouterMocks: true});
  78. act(() =>
  79. setSorts([
  80. {
  81. kind: 'asc',
  82. field: 'foo',
  83. },
  84. ])
  85. );
  86. expect(sorts).toEqual([
  87. {
  88. kind: 'desc',
  89. field: 'id',
  90. },
  91. ]);
  92. });
  93. });