organization.spec.jsx 4.1 KB

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