utils.spec.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import { Option } from 'react-multi-select-component/dist/lib/interfaces';
  2. import {
  3. filterOptions,
  4. getOffsetFromSeekToParam,
  5. getTimestampFromSeekToParam,
  6. getSelectedPartitionsFromSeekToParam,
  7. } from 'components/Topics/Topic/Messages/Filters/utils';
  8. import { SeekType, Partition } from 'generated-sources';
  9. const options: Option[] = [
  10. {
  11. value: 0,
  12. label: 'Partition #0',
  13. },
  14. {
  15. value: 1,
  16. label: 'Partition #1',
  17. },
  18. {
  19. value: 11,
  20. label: 'Partition #11',
  21. },
  22. {
  23. value: 21,
  24. label: 'Partition #21',
  25. },
  26. ];
  27. let paramsString;
  28. let searchParams = new URLSearchParams(paramsString);
  29. describe('utils', () => {
  30. describe('filterOptions', () => {
  31. it('returns options if no filter is defined', () => {
  32. expect(filterOptions(options, '')).toEqual(options);
  33. });
  34. it('returns filtered options', () => {
  35. expect(filterOptions(options, '11')).toEqual([options[2]]);
  36. });
  37. });
  38. describe('getOffsetFromSeekToParam', () => {
  39. beforeEach(() => {
  40. paramsString = 'seekTo=0::123,1::123,2::0';
  41. searchParams = new URLSearchParams(paramsString);
  42. });
  43. it('returns nothing when param "seekType" is equal BEGGINING', () => {
  44. searchParams.set('seekType', SeekType.BEGINNING);
  45. expect(getOffsetFromSeekToParam(searchParams)).toEqual('');
  46. });
  47. it('returns nothing when param "seekType" is equal TIMESTAMP', () => {
  48. searchParams.set('seekType', SeekType.TIMESTAMP);
  49. expect(getOffsetFromSeekToParam(searchParams)).toEqual('');
  50. });
  51. it('returns correct messages list when param "seekType" is equal OFFSET', () => {
  52. searchParams.set('seekType', SeekType.OFFSET);
  53. expect(getOffsetFromSeekToParam(searchParams)).toEqual('123');
  54. });
  55. it('returns 0 when param "seekTo" is not defined and param "seekType" is equal OFFSET', () => {
  56. searchParams.set('seekType', SeekType.OFFSET);
  57. searchParams.delete('seekTo');
  58. expect(getOffsetFromSeekToParam(searchParams)).toEqual('0');
  59. });
  60. });
  61. describe('getTimestampFromSeekToParam', () => {
  62. beforeEach(() => {
  63. paramsString = `seekTo=0::1627333200000,1::1627333200000`;
  64. searchParams = new URLSearchParams(paramsString);
  65. });
  66. it('returns null when param "seekType" is equal BEGGINING', () => {
  67. searchParams.set('seekType', SeekType.BEGINNING);
  68. expect(getTimestampFromSeekToParam(searchParams)).toEqual(null);
  69. });
  70. it('returns null when param "seekType" is equal OFFSET', () => {
  71. searchParams.set('seekType', SeekType.OFFSET);
  72. expect(getTimestampFromSeekToParam(searchParams)).toEqual(null);
  73. });
  74. it('returns correct messages list when param "seekType" is equal TIMESTAMP', () => {
  75. searchParams.set('seekType', SeekType.TIMESTAMP);
  76. expect(getTimestampFromSeekToParam(searchParams)).toEqual(
  77. new Date(1627333200000)
  78. );
  79. });
  80. it('returns default timestamp when param "seekTo" is empty and param "seekType" is equal TIMESTAMP', () => {
  81. searchParams.set('seekType', SeekType.TIMESTAMP);
  82. searchParams.delete('seekTo');
  83. expect(getTimestampFromSeekToParam(searchParams)).toEqual(new Date(0));
  84. });
  85. });
  86. describe('getSelectedPartitionsFromSeekToParam', () => {
  87. const part: Partition[] = [{ partition: 42, offsetMin: 0, offsetMax: 100 }];
  88. it('returns parsed partition from params when partition list includes selected partition', () => {
  89. searchParams.set('seekTo', '42::0');
  90. expect(getSelectedPartitionsFromSeekToParam(searchParams, part)).toEqual([
  91. { label: 'Partition #42', value: 42 },
  92. ]);
  93. });
  94. it('returns parsed partition from params when partition list NOT includes selected partition', () => {
  95. searchParams.set('seekTo', '24::0');
  96. expect(getSelectedPartitionsFromSeekToParam(searchParams, part)).toEqual(
  97. []
  98. );
  99. });
  100. it('returns partitions when param "seekTo" is not defined', () => {
  101. searchParams.delete('seekTo');
  102. expect(getSelectedPartitionsFromSeekToParam(searchParams, part)).toEqual([
  103. { label: 'Partition #42', value: 42 },
  104. ]);
  105. });
  106. });
  107. });