dashboardSearch.test.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. import { searchResults, sections } from '../testData';
  2. import { FETCH_ITEMS, FETCH_RESULTS, TOGGLE_SECTION, MOVE_SELECTION_DOWN, MOVE_SELECTION_UP } from './actionTypes';
  3. import { searchReducer as reducer, dashboardsSearchState } from './dashboardSearch';
  4. const defaultState = { selectedIndex: 0, loading: false, results: sections as any[], initialLoading: false };
  5. describe('Dashboard Search reducer', () => {
  6. it('should return the initial state', () => {
  7. expect(reducer(dashboardsSearchState, {} as any)).toEqual(dashboardsSearchState);
  8. });
  9. it('should set the results and mark first item as selected', () => {
  10. const newState = reducer(dashboardsSearchState, { type: FETCH_RESULTS, payload: searchResults });
  11. expect(newState).toEqual({ loading: false, selectedIndex: 0, results: searchResults, initialLoading: false });
  12. expect(newState.results[0].selected).toBeTruthy();
  13. });
  14. it('should toggle selected section', () => {
  15. const newState = reducer(defaultState, { type: TOGGLE_SECTION, payload: sections[5] });
  16. expect(newState.results[5].expanded).toBeFalsy();
  17. const newState2 = reducer(defaultState, { type: TOGGLE_SECTION, payload: sections[1] });
  18. expect(newState2.results[1].expanded).toBeTruthy();
  19. });
  20. it('should handle FETCH_ITEMS', () => {
  21. const items = [
  22. {
  23. id: 4072,
  24. uid: 'OzAIf_rWz',
  25. title: 'New dashboard Copy 3',
  26. type: 'dash-db',
  27. isStarred: false,
  28. },
  29. {
  30. id: 46,
  31. uid: '8DY63kQZk',
  32. title: 'Stocks',
  33. type: 'dash-db',
  34. isStarred: false,
  35. },
  36. ];
  37. const newState = reducer(defaultState, {
  38. type: FETCH_ITEMS,
  39. payload: {
  40. section: sections[2],
  41. items,
  42. },
  43. });
  44. expect(newState.results[2].items).toEqual(items);
  45. });
  46. it('should handle MOVE_SELECTION_DOWN', () => {
  47. const newState = reducer(defaultState, {
  48. type: MOVE_SELECTION_DOWN,
  49. });
  50. expect(newState.selectedIndex).toEqual(1);
  51. expect(newState.results[0].items[0].selected).toBeTruthy();
  52. const newState2 = reducer(newState, {
  53. type: MOVE_SELECTION_DOWN,
  54. });
  55. expect(newState2.selectedIndex).toEqual(2);
  56. expect(newState2.results[1].selected).toBeTruthy();
  57. // Shouldn't go over the visible results length - 1 (9)
  58. const newState3 = reducer(
  59. { ...defaultState, selectedIndex: 9 },
  60. {
  61. type: MOVE_SELECTION_DOWN,
  62. }
  63. );
  64. expect(newState3.selectedIndex).toEqual(9);
  65. });
  66. it('should handle MOVE_SELECTION_UP', () => {
  67. // shouldn't move beyond 0
  68. const newState = reducer(defaultState, {
  69. type: MOVE_SELECTION_UP,
  70. });
  71. expect(newState.selectedIndex).toEqual(0);
  72. const newState2 = reducer(
  73. { ...defaultState, selectedIndex: 3 },
  74. {
  75. type: MOVE_SELECTION_UP,
  76. }
  77. );
  78. expect(newState2.selectedIndex).toEqual(2);
  79. expect(newState2.results[1].selected).toBeTruthy();
  80. });
  81. });