import React from 'react';
import Enzyme, { shallow } from 'enzyme/build/index';
import Adapter from 'enzyme-adapter-react-16/build/index';
import ReactTestRenderer from 'react-test-renderer';
import { TermsNeedingAccept } from 'app/modules/termsNeedingAccept/components/termsNeedingAccept';
import { ConsoleMock } from 'app/testUtils/consoleMock';
import { selectUserTermsToAccept } from 'app/modules/user/store/user.selectors';
import JestMock from 'jest-mock';

jest.mock('app/modules/user/store', () => ({
  userTermsAccept: jest.fn(),
}));

jest.mock('app/modules/user/store/user.selectors', () => ({
  selectUserTermsToAccept: jest.fn(() => ({
    version: '100',
    text: 'terms to accept',
  })),
}));

jest.mock('react-redux', () => ({
  useSelector: jest.fn((selector) => selector()),
}));

describe('TermsNeedingAccept component tests', () => {
  Enzyme.configure({ adapter: new Adapter() });

  let consoleMock, wrapper, component;
  beforeEach(() => {
    consoleMock = new ConsoleMock();
    component = <TermsNeedingAccept/>;
    wrapper = shallow(component);
  });

  afterEach(() => {
    consoleMock.destroy();
  });

  it('renders TermsNeedingAccept correctly', () => {
    const tree = ReactTestRenderer
      .create(component)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });

  it('renders error when data is not provided', () => {
    (selectUserTermsToAccept as JestMock.Mock<any>).mockImplementation(() => null);
    const tree = ReactTestRenderer
      .create(component)
      .toJSON();
    expect(tree).toMatchSnapshot();
  });
});
