actions.test.ts 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import { initialState } from '../../dashboard/state/reducers';
  2. import { variableAdapters } from '../adapters';
  3. import { createConstantVariableAdapter } from '../constant/adapter';
  4. import { initialConstantVariableModelState } from '../constant/reducer';
  5. import * as inspectUtils from '../inspect/utils';
  6. import { constantBuilder, customBuilder } from '../shared/testing/builders';
  7. import { initialKeyedVariablesState, toKeyedAction } from '../state/keyedVariablesReducer';
  8. import * as selectors from '../state/selectors';
  9. import { addVariable } from '../state/sharedReducer';
  10. import { getNextAvailableId, switchToListMode, switchToNewMode } from './actions';
  11. import { setIdInEditor } from './reducer';
  12. describe('getNextAvailableId', () => {
  13. describe('when called with a custom type and there is already 2 variables', () => {
  14. it('then the correct id should be created', () => {
  15. const custom1 = customBuilder().withId('custom0').withName('custom0').build();
  16. const constant1 = constantBuilder().withId('custom1').withName('custom1').build();
  17. const variables = [custom1, constant1];
  18. const type = 'custom';
  19. const result = getNextAvailableId(type, variables);
  20. expect(result).toEqual('custom2');
  21. });
  22. });
  23. });
  24. describe('switchToNewMode', () => {
  25. variableAdapters.setInit(() => [createConstantVariableAdapter()]);
  26. it('should dispatch with the correct rootStateKey', () => {
  27. jest.spyOn(selectors, 'getVariablesByKey').mockReturnValue([]);
  28. jest.spyOn(selectors, 'getNewVariableIndex').mockReturnValue(0);
  29. const mockId = 'constant0';
  30. const mockGetState = jest.fn().mockReturnValue({ templating: initialKeyedVariablesState });
  31. const mockDispatch = jest.fn();
  32. const model = { ...initialConstantVariableModelState, name: mockId, id: mockId, rootStateKey: 'null' };
  33. switchToNewMode(null, 'constant')(mockDispatch, mockGetState, undefined);
  34. expect(mockDispatch).toHaveBeenCalledTimes(2);
  35. expect(mockDispatch.mock.calls[0][0]).toEqual(
  36. toKeyedAction('null', addVariable({ data: { global: false, index: 0, model }, type: 'constant', id: mockId }))
  37. );
  38. expect(mockDispatch.mock.calls[1][0]).toEqual(toKeyedAction('null', setIdInEditor({ id: mockId })));
  39. });
  40. });
  41. describe('switchToListMode', () => {
  42. variableAdapters.setInit(() => [createConstantVariableAdapter()]);
  43. it('should dispatch with the correct rootStateKey', () => {
  44. jest.spyOn(selectors, 'getEditorVariables').mockReturnValue([]);
  45. jest.spyOn(inspectUtils, 'createUsagesNetwork').mockReturnValue({ usages: [], unUsed: [] });
  46. jest.spyOn(inspectUtils, 'transformUsagesToNetwork').mockReturnValue([]);
  47. const mockGetState = jest.fn().mockReturnValue({ templating: initialKeyedVariablesState, dashboard: initialState });
  48. const mockDispatch = jest.fn();
  49. switchToListMode(null)(mockDispatch, mockGetState, undefined);
  50. const keyedAction = {
  51. type: expect.any(String),
  52. payload: {
  53. key: 'null',
  54. action: expect.any(Object),
  55. },
  56. };
  57. expect(mockDispatch).toHaveBeenCalledTimes(2);
  58. expect(mockDispatch.mock.calls[0][0]).toMatchObject(keyedAction);
  59. expect(mockDispatch.mock.calls[1][0]).toMatchObject(keyedAction);
  60. });
  61. });