organization.spec.jsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. import {fetchOrganizationDetails} from 'sentry/actionCreators/organization';
  2. import * as OrganizationsActionCreator from 'sentry/actionCreators/organizations';
  3. import OrganizationActions from 'sentry/actions/organizationActions';
  4. import PageFiltersActions from 'sentry/actions/pageFiltersActions';
  5. import ProjectActions from 'sentry/actions/projectActions';
  6. import TeamActions from 'sentry/actions/teamActions';
  7. import OrganizationStore from 'sentry/stores/organizationStore';
  8. describe('OrganizationActionCreator', function () {
  9. const org = TestStubs.Organization();
  10. delete org.teams;
  11. delete org.projects;
  12. const teams = [TestStubs.Team()];
  13. const projects = [TestStubs.Project()];
  14. const api = new MockApiClient();
  15. beforeEach(function () {
  16. MockApiClient.clearMockResponses();
  17. jest.spyOn(TeamActions, 'loadTeams');
  18. jest.spyOn(TeamActions, 'reset');
  19. jest.spyOn(PageFiltersActions, 'reset');
  20. jest.spyOn(ProjectActions, 'loadProjects');
  21. jest.spyOn(ProjectActions, 'reset');
  22. jest.spyOn(OrganizationActions, 'reset');
  23. jest.spyOn(OrganizationActions, 'update');
  24. jest.spyOn(OrganizationActions, 'fetchOrgError');
  25. jest.spyOn(OrganizationsActionCreator, 'setActiveOrganization');
  26. });
  27. afterEach(function () {
  28. jest.restoreAllMocks();
  29. MockApiClient.clearMockResponses();
  30. });
  31. it('fetches organization details', async function () {
  32. const getOrgMock = MockApiClient.addMockResponse({
  33. url: `/organizations/${org.slug}/`,
  34. body: org,
  35. });
  36. const getProjectsMock = MockApiClient.addMockResponse({
  37. url: `/organizations/${org.slug}/projects/`,
  38. body: projects,
  39. });
  40. const getTeamsMock = MockApiClient.addMockResponse({
  41. url: `/organizations/${org.slug}/teams/`,
  42. body: teams,
  43. });
  44. fetchOrganizationDetails(api, org.slug, false);
  45. await tick();
  46. await tick();
  47. expect(OrganizationActions.reset).toHaveBeenCalled();
  48. expect(PageFiltersActions.reset).toHaveBeenCalled();
  49. expect(ProjectActions.reset).toHaveBeenCalled();
  50. expect(TeamActions.reset).toHaveBeenCalled();
  51. expect(getOrgMock).toHaveBeenCalledWith(
  52. `/organizations/${org.slug}/`,
  53. expect.anything()
  54. );
  55. expect(getProjectsMock).toHaveBeenCalledWith(
  56. `/organizations/${org.slug}/projects/`,
  57. expect.anything()
  58. );
  59. expect(getTeamsMock).toHaveBeenCalledWith(
  60. `/organizations/${org.slug}/teams/`,
  61. expect.anything()
  62. );
  63. expect(OrganizationActions.update).toHaveBeenCalledWith(org, {replace: true});
  64. expect(OrganizationsActionCreator.setActiveOrganization).toHaveBeenCalled();
  65. expect(TeamActions.loadTeams).toHaveBeenCalledWith(teams);
  66. expect(ProjectActions.loadProjects).toHaveBeenCalledWith(projects);
  67. expect(OrganizationStore.organization).toEqual(org);
  68. });
  69. it('silently fetches organization details', async function () {
  70. const getOrgMock = MockApiClient.addMockResponse({
  71. url: `/organizations/${org.slug}/`,
  72. body: org,
  73. });
  74. MockApiClient.addMockResponse({
  75. url: `/organizations/${org.slug}/projects/`,
  76. body: projects,
  77. });
  78. MockApiClient.addMockResponse({
  79. url: `/organizations/${org.slug}/teams/`,
  80. body: teams,
  81. });
  82. fetchOrganizationDetails(api, org.slug, true, true);
  83. await tick();
  84. expect(OrganizationActions.reset).not.toHaveBeenCalled();
  85. expect(PageFiltersActions.reset).not.toHaveBeenCalled();
  86. expect(ProjectActions.reset).not.toHaveBeenCalled();
  87. expect(TeamActions.reset).not.toHaveBeenCalled();
  88. expect(getOrgMock).toHaveBeenCalledWith(
  89. `/organizations/${org.slug}/`,
  90. expect.anything()
  91. );
  92. expect(OrganizationActions.update).toHaveBeenCalledWith(org, {replace: true});
  93. expect(OrganizationsActionCreator.setActiveOrganization).toHaveBeenCalled();
  94. expect(TeamActions.loadTeams).toHaveBeenCalledWith(teams);
  95. expect(ProjectActions.loadProjects).toHaveBeenCalledWith(projects);
  96. });
  97. it('errors out correctly', async function () {
  98. const getOrgMock = MockApiClient.addMockResponse({
  99. url: `/organizations/${org.slug}/`,
  100. statusCode: 400,
  101. });
  102. MockApiClient.addMockResponse({
  103. url: `/organizations/${org.slug}/projects/`,
  104. body: projects,
  105. });
  106. MockApiClient.addMockResponse({
  107. url: `/organizations/${org.slug}/teams/`,
  108. body: teams,
  109. });
  110. fetchOrganizationDetails(api, org.slug, false);
  111. await tick();
  112. expect(OrganizationActions.reset).toHaveBeenCalled();
  113. expect(PageFiltersActions.reset).toHaveBeenCalled();
  114. expect(ProjectActions.reset).toHaveBeenCalled();
  115. expect(TeamActions.reset).toHaveBeenCalled();
  116. expect(getOrgMock).toHaveBeenCalledWith(
  117. `/organizations/${org.slug}/`,
  118. expect.anything()
  119. );
  120. expect(OrganizationActions.fetchOrgError).toHaveBeenCalled();
  121. });
  122. });