ownershipRulesTable.spec.tsx 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. import {CodeOwnerFixture} from 'sentry-fixture/codeOwner';
  2. import {UserFixture} from 'sentry-fixture/user';
  3. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  4. import ConfigStore from 'sentry/stores/configStore';
  5. import MemberListStore from 'sentry/stores/memberListStore';
  6. import type {Actor, ParsedOwnershipRule} from 'sentry/types';
  7. import {OwnershipRulesTable} from './ownershipRulesTable';
  8. describe('OwnershipRulesTable', () => {
  9. const user1 = UserFixture();
  10. const user2 = UserFixture({id: '2', name: 'Jane Doe'});
  11. beforeEach(() => {
  12. ConfigStore.init();
  13. ConfigStore.set('user', user1);
  14. MemberListStore.init();
  15. MemberListStore.loadInitialData([user1, user2]);
  16. });
  17. it('should render empty state', () => {
  18. render(<OwnershipRulesTable projectRules={[]} codeowners={[]} />);
  19. expect(screen.getByText('No ownership rules found')).toBeInTheDocument();
  20. });
  21. it('should render project owners members', () => {
  22. const rules: ParsedOwnershipRule[] = [
  23. {
  24. matcher: {pattern: 'pattern', type: 'path'},
  25. owners: [{type: 'user', id: user1.id, name: user1.name}],
  26. },
  27. ];
  28. render(<OwnershipRulesTable projectRules={rules} codeowners={[]} />);
  29. expect(screen.getByText('path')).toBeInTheDocument();
  30. expect(screen.getByText('pattern')).toBeInTheDocument();
  31. expect(screen.getByText(user1.name)).toBeInTheDocument();
  32. });
  33. it('should filter codeowners rules without actor names', () => {
  34. const rules: ParsedOwnershipRule[] = [
  35. {
  36. matcher: {pattern: 'pattern', type: 'path'},
  37. // Name = undefined only seems to happen when adding a new codeowners file
  38. owners: [{type: 'user', id: user1.id, name: undefined as any}],
  39. },
  40. {
  41. matcher: {pattern: 'my/path', type: 'path'},
  42. owners: [{type: 'user', id: user2.id, name: user2.name}],
  43. },
  44. ];
  45. render(
  46. <OwnershipRulesTable
  47. projectRules={[]}
  48. codeowners={[CodeOwnerFixture({schema: {rules, version: 1}})]}
  49. />
  50. );
  51. expect(screen.getByText('pattern')).toBeInTheDocument();
  52. expect(screen.getByText('my/path')).toBeInTheDocument();
  53. expect(screen.getByRole('button', {name: 'Everyone'})).toBeEnabled();
  54. });
  55. it('should render multiple project owners', () => {
  56. const rules: ParsedOwnershipRule[] = [
  57. {
  58. matcher: {pattern: 'pattern', type: 'path'},
  59. owners: [
  60. {type: 'user', id: user1.id, name: user1.name},
  61. {type: 'user', id: user2.id, name: user2.name},
  62. ],
  63. },
  64. ];
  65. render(<OwnershipRulesTable projectRules={rules} codeowners={[]} />);
  66. expect(screen.getByText('path')).toBeInTheDocument();
  67. expect(screen.getByText('pattern')).toBeInTheDocument();
  68. expect(screen.getByText(`${user1.name} and 1 other`)).toBeInTheDocument();
  69. expect(screen.queryByText(user2.name)).not.toBeInTheDocument();
  70. });
  71. it('should filter by rule type and pattern', async () => {
  72. const owners: Actor[] = [{type: 'user', id: user1.id, name: user1.name}];
  73. const rules: ParsedOwnershipRule[] = [
  74. {matcher: {pattern: 'filepath', type: 'path'}, owners},
  75. {matcher: {pattern: 'mytag', type: 'tag'}, owners},
  76. ];
  77. render(<OwnershipRulesTable projectRules={rules} codeowners={[]} />);
  78. const searchbar = screen.getByPlaceholderText('Search by type or rule');
  79. await userEvent.click(searchbar);
  80. await userEvent.paste('path');
  81. expect(screen.getByText('filepath')).toBeInTheDocument();
  82. expect(screen.queryByText('mytag')).not.toBeInTheDocument();
  83. // Change the filter to mytag
  84. await userEvent.clear(searchbar);
  85. await userEvent.paste('mytag');
  86. expect(screen.getByText('mytag')).toBeInTheDocument();
  87. expect(screen.queryByText('filepath')).not.toBeInTheDocument();
  88. });
  89. it('should filter by my teams by default', async () => {
  90. const rules: ParsedOwnershipRule[] = [
  91. {
  92. matcher: {pattern: 'filepath', type: 'path'},
  93. owners: [{type: 'user', id: user1.id, name: user1.name}],
  94. },
  95. {
  96. matcher: {pattern: 'mytag', type: 'tag'},
  97. owners: [{type: 'user', id: user2.id, name: user2.name}],
  98. },
  99. ];
  100. render(<OwnershipRulesTable projectRules={rules} codeowners={[]} />);
  101. expect(screen.getByText('filepath')).toBeInTheDocument();
  102. expect(screen.queryByText('mytag')).not.toBeInTheDocument();
  103. // Clear the filter
  104. await userEvent.click(screen.getByRole('button', {name: 'My Teams'}));
  105. await userEvent.click(screen.getByRole('button', {name: 'Clear'}));
  106. expect(screen.getByText('filepath')).toBeInTheDocument();
  107. expect(screen.queryByText('mytag')).toBeInTheDocument();
  108. });
  109. it('preserves selected teams when rules are updated', async () => {
  110. const rules: ParsedOwnershipRule[] = [
  111. {
  112. matcher: {pattern: 'filepath', type: 'path'},
  113. owners: [{type: 'user', id: user1.id, name: user1.name}],
  114. },
  115. {
  116. matcher: {pattern: 'anotherpath', type: 'path'},
  117. owners: [{type: 'user', id: user2.id, name: user2.name}],
  118. },
  119. ];
  120. const {rerender} = render(
  121. <OwnershipRulesTable projectRules={rules} codeowners={[]} />
  122. );
  123. // Clear the filter
  124. await userEvent.click(screen.getByRole('button', {name: 'My Teams'}));
  125. await userEvent.click(screen.getByRole('button', {name: 'Clear'}));
  126. expect(screen.getAllByText('path')).toHaveLength(2);
  127. const newRules: ParsedOwnershipRule[] = [
  128. ...rules,
  129. {
  130. matcher: {pattern: 'thirdpath', type: 'path'},
  131. owners: [{type: 'user', id: user2.id, name: user2.name}],
  132. },
  133. ];
  134. rerender(<OwnershipRulesTable projectRules={newRules} codeowners={[]} />);
  135. expect(screen.getAllByText('path')).toHaveLength(3);
  136. expect(screen.getByRole('button', {name: 'Everyone'})).toBeInTheDocument();
  137. });
  138. it('should paginate results', async () => {
  139. const owners: Actor[] = [{type: 'user', id: user1.id, name: user1.name}];
  140. const rules: ParsedOwnershipRule[] = Array(100)
  141. .fill(0)
  142. .map((_, i) => ({
  143. matcher: {pattern: `mytag${i}`, type: 'tag'},
  144. owners,
  145. }));
  146. render(<OwnershipRulesTable projectRules={rules} codeowners={[]} />);
  147. expect(screen.getByText('mytag1')).toBeInTheDocument();
  148. await userEvent.click(screen.getByRole('button', {name: 'Next page'}));
  149. expect(screen.getByText('mytag30')).toBeInTheDocument();
  150. expect(screen.queryByText('mytag1')).not.toBeInTheDocument();
  151. });
  152. });