import {mountWithTheme} from 'sentry-test/enzyme';

import * as modal from 'sentry/actionCreators/modal';
import {Client} from 'sentry/api';
import PluginDetailedView from 'sentry/views/organizationIntegrations/pluginDetailedView';

const mockResponse = mocks => {
  mocks.forEach(([url, body]) =>
    Client.addMockResponse({
      url,
      body,
    })
  );
};

describe('PluginDetailedView', function () {
  const org = TestStubs.Organization();
  let wrapper;

  beforeEach(() => {
    Client.clearMockResponses();

    mockResponse([
      [
        `/organizations/${org.slug}/plugins/configs/?plugins=pagerduty`,
        [
          {
            status: 'unknown',
            description: 'Send alerts to PagerDuty.',
            isTestable: true,
            isHidden: true,
            hasConfiguration: true,
            features: [],
            shortName: 'PagerDuty',
            id: 'pagerduty',
            assets: [],
            featureDescriptions: [],
            name: 'PagerDuty',
            author: {url: 'https://github.com/getsentry/sentry', name: 'Sentry Team'},
            contexts: [],
            doc: '',
            resourceLinks: [
              {url: 'https://github.com/getsentry/sentry/issues', title: 'Report Issue'},
              {
                url: 'https://github.com/getsentry/sentry/tree/master/src/sentry_plugins',
                title: 'View Source',
              },
            ],
            slug: 'pagerduty',
            projectList: [
              {
                projectId: 2,
                configured: true,
                enabled: true,
                projectSlug: 'javascript',

                projectPlatform: 'javascript',
                projectName: 'JavaScript',
              },
            ],
            version: '10.1.0.dev0',
            canDisable: true,
            type: 'notification',
            metadata: {},
          },
        ],
      ],
    ]);

    wrapper = mountWithTheme(
      <PluginDetailedView
        params={{integrationSlug: 'pagerduty', orgId: org.slug}}
        location={{query: {}}}
      />
    );
  });
  it('shows the Integration name and install status', function () {
    expect(wrapper.find('Name').props().children).toEqual('PagerDuty (Legacy)');
    expect(wrapper.find('IntegrationStatus').props().status).toEqual('Installed');
  });
  it('shows the Add to Project button', function () {
    expect(wrapper.find('AddButton').props().disabled).toEqual(false);
    expect(wrapper.find('AddButton').props().children).toEqual('Add to Project');
  });

  it('onClick', function () {
    modal.openModal = jest.fn();
    wrapper.find('AddButton').simulate('click');
    expect(modal.openModal).toHaveBeenCalled();
  });

  it('view configurations', function () {
    wrapper = mountWithTheme(
      <PluginDetailedView
        params={{integrationSlug: 'pagerduty', orgId: org.slug}}
        location={{query: {tab: 'configurations'}}}
      />
    );
    expect(wrapper.find('InstalledPlugin')).toHaveLength(1);
  });
});
