actions.test.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { thunkTester } from 'test/core/thunk/thunkTester';
  2. import { updateConfigurationSubtitle } from 'app/core/actions';
  3. import { OrgRole } from 'app/types';
  4. import { updateOrganization, setUserOrganization, getUserOrganizations } from './actions';
  5. const setup = () => {
  6. const initialState = {
  7. organization: {
  8. organization: {
  9. id: 1,
  10. name: 'New Org Name',
  11. },
  12. userOrg: [{ orgId: 1, name: 'New Org Name', role: OrgRole.Editor }],
  13. },
  14. };
  15. return {
  16. initialState,
  17. };
  18. };
  19. describe('updateOrganization', () => {
  20. describe('when updateOrganization thunk is dispatched', () => {
  21. const getMock = jest.fn().mockResolvedValue({ id: 1, name: 'New Org Name' });
  22. const putMock = jest.fn().mockResolvedValue({ id: 1, name: 'New Org Name' });
  23. const backendSrvMock: any = {
  24. get: getMock,
  25. put: putMock,
  26. };
  27. it('then it should dispatch updateConfigurationSubtitle', async () => {
  28. const { initialState } = setup();
  29. const dispatchedActions = await thunkTester(initialState)
  30. .givenThunk(updateOrganization)
  31. .whenThunkIsDispatched({ getBackendSrv: () => backendSrvMock });
  32. expect(dispatchedActions[0].type).toEqual(updateConfigurationSubtitle.type);
  33. expect(dispatchedActions[0].payload).toEqual(initialState.organization.organization.name);
  34. });
  35. });
  36. });
  37. describe('setUserOrganization', () => {
  38. describe('when setUserOrganization thunk is dispatched', () => {
  39. const postMock = jest.fn().mockResolvedValue({ id: 1, name: 'New Org Name' });
  40. const backendSrvMock: any = {
  41. post: postMock,
  42. };
  43. const orgId = 1;
  44. it('then it should dispatch updateConfigurationSubtitle', async () => {
  45. const { initialState } = setup();
  46. const dispatchedActions = await thunkTester(initialState)
  47. .givenThunk(setUserOrganization)
  48. .whenThunkIsDispatched(orgId, { getBackendSrv: () => backendSrvMock });
  49. expect(dispatchedActions[0].type).toEqual(updateConfigurationSubtitle.type);
  50. expect(dispatchedActions[0].payload).toEqual(initialState.organization.organization.name);
  51. });
  52. });
  53. });
  54. describe('getUserOrganizations', () => {
  55. describe('when getUserOrganizations thunk is dispatched', () => {
  56. const getMock = jest.fn().mockResolvedValue({ orgId: 1, name: 'New Org Name', role: OrgRole.Editor });
  57. const backendSrvMock: any = {
  58. get: getMock,
  59. };
  60. it('then it should dispatch updateConfigurationSubtitle', async () => {
  61. const { initialState } = setup();
  62. const dispatchedActions = await thunkTester(initialState)
  63. .givenThunk(getUserOrganizations)
  64. .whenThunkIsDispatched({ getBackendSrv: () => backendSrvMock });
  65. expect(dispatchedActions[0].payload).toEqual(initialState.organization.userOrg[0]);
  66. });
  67. });
  68. });