addCodeOwnerModal.spec.jsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import {selectByValue} from 'sentry-test/select-new';
  3. import {Client} from 'sentry/api';
  4. import {AddCodeOwnerModal} from 'sentry/views/settings/project/projectOwnership/addCodeOwnerModal';
  5. describe('AddCodeOwnerModal', function () {
  6. const modalProps = {
  7. Body: p => p.children,
  8. Header: p => p.children,
  9. Footer: p => p.children,
  10. closeModal: jest.fn(() => null),
  11. };
  12. const org = TestStubs.Organization({features: ['integrations-codeowners']});
  13. const project = TestStubs.ProjectDetails();
  14. const integration = TestStubs.GitHubIntegration();
  15. const repo = TestStubs.Repository({
  16. integrationId: integration.id,
  17. id: '5',
  18. name: 'example/hello-there',
  19. });
  20. const codeMapping = TestStubs.RepositoryProjectPathConfig({
  21. project,
  22. repo,
  23. integration,
  24. stackRoot: 'stack/root',
  25. sourceRoot: 'source/root',
  26. });
  27. beforeEach(function () {
  28. Client.clearMockResponses();
  29. Client.addMockResponse({
  30. url: `/organizations/${org.slug}/code-mappings/`,
  31. method: 'GET',
  32. query: {project: '2'},
  33. body: [codeMapping],
  34. });
  35. Client.addMockResponse({
  36. url: `/organizations/${org.slug}/integrations/`,
  37. method: 'GET',
  38. query: {features: ['codeowners']},
  39. body: [integration],
  40. });
  41. });
  42. it('renders', function () {
  43. const wrapper = mountWithTheme(
  44. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  45. );
  46. expect(wrapper.find('Button').prop('disabled')).toBe(true);
  47. });
  48. it('renders codeowner file', async function () {
  49. Client.addMockResponse({
  50. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  51. method: 'GET',
  52. body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
  53. });
  54. const wrapper = mountWithTheme(
  55. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  56. );
  57. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  58. await tick();
  59. wrapper.update();
  60. expect(wrapper.find('IconCheckmark').exists()).toBe(true);
  61. expect(wrapper.find('SourceFileBody').find('Button').prop('href')).toEqual('blah');
  62. expect(wrapper.find('SourceFileBody').text()).toContain('CODEOWNERS');
  63. expect(wrapper.state('codeownersFile').raw).toEqual('* @MeredithAnya\n');
  64. });
  65. it('renders no codeowner file found', async function () {
  66. Client.addMockResponse({
  67. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  68. method: 'GET',
  69. statusCode: 404,
  70. });
  71. const wrapper = mountWithTheme(
  72. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  73. );
  74. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  75. await tick();
  76. wrapper.update();
  77. expect(wrapper.find('IconNot').exists()).toBe(true);
  78. expect(wrapper.find('NoSourceFileBody').text()).toEqual('No codeowner file found.');
  79. expect(wrapper.state('codeownersFile')).toBe(null);
  80. });
  81. it('adds codeowner file', async function () {
  82. Client.addMockResponse({
  83. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  84. method: 'GET',
  85. body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
  86. });
  87. const addFileRequest = Client.addMockResponse({
  88. url: `/projects/${org.slug}/${project.slug}/codeowners/`,
  89. method: 'POST',
  90. body: {},
  91. });
  92. const wrapper = mountWithTheme(
  93. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  94. );
  95. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  96. await tick();
  97. wrapper.update();
  98. wrapper.find('Footer').find('Button').simulate('click');
  99. await tick();
  100. wrapper.update();
  101. expect(addFileRequest).toHaveBeenCalledWith(
  102. `/projects/${org.slug}/${project.slug}/codeowners/`,
  103. expect.objectContaining({
  104. data: {raw: '* @MeredithAnya\n', codeMappingId: codeMapping.id},
  105. })
  106. );
  107. const closeModal = wrapper.prop('closeModal');
  108. expect(closeModal).toHaveBeenCalled();
  109. });
  110. });