organization.spec.tsx 4.7 KB

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