organization.spec.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {ProjectFixture} from 'sentry-fixture/project';
  3. import {TeamFixture} from 'sentry-fixture/team';
  4. import {fetchOrganizationDetails} from 'sentry/actionCreators/organization';
  5. import * as OrganizationsActionCreator from 'sentry/actionCreators/organizations';
  6. import OrganizationStore from 'sentry/stores/organizationStore';
  7. import PageFiltersStore from 'sentry/stores/pageFiltersStore';
  8. import ProjectsStore from 'sentry/stores/projectsStore';
  9. import TeamStore from 'sentry/stores/teamStore';
  10. describe('OrganizationActionCreator', function () {
  11. const org = OrganizationFixture();
  12. const teams = [TeamFixture()];
  13. const projects = [ProjectFixture()];
  14. const api = new MockApiClient();
  15. beforeEach(function () {
  16. MockApiClient.clearMockResponses();
  17. jest.spyOn(TeamStore, 'loadInitialData');
  18. jest.spyOn(TeamStore, 'reset');
  19. jest.spyOn(PageFiltersStore, 'onReset');
  20. jest.spyOn(ProjectsStore, 'loadInitialData');
  21. jest.spyOn(ProjectsStore, 'reset');
  22. jest.spyOn(OrganizationStore, 'reset');
  23. jest.spyOn(OrganizationStore, 'onUpdate');
  24. jest.spyOn(OrganizationStore, 'onFetchOrgError');
  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. expect(OrganizationStore.reset).toHaveBeenCalled();
  47. expect(PageFiltersStore.onReset).toHaveBeenCalled();
  48. expect(ProjectsStore.reset).toHaveBeenCalled();
  49. expect(TeamStore.reset).toHaveBeenCalled();
  50. expect(getOrgMock).toHaveBeenCalledWith(
  51. `/organizations/${org.slug}/`,
  52. expect.anything()
  53. );
  54. expect(getProjectsMock).toHaveBeenCalledWith(
  55. `/organizations/${org.slug}/projects/`,
  56. expect.anything()
  57. );
  58. expect(getTeamsMock).toHaveBeenCalledWith(
  59. `/organizations/${org.slug}/teams/`,
  60. expect.anything()
  61. );
  62. expect(OrganizationStore.onUpdate).toHaveBeenCalledWith(org, {replace: true});
  63. expect(OrganizationsActionCreator.setActiveOrganization).toHaveBeenCalled();
  64. expect(TeamStore.loadInitialData).toHaveBeenCalledWith(teams);
  65. expect(ProjectsStore.loadInitialData).toHaveBeenCalledWith(projects);
  66. expect(OrganizationStore.get().organization).toEqual(org);
  67. });
  68. it('silently fetches organization details', async function () {
  69. const getOrgMock = MockApiClient.addMockResponse({
  70. url: `/organizations/${org.slug}/`,
  71. body: org,
  72. });
  73. MockApiClient.addMockResponse({
  74. url: `/organizations/${org.slug}/projects/`,
  75. body: projects,
  76. });
  77. MockApiClient.addMockResponse({
  78. url: `/organizations/${org.slug}/teams/`,
  79. body: teams,
  80. });
  81. fetchOrganizationDetails(api, org.slug, true, true);
  82. await tick();
  83. expect(OrganizationStore.reset).not.toHaveBeenCalled();
  84. expect(PageFiltersStore.onReset).not.toHaveBeenCalled();
  85. expect(ProjectsStore.reset).not.toHaveBeenCalled();
  86. expect(TeamStore.reset).not.toHaveBeenCalled();
  87. expect(getOrgMock).toHaveBeenCalledWith(
  88. `/organizations/${org.slug}/`,
  89. expect.anything()
  90. );
  91. expect(OrganizationStore.onUpdate).toHaveBeenCalledWith(org, {replace: true});
  92. expect(OrganizationsActionCreator.setActiveOrganization).toHaveBeenCalled();
  93. expect(TeamStore.loadInitialData).toHaveBeenCalledWith(teams);
  94. expect(ProjectsStore.loadInitialData).toHaveBeenCalledWith(projects);
  95. });
  96. it('errors out correctly', async function () {
  97. const getOrgMock = MockApiClient.addMockResponse({
  98. url: `/organizations/${org.slug}/`,
  99. statusCode: 400,
  100. });
  101. MockApiClient.addMockResponse({
  102. url: `/organizations/${org.slug}/projects/`,
  103. body: projects,
  104. });
  105. MockApiClient.addMockResponse({
  106. url: `/organizations/${org.slug}/teams/`,
  107. body: teams,
  108. });
  109. fetchOrganizationDetails(api, org.slug, false);
  110. await tick();
  111. expect(OrganizationStore.reset).toHaveBeenCalled();
  112. expect(PageFiltersStore.onReset).toHaveBeenCalled();
  113. expect(ProjectsStore.reset).toHaveBeenCalled();
  114. expect(TeamStore.reset).toHaveBeenCalled();
  115. expect(getOrgMock).toHaveBeenCalledWith(
  116. `/organizations/${org.slug}/`,
  117. expect.anything()
  118. );
  119. expect(OrganizationStore.onFetchOrgError).toHaveBeenCalled();
  120. });
  121. });