ruleBuilder.spec.jsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {act} from 'sentry-test/reactTestingLibrary';
  3. import {findOption, openMenu, selectByValueAsync} from 'sentry-test/select-new';
  4. import MemberListStore from 'sentry/stores/memberListStore';
  5. import ProjectsStore from 'sentry/stores/projectsStore';
  6. import TeamStore from 'sentry/stores/teamStore';
  7. import RuleBuilder from 'sentry/views/settings/project/projectOwnership/ruleBuilder';
  8. describe('RuleBuilder', function () {
  9. const organization = TestStubs.Organization();
  10. let project;
  11. let handleAdd;
  12. const USER_1 = TestStubs.User({
  13. id: '1',
  14. name: 'Jane Bloggs',
  15. email: 'janebloggs@example.com',
  16. user: {
  17. id: '1',
  18. name: 'Jane Bloggs',
  19. email: 'janebloggs@example.com',
  20. },
  21. });
  22. const USER_2 = TestStubs.User({
  23. id: '2',
  24. name: 'John Smith',
  25. email: 'johnsmith@example.com',
  26. user: {
  27. id: '2',
  28. name: 'John Smith',
  29. email: 'johnsmith@example.com',
  30. },
  31. });
  32. const TEAM_1 = TestStubs.Team({
  33. id: '3',
  34. name: 'COOL TEAM',
  35. slug: 'cool-team',
  36. });
  37. // This team is in project
  38. const TEAM_2 = TestStubs.Team({
  39. id: '4',
  40. name: 'TEAM NOT IN PROJECT',
  41. slug: 'team-not-in-project',
  42. });
  43. beforeEach(function () {
  44. // User in project
  45. MemberListStore.loadInitialData([USER_1]);
  46. // All teams
  47. jest.spyOn(TeamStore, 'getAll').mockImplementation(() => [TEAM_1, TEAM_2]);
  48. handleAdd = jest.fn();
  49. project = TestStubs.Project({
  50. // Teams in project
  51. teams: [TEAM_1],
  52. });
  53. act(() => ProjectsStore.loadInitialData([project]));
  54. jest.spyOn(ProjectsStore, 'getBySlug').mockImplementation(() => project);
  55. MockApiClient.clearMockResponses();
  56. MockApiClient.addMockResponse({
  57. url: '/organizations/org-slug/members/',
  58. body: [USER_1, USER_2],
  59. });
  60. });
  61. afterEach(function () {});
  62. it('renders', async function () {
  63. const wrapper = mountWithTheme(
  64. <RuleBuilder project={project} organization={organization} onAddRule={handleAdd} />
  65. );
  66. await tick();
  67. wrapper.update();
  68. const add = wrapper.find('AddButton');
  69. add.simulate('click');
  70. expect(handleAdd).not.toHaveBeenCalled();
  71. const text = wrapper.find('BuilderInput');
  72. text.simulate('change', {target: {value: 'some/path/*'}});
  73. expect(wrapper.find('AddButton').prop('disabled')).toBe(true);
  74. add.simulate('click');
  75. expect(handleAdd).not.toHaveBeenCalled();
  76. // Select the first item in the list.
  77. await selectByValueAsync(wrapper, 'user:1', {name: 'owners', control: true});
  78. wrapper.update();
  79. expect(wrapper.find('AddButton').prop('disabled')).toBe(false);
  80. add.simulate('click');
  81. expect(handleAdd).toHaveBeenCalled();
  82. // This is because after selecting, react-select (async) reloads
  83. await tick();
  84. wrapper.update();
  85. expect(wrapper.find(RuleBuilder)).toSnapshot();
  86. });
  87. it('renders with suggestions', async function () {
  88. const wrapper = mountWithTheme(
  89. <RuleBuilder
  90. project={project}
  91. organization={organization}
  92. onAddRule={handleAdd}
  93. urls={['example.com/a', 'example.com/a/foo']}
  94. paths={['a/bar', 'a/foo']}
  95. />
  96. );
  97. // Open the menu so we can do some assertions.
  98. openMenu(wrapper, {name: 'owners', control: true});
  99. await tick();
  100. wrapper.update();
  101. // Should have all 4 users/teams listed
  102. expect(wrapper.find('IdBadge')).toHaveLength(4);
  103. // Should have 1 user not in project and 1 team not in project
  104. expect(wrapper.find('DisabledLabel IdBadge')).toHaveLength(2);
  105. // Team not in project should not be selectable
  106. expect(wrapper.find('DisabledLabel IdBadge').at(0).prop('team').id).toBe('4');
  107. // John Smith should not be selectable
  108. expect(wrapper.find('DisabledLabel IdBadge').at(1).prop('user').id).toBe('2');
  109. // Enter to select Jane Bloggs
  110. findOption(wrapper, {value: 'user:1'}, {name: 'owners', control: true})
  111. .at(0)
  112. .simulate('click');
  113. const ruleCandidate = wrapper.find('RuleCandidate').first();
  114. ruleCandidate.simulate('click');
  115. // This is because after selecting, react-select (async) reloads
  116. await tick();
  117. wrapper.update();
  118. expect(wrapper.find(RuleBuilder)).toSnapshot();
  119. wrapper.find('AddButton').simulate('click');
  120. expect(handleAdd).toHaveBeenCalled();
  121. });
  122. });