organizations.spec.tsx 2.6 KB

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