useCompactSelectOptionsCache.spec.tsx 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import {renderHook} from 'sentry-test/reactTestingLibrary';
  2. import {useCompactSelectOptionsCache} from 'sentry/views/insights/common/utils/useCompactSelectOptionsCache';
  3. describe('useCompactSelectOptionsCache', function () {
  4. it('keeps a cache', function () {
  5. const {result, rerender} = renderHook(
  6. (args: Parameters<typeof useCompactSelectOptionsCache>) => {
  7. return useCompactSelectOptionsCache(...args);
  8. },
  9. {
  10. initialProps: [
  11. [
  12. {value: '', label: 'All'},
  13. {value: '2', label: '2XX'},
  14. {value: '3', label: '3XX'},
  15. ],
  16. ],
  17. }
  18. );
  19. expect(result.current.options).toEqual([
  20. {value: '', label: 'All'},
  21. {value: '2', label: '2XX'},
  22. {value: '3', label: '3XX'},
  23. ]);
  24. rerender([[{value: '4', label: '4XX'}]]);
  25. rerender([[{value: '5', label: '5XX'}]]);
  26. expect(result.current.options).toEqual([
  27. {value: '', label: 'All'},
  28. {value: '2', label: '2XX'},
  29. {value: '3', label: '3XX'},
  30. {value: '4', label: '4XX'},
  31. {value: '5', label: '5XX'},
  32. ]);
  33. });
  34. it('sorts the output', function () {
  35. const {result} = renderHook(
  36. (args: Parameters<typeof useCompactSelectOptionsCache>) => {
  37. return useCompactSelectOptionsCache(...args);
  38. },
  39. {
  40. initialProps: [
  41. [
  42. {value: '3', label: '3XX'},
  43. {value: '5', label: '5XX'},
  44. {value: '', label: 'All'},
  45. {value: '2', label: '2XX'},
  46. {value: '4', label: '4XX'},
  47. ],
  48. ],
  49. }
  50. );
  51. expect(result.current.options).toEqual([
  52. {value: '', label: 'All'},
  53. {value: '2', label: '2XX'},
  54. {value: '3', label: '3XX'},
  55. {value: '4', label: '4XX'},
  56. {value: '5', label: '5XX'},
  57. ]);
  58. });
  59. it('clears the cache', function () {
  60. const {result, rerender} = renderHook(
  61. (args: Parameters<typeof useCompactSelectOptionsCache>) => {
  62. return useCompactSelectOptionsCache(...args);
  63. },
  64. {
  65. initialProps: [
  66. [
  67. {value: '3', label: '3XX'},
  68. {value: '5', label: '5XX'},
  69. {value: '', label: 'All'},
  70. {value: '2', label: '2XX'},
  71. {value: '4', label: '4XX'},
  72. ],
  73. ],
  74. }
  75. );
  76. expect(result.current.options).toEqual([
  77. {value: '', label: 'All'},
  78. {value: '2', label: '2XX'},
  79. {value: '3', label: '3XX'},
  80. {value: '4', label: '4XX'},
  81. {value: '5', label: '5XX'},
  82. ]);
  83. result.current.clear();
  84. rerender([[]]);
  85. expect(result.current.options).toEqual([]);
  86. });
  87. });