useSorts.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {createMemoryHistory, Route, Router, RouterContext} from 'react-router';
  2. import {act, render} from 'sentry-test/reactTestingLibrary';
  3. import {useSorts} from 'sentry/views/explore/hooks/useSorts';
  4. import {RouteContext} from 'sentry/views/routeContext';
  5. describe('useSorts', function () {
  6. it('allows changing sorts', function () {
  7. let sorts, setSorts;
  8. const fields = ['id', 'timestamp'];
  9. function TestPage() {
  10. [sorts, setSorts] = useSorts({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(sorts).toEqual([
  29. {
  30. kind: 'desc',
  31. field: 'timestamp',
  32. },
  33. ]); // default
  34. act(() =>
  35. setSorts([
  36. {
  37. kind: 'asc',
  38. field: 'timestamp',
  39. },
  40. ])
  41. );
  42. expect(sorts).toEqual([
  43. {
  44. kind: 'asc',
  45. field: 'timestamp',
  46. },
  47. ]);
  48. act(() =>
  49. setSorts([
  50. {
  51. kind: 'desc',
  52. field: 'id',
  53. },
  54. ])
  55. );
  56. expect(sorts).toEqual([
  57. {
  58. kind: 'desc',
  59. field: 'id',
  60. },
  61. ]);
  62. });
  63. it('falls back to timestamp desc if possible', function () {
  64. let sorts, setSorts;
  65. const fields = ['id', 'timestamp'];
  66. function TestPage() {
  67. [sorts, setSorts] = useSorts({fields});
  68. return null;
  69. }
  70. const memoryHistory = createMemoryHistory();
  71. render(
  72. <Router
  73. history={memoryHistory}
  74. render={props => {
  75. return (
  76. <RouteContext.Provider value={props}>
  77. <RouterContext {...props} />
  78. </RouteContext.Provider>
  79. );
  80. }}
  81. >
  82. <Route path="/" component={TestPage} />
  83. </Router>
  84. );
  85. act(() =>
  86. setSorts([
  87. {
  88. kind: 'asc',
  89. field: 'foo',
  90. },
  91. ])
  92. );
  93. expect(sorts).toEqual([
  94. {
  95. kind: 'desc',
  96. field: 'timestamp',
  97. },
  98. ]);
  99. });
  100. it('falls back to first column desc if timestamp is not available', function () {
  101. let sorts, setSorts;
  102. const fields = ['id'];
  103. function TestPage() {
  104. [sorts, setSorts] = useSorts({fields});
  105. return null;
  106. }
  107. const memoryHistory = createMemoryHistory();
  108. render(
  109. <Router
  110. history={memoryHistory}
  111. render={props => {
  112. return (
  113. <RouteContext.Provider value={props}>
  114. <RouterContext {...props} />
  115. </RouteContext.Provider>
  116. );
  117. }}
  118. >
  119. <Route path="/" component={TestPage} />
  120. </Router>
  121. );
  122. act(() =>
  123. setSorts([
  124. {
  125. kind: 'asc',
  126. field: 'foo',
  127. },
  128. ])
  129. );
  130. expect(sorts).toEqual([
  131. {
  132. kind: 'desc',
  133. field: 'id',
  134. },
  135. ]);
  136. });
  137. });