useTeams.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {reactHooks} from 'sentry-test/reactTestingLibrary';
  2. import OrganizationStore from 'sentry/stores/organizationStore';
  3. import TeamStore from 'sentry/stores/teamStore';
  4. import useTeams from 'sentry/utils/useTeams';
  5. describe('useTeams', function () {
  6. const org = TestStubs.Organization();
  7. const mockTeams = [TestStubs.Team()];
  8. beforeEach(function () {
  9. TeamStore.reset();
  10. });
  11. it('provides teams from the team store', function () {
  12. TeamStore.loadInitialData(mockTeams);
  13. const {result} = reactHooks.renderHook(() => useTeams());
  14. const {teams} = result.current;
  15. expect(teams).toEqual(mockTeams);
  16. });
  17. it('loads more teams when using onSearch', async function () {
  18. TeamStore.loadInitialData(mockTeams);
  19. OrganizationStore.onUpdate(org, {replace: true});
  20. const newTeam2 = TestStubs.Team({id: '2', slug: 'test-team2'});
  21. const newTeam3 = TestStubs.Team({id: '3', slug: 'test-team3'});
  22. const mockRequest = MockApiClient.addMockResponse({
  23. url: `/organizations/${org.slug}/teams/`,
  24. method: 'GET',
  25. body: [newTeam2, newTeam3],
  26. });
  27. const {result, waitFor} = reactHooks.renderHook(() => useTeams());
  28. const {onSearch} = result.current;
  29. // Works with append
  30. const onSearchPromise = reactHooks.act(() => onSearch('test'));
  31. expect(result.current.fetching).toBe(true);
  32. await onSearchPromise;
  33. expect(result.current.fetching).toBe(false);
  34. // Wait for state to be reflected from the store
  35. await waitFor(() => result.current.teams.length === 3);
  36. expect(mockRequest).toHaveBeenCalled();
  37. expect(result.current.teams).toEqual([...mockTeams, newTeam2, newTeam3]);
  38. // de-duplicates items in the query results
  39. mockRequest.mockClear();
  40. await reactHooks.act(() => onSearch('test'));
  41. // No new items have been added
  42. expect(mockRequest).toHaveBeenCalled();
  43. expect(result.current.teams).toEqual([...mockTeams, newTeam2, newTeam3]);
  44. });
  45. it('provides only the users teams', function () {
  46. const userTeams = [TestStubs.Team({id: '1', isMember: true})];
  47. const nonUserTeams = [TestStubs.Team({id: '2', isMember: false})];
  48. TeamStore.loadInitialData([...userTeams, ...nonUserTeams], false, null);
  49. const {result} = reactHooks.renderHook(props => useTeams(props), {
  50. initialProps: {provideUserTeams: true},
  51. });
  52. const {teams} = result.current;
  53. expect(teams.length).toBe(1);
  54. expect(teams).toEqual(expect.arrayContaining(userTeams));
  55. });
  56. it('provides only the specified slugs', async function () {
  57. TeamStore.loadInitialData(mockTeams);
  58. const teamFoo = TestStubs.Team({slug: 'foo'});
  59. const mockRequest = MockApiClient.addMockResponse({
  60. url: `/organizations/${org.slug}/teams/`,
  61. method: 'GET',
  62. body: [teamFoo],
  63. });
  64. const {result, waitFor} = reactHooks.renderHook(props => useTeams(props), {
  65. initialProps: {slugs: ['foo']},
  66. });
  67. expect(result.current.initiallyLoaded).toBe(false);
  68. expect(mockRequest).toHaveBeenCalled();
  69. await waitFor(() => expect(result.current.teams.length).toBe(1));
  70. const {teams} = result.current;
  71. expect(teams).toEqual(expect.arrayContaining([teamFoo]));
  72. });
  73. it('only loads slugs when needed', function () {
  74. TeamStore.loadInitialData(mockTeams);
  75. const {result} = reactHooks.renderHook(props => useTeams(props), {
  76. initialProps: {slugs: [mockTeams[0].slug]},
  77. });
  78. const {teams, initiallyLoaded} = result.current;
  79. expect(initiallyLoaded).toBe(true);
  80. expect(teams).toEqual(expect.arrayContaining(mockTeams));
  81. });
  82. it('can load teams by id', async function () {
  83. const requestedTeams = [TestStubs.Team({id: '2', slug: 'requested-team'})];
  84. const mockRequest = MockApiClient.addMockResponse({
  85. url: `/organizations/${org.slug}/teams/`,
  86. method: 'GET',
  87. body: requestedTeams,
  88. });
  89. TeamStore.loadInitialData(mockTeams);
  90. const {result, waitFor} = reactHooks.renderHook(props => useTeams(props), {
  91. initialProps: {ids: ['2']},
  92. });
  93. expect(result.current.initiallyLoaded).toBe(false);
  94. expect(mockRequest).toHaveBeenCalled();
  95. await waitFor(() => expect(result.current.teams.length).toBe(1));
  96. const {teams} = result.current;
  97. expect(teams).toEqual(expect.arrayContaining(requestedTeams));
  98. });
  99. it('only loads ids when needed', function () {
  100. TeamStore.loadInitialData(mockTeams);
  101. const {result} = reactHooks.renderHook(props => useTeams(props), {
  102. initialProps: {ids: [mockTeams[0].id]},
  103. });
  104. const {teams, initiallyLoaded} = result.current;
  105. expect(initiallyLoaded).toBe(true);
  106. expect(teams).toEqual(expect.arrayContaining(mockTeams));
  107. });
  108. it('correctly returns hasMore before and after store update', async function () {
  109. const {result, waitFor} = reactHooks.renderHook(() => useTeams());
  110. const {teams, hasMore} = result.current;
  111. expect(hasMore).toBe(null);
  112. expect(teams).toEqual(expect.arrayContaining([]));
  113. reactHooks.act(() => TeamStore.loadInitialData(mockTeams, false, null));
  114. await waitFor(() => expect(result.current.teams.length).toBe(1));
  115. expect(result.current.hasMore).toBe(false);
  116. });
  117. });