organizations.spec.tsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {OrganizationFixture} from 'sentry-fixture/organization';
  2. import {fetchOrganizations} from 'sentry/actionCreators/organizations';
  3. import ConfigStore from 'sentry/stores/configStore';
  4. describe('fetchOrganizations', function () {
  5. const api = new MockApiClient();
  6. const usorg = OrganizationFixture({slug: 'us-org'});
  7. const deorg = OrganizationFixture({slug: 'de-org'});
  8. beforeEach(function () {
  9. MockApiClient.clearMockResponses();
  10. });
  11. it('fetches from multiple regions', async function () {
  12. ConfigStore.set('memberRegions', [
  13. {name: 'us', url: 'https://us.example.org'},
  14. {name: 'de', url: 'https://de.example.org'},
  15. ]);
  16. const usMock = MockApiClient.addMockResponse({
  17. url: '/organizations/',
  18. body: [usorg],
  19. match: [
  20. function (_url: string, options: Record<string, any>) {
  21. return options.host === 'https://us.example.org';
  22. },
  23. ],
  24. });
  25. const deMock = MockApiClient.addMockResponse({
  26. url: '/organizations/',
  27. body: [deorg],
  28. match: [
  29. function (_url: string, options: Record<string, any>) {
  30. return options.host === 'https://de.example.org';
  31. },
  32. ],
  33. });
  34. const organizations = await fetchOrganizations(api);
  35. expect(organizations).toHaveLength(2);
  36. expect(usMock).toHaveBeenCalledTimes(1);
  37. expect(deMock).toHaveBeenCalledTimes(1);
  38. });
  39. it('ignores 401 errors from a region', async function () {
  40. ConfigStore.set('memberRegions', [
  41. {name: 'us', url: 'https://us.example.org'},
  42. {name: 'de', url: 'https://de.example.org'},
  43. ]);
  44. const usMock = MockApiClient.addMockResponse({
  45. url: '/organizations/',
  46. body: [usorg],
  47. match: [
  48. function (_url: string, options: Record<string, any>) {
  49. return options.host === 'https://us.example.org';
  50. },
  51. ],
  52. });
  53. const deMock = MockApiClient.addMockResponse({
  54. url: '/organizations/',
  55. body: {detail: 'Authentication credentials required'},
  56. status: 401,
  57. match: [
  58. function (_url: string, options: Record<string, any>) {
  59. return options.host === 'https://de.example.org';
  60. },
  61. ],
  62. });
  63. const organizations = await fetchOrganizations(api);
  64. expect(organizations).toHaveLength(1);
  65. expect(organizations[0].slug).toEqual(usorg.slug);
  66. expect(usMock).toHaveBeenCalledTimes(1);
  67. expect(deMock).toHaveBeenCalledTimes(1);
  68. expect(window.location.reload).not.toHaveBeenCalled();
  69. });
  70. });