organization.spec.jsx 4.6 KB

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