addCodeOwnerModal.spec.jsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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(project, repo, integration, {
  21. stackRoot: 'stack/root',
  22. sourceRoot: 'source/root',
  23. });
  24. beforeEach(function () {
  25. Client.clearMockResponses();
  26. Client.addMockResponse({
  27. url: `/organizations/${org.slug}/code-mappings/`,
  28. method: 'GET',
  29. query: {project: '2'},
  30. body: [codeMapping],
  31. });
  32. Client.addMockResponse({
  33. url: `/organizations/${org.slug}/integrations/`,
  34. method: 'GET',
  35. query: {features: ['codeowners']},
  36. body: [integration],
  37. });
  38. });
  39. it('renders', function () {
  40. const wrapper = mountWithTheme(
  41. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  42. );
  43. expect(wrapper.find('Button').prop('disabled')).toBe(true);
  44. });
  45. it('renders codeowner file', async function () {
  46. Client.addMockResponse({
  47. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  48. method: 'GET',
  49. body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
  50. });
  51. const wrapper = mountWithTheme(
  52. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  53. );
  54. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  55. await tick();
  56. wrapper.update();
  57. expect(wrapper.find('IconCheckmark').exists()).toBe(true);
  58. expect(wrapper.find('SourceFileBody').find('Button').prop('href')).toEqual('blah');
  59. expect(wrapper.find('SourceFileBody').text()).toContain('CODEOWNERS');
  60. expect(wrapper.state('codeownersFile').raw).toEqual('* @MeredithAnya\n');
  61. });
  62. it('renders no codeowner file found', async function () {
  63. Client.addMockResponse({
  64. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  65. method: 'GET',
  66. statusCode: 404,
  67. });
  68. const wrapper = mountWithTheme(
  69. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  70. );
  71. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  72. await tick();
  73. wrapper.update();
  74. expect(wrapper.find('IconNot').exists()).toBe(true);
  75. expect(wrapper.find('NoSourceFileBody').text()).toEqual('No codeowner file found.');
  76. expect(wrapper.state('codeownersFile')).toBe(null);
  77. });
  78. it('adds codeowner file', async function () {
  79. Client.addMockResponse({
  80. url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
  81. method: 'GET',
  82. body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
  83. });
  84. const addFileRequest = Client.addMockResponse({
  85. url: `/projects/${org.slug}/${project.slug}/codeowners/`,
  86. method: 'POST',
  87. body: {},
  88. });
  89. const wrapper = mountWithTheme(
  90. <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
  91. );
  92. selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
  93. await tick();
  94. wrapper.update();
  95. wrapper.find('Footer').find('Button').simulate('click');
  96. await tick();
  97. wrapper.update();
  98. expect(addFileRequest).toHaveBeenCalledWith(
  99. `/projects/${org.slug}/${project.slug}/codeowners/`,
  100. expect.objectContaining({
  101. data: {raw: '* @MeredithAnya\n', codeMappingId: codeMapping.id},
  102. })
  103. );
  104. const closeModal = wrapper.prop('closeModal');
  105. expect(closeModal).toHaveBeenCalled();
  106. });
  107. });