123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {selectByValue} from 'sentry-test/select-new';
- import {Client} from 'sentry/api';
- import {AddCodeOwnerModal} from 'sentry/views/settings/project/projectOwnership/addCodeOwnerModal';
- describe('AddCodeOwnerModal', function () {
- const modalProps = {
- Body: p => p.children,
- Header: p => p.children,
- Footer: p => p.children,
- closeModal: jest.fn(() => null),
- };
- const org = TestStubs.Organization({features: ['integrations-codeowners']});
- const project = TestStubs.ProjectDetails();
- const integration = TestStubs.GitHubIntegration();
- const repo = TestStubs.Repository({
- integrationId: integration.id,
- id: '5',
- name: 'example/hello-there',
- });
- const codeMapping = TestStubs.RepositoryProjectPathConfig(project, repo, integration, {
- stackRoot: 'stack/root',
- sourceRoot: 'source/root',
- });
- beforeEach(function () {
- Client.clearMockResponses();
- Client.addMockResponse({
- url: `/organizations/${org.slug}/code-mappings/`,
- method: 'GET',
- query: {project: '2'},
- body: [codeMapping],
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/`,
- method: 'GET',
- query: {features: ['codeowners']},
- body: [integration],
- });
- });
- it('renders', function () {
- const wrapper = mountWithTheme(
- <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
- );
- expect(wrapper.find('Button').prop('disabled')).toBe(true);
- });
- it('renders codeowner file', async function () {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
- method: 'GET',
- body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
- });
- const wrapper = mountWithTheme(
- <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
- );
- selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
- await tick();
- wrapper.update();
- expect(wrapper.find('IconCheckmark').exists()).toBe(true);
- expect(wrapper.find('SourceFileBody').find('Button').prop('href')).toEqual('blah');
- expect(wrapper.find('SourceFileBody').text()).toContain('CODEOWNERS');
- expect(wrapper.state('codeownersFile').raw).toEqual('* @MeredithAnya\n');
- });
- it('renders no codeowner file found', async function () {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
- method: 'GET',
- statusCode: 404,
- });
- const wrapper = mountWithTheme(
- <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
- );
- selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
- await tick();
- wrapper.update();
- expect(wrapper.find('IconNot').exists()).toBe(true);
- expect(wrapper.find('NoSourceFileBody').text()).toEqual('No codeowner file found.');
- expect(wrapper.state('codeownersFile')).toBe(null);
- });
- it('adds codeowner file', async function () {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/code-mappings/${codeMapping.id}/codeowners/`,
- method: 'GET',
- body: {html_url: 'blah', filepath: 'CODEOWNERS', raw: '* @MeredithAnya\n'},
- });
- const addFileRequest = Client.addMockResponse({
- url: `/projects/${org.slug}/${project.slug}/codeowners/`,
- method: 'POST',
- body: {},
- });
- const wrapper = mountWithTheme(
- <AddCodeOwnerModal {...modalProps} organization={org} project={project} />
- );
- selectByValue(wrapper, codeMapping.id, {name: 'codeMappingId'});
- await tick();
- wrapper.update();
- wrapper.find('Footer').find('Button').simulate('click');
- await tick();
- wrapper.update();
- expect(addFileRequest).toHaveBeenCalledWith(
- `/projects/${org.slug}/${project.slug}/codeowners/`,
- expect.objectContaining({
- data: {raw: '* @MeredithAnya\n', codeMappingId: codeMapping.id},
- })
- );
- const closeModal = wrapper.prop('closeModal');
- expect(closeModal).toHaveBeenCalled();
- });
- });
|