123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- import {mountWithTheme} from 'sentry-test/enzyme';
- import {Client} from 'sentry/api';
- import RepositoryStore from 'sentry/stores/repositoryStore';
- import IntegrationRepos from 'sentry/views/organizationIntegrations/integrationRepos';
- describe('IntegrationRepos', function () {
- const org = TestStubs.Organization();
- const integration = TestStubs.GitHubIntegration();
- beforeEach(() => {
- Client.clearMockResponses();
- jest.spyOn(RepositoryStore, 'resetRepositories');
- });
- afterEach(() => {
- jest.restoreAllMocks();
- });
- describe('Getting repositories', function () {
- it('handles broken integrations', function () {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- statusCode: 400,
- body: {detail: 'Invalid grant'},
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- const wrapper = mountWithTheme(<IntegrationRepos integration={integration} />);
- expect(wrapper.find('PanelBody')).toHaveLength(0);
- expect(wrapper.find('Alert')).toHaveLength(1);
- });
- });
- describe('Adding repositories', function () {
- it('can save successfully', async function () {
- const addRepo = Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'POST',
- body: TestStubs.Repository({integrationId: '1'}),
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- body: {
- repos: [{identifier: 'example/repo-name', name: 'repo-name'}],
- },
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- const wrapper = mountWithTheme(<IntegrationRepos integration={integration} />);
- wrapper.find('DropdownButton').simulate('click');
- wrapper.find('StyledListElement').simulate('click');
- expect(addRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/`,
- expect.objectContaining({
- data: {
- installation: '1',
- provider: 'integrations:github',
- identifier: 'example/repo-name',
- },
- })
- );
- await tick();
- wrapper.update();
- const name = wrapper.find('RepositoryRow').find('strong').first();
- expect(name).toHaveLength(1);
- expect(name.text()).toEqual('example/repo-name');
- expect(RepositoryStore.resetRepositories).toHaveBeenCalled();
- });
- it('handles failure during save', function () {
- const addRepo = Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'POST',
- statusCode: 400,
- body: {
- errors: {
- __all__: 'Repository already exists.',
- },
- },
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- body: {
- repos: [{identifier: 'getsentry/sentry', name: 'sentry'}],
- },
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- const wrapper = mountWithTheme(<IntegrationRepos integration={integration} />);
- wrapper.find('DropdownButton').simulate('click');
- wrapper.find('StyledListElement').simulate('click');
- wrapper.update();
- expect(addRepo).toHaveBeenCalled();
- expect(wrapper.find('RepoOption')).toHaveLength(0);
- });
- });
- describe('migratable repo', function () {
- it('associates repository with integration', async () => {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- body: [
- TestStubs.Repository({
- integrationId: null,
- externalSlug: 'example/repo-name',
- provider: {
- id: 'integrations:github',
- name: 'GitHub',
- status: 'active',
- },
- }),
- ],
- });
- Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
- body: {repos: [{identifier: 'example/repo-name', name: 'repo-name'}]},
- });
- const updateRepo = Client.addMockResponse({
- method: 'PUT',
- url: `/organizations/${org.slug}/repos/4/`,
- body: {},
- });
- const wrapper = mountWithTheme(<IntegrationRepos integration={integration} />);
- wrapper.find('DropdownButton').simulate('click');
- wrapper.find('StyledListElement').simulate('click');
- await tick();
- expect(updateRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/4/`,
- expect.objectContaining({
- data: {integrationId: '1'},
- })
- );
- expect(RepositoryStore.resetRepositories).toHaveBeenCalled();
- });
- it('uses externalSlug not name for comparison', () => {
- Client.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [TestStubs.Repository({name: 'repo-name', externalSlug: 9876})],
- });
- const getItems = Client.addMockResponse({
- url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
- method: 'GET',
- body: {
- repos: [{identifier: 9876, name: 'repo-name'}],
- },
- });
- const updateRepo = Client.addMockResponse({
- method: 'PUT',
- url: `/organizations/${org.slug}/repos/4/`,
- body: {},
- });
- const wrapper = mountWithTheme(<IntegrationRepos integration={integration} />);
- wrapper.find('DropdownButton').simulate('click');
- wrapper.find('StyledListElement').simulate('click');
- expect(getItems).toHaveBeenCalled();
- expect(updateRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/4/`,
- expect.objectContaining({
- data: {integrationId: '1'},
- })
- );
- });
- });
- });
|