organizations.spec.tsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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('has a fallback to regions', async function () {
  13. // @ts-ignore-next-line Need to simulate undefined
  14. // key that can happen during deploy
  15. ConfigStore.set('memberRegions', undefined);
  16. ConfigStore.set('regions', [
  17. {name: 'us', url: 'https://us.example.org'},
  18. {name: 'de', url: 'https://de.example.org'},
  19. ]);
  20. const usMock = MockApiClient.addMockResponse({
  21. url: '/organizations/',
  22. body: [usorg],
  23. });
  24. const deMock = MockApiClient.addMockResponse({
  25. url: '/organizations/',
  26. body: [deorg],
  27. match: [
  28. function (_url: string, options: Record<string, any>) {
  29. return options.host === 'https://de.example.org';
  30. },
  31. ],
  32. });
  33. const organizations = await fetchOrganizations(api);
  34. expect(organizations).toHaveLength(2);
  35. expect(organizations[0].slug).toEqual(usorg.slug);
  36. expect(usMock).toHaveBeenCalledTimes(1);
  37. expect(deMock).toHaveBeenCalledTimes(1);
  38. });
  39. it('fetches from multiple regions', 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: [deorg],
  56. match: [
  57. function (_url: string, options: Record<string, any>) {
  58. return options.host === 'https://de.example.org';
  59. },
  60. ],
  61. });
  62. const organizations = await fetchOrganizations(api);
  63. expect(organizations).toHaveLength(2);
  64. expect(usMock).toHaveBeenCalledTimes(1);
  65. expect(deMock).toHaveBeenCalledTimes(1);
  66. });
  67. it('ignores 401 errors from a region', async function () {
  68. ConfigStore.set('memberRegions', [
  69. {name: 'us', url: 'https://us.example.org'},
  70. {name: 'de', url: 'https://de.example.org'},
  71. ]);
  72. const usMock = MockApiClient.addMockResponse({
  73. url: '/organizations/',
  74. body: [usorg],
  75. match: [
  76. function (_url: string, options: Record<string, any>) {
  77. return options.host === 'https://us.example.org';
  78. },
  79. ],
  80. });
  81. const deMock = MockApiClient.addMockResponse({
  82. url: '/organizations/',
  83. body: {detail: 'Authentication credentials required'},
  84. status: 401,
  85. match: [
  86. function (_url: string, options: Record<string, any>) {
  87. return options.host === 'https://de.example.org';
  88. },
  89. ],
  90. });
  91. const organizations = await fetchOrganizations(api);
  92. expect(organizations).toHaveLength(1);
  93. expect(organizations[0].slug).toEqual(usorg.slug);
  94. expect(usMock).toHaveBeenCalledTimes(1);
  95. expect(deMock).toHaveBeenCalledTimes(1);
  96. expect(window.location.reload).not.toHaveBeenCalled();
  97. expect(browserHistory.replace).not.toHaveBeenCalled();
  98. });
  99. });