import React from 'react';
import { configure, mount, shallow } from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import { Provider } from 'react-redux';
import { Link } from '@bufferapp/components';
import NavSidebar, {
  reducer,
} from './index';
import Channels from './components/Channels';

configure({ adapter: new Adapter() });
const storeFake = state => ({
  default: () => {},
  subscribe: () => {},
  dispatch: jest.fn(),
  getState: () => ({ ...state }),
});

const navSidebarState = {
  channels: [
    { id: '5', service: 'facebook', supportedTabs: ['overview', 'posts'] },
    { id: '2', service: 'instagram', supportedTabs: ['overview', 'posts'] },
    { id: '4', service: 'twitter', supportedTabs: ['overview', 'posts'] },
  ],
};

describe('NavSidebar', () => {
  describe('clicking on items', () => {
    let store;

    beforeEach(() => {
      store = storeFake({
        appSidebar: {
          user: {
            productlinks: ['analyze'],
          },
        },
        profiles: {
          organizationId: 'organization123',
          isOwner: false,
          profiles: [{
            id: '4',
            service: 'instagram',
          }],
        },
        account: {
          trialDaysRemaining: 6,
        },
        navSidebar: navSidebarState,
      });
    });

    it('should dispatch CALL_HISTORY_METHOD', () => {
      const wrapper = mount(
        <Provider store={store}>
          <NavSidebar route="/" />
        </Provider>,
      );
      const link = wrapper
        .find(Channels)
        .find(Link)
        .get(0);
      link.props.onClick({ preventDefault: () => {} });
      expect(store.dispatch).toHaveBeenCalledTimes(1);
      expect(store.dispatch.mock.calls[0][0]).toMatchObject({
        type: '@@router/CALL_HISTORY_METHOD',
      });
    });

    it('should not dispatch anything if item is active already', () => {
      const wrapper = mount(
        <Provider store={store}>
          <NavSidebar route="/facebook/overview/5" />
        </Provider>,
      );
      const link = wrapper
        .find(Channels)
        .find(Link)
        .get(0);
      link.props.onClick({ preventDefault: () => {} });
      expect(store.dispatch).toHaveBeenCalledTimes(0);
    });
  });

  it('it should export onClick', () => {
    const store = storeFake({
      appSidebar: {
        user: {
          productlinks: ['analyze'],
        },
      },
      account: {
        trialDaysRemaining: 6,
      },
      profiles: {
        organizationId: 'organization123',
        isOwner: false,
      },
      navSidebar: {
        channels: [],
      },
    });
    const component = shallow(
      <NavSidebar store={store} route="/" />,
    );
    expect(component.props().onClick).toBeDefined();
    component.props().onClick();
    expect(store.dispatch).toHaveBeenCalledTimes(1);
    expect(store.dispatch.mock.calls[0][0]).toMatchObject({
      type: '@@router/CALL_HISTORY_METHOD',
    });
  });

  it('should export reducer', () => {
    expect(reducer)
      .toBeDefined();
  });
});

