import React from 'react';
import { shallow, mount } from 'enzyme';
import MastheadSecondary from './masthead-secondary';

describe('<MastheadSecondary />', () => {
  const defaultWindowWidth = window.innerWidth;
  const links = [
    { text: 'Test', href: '/test1', id: '1' },
    { text: 'Test2', href: '/test2', id: '2' },
  ];

  describe('menu closed', () => {
    const masthead = shallow(
      <MastheadSecondary mastheadLinks={links} showSubscribe />,
    );

    it('should not apply the inverse styles', () => {
      expect(masthead.find('MastheadSecondaryLinks').props().inverse).toEqual(
        false,
      );
      expect(masthead.find('MenuDisclosure').props().inverse).toEqual(false);
      expect(masthead.find('CallToAction').props().inverse).toEqual(false);
    });
  });

  describe('menu open', () => {
    let masthead;
    beforeAll(() => {
      window.innerWidth = 500;
      masthead = mount(
        <MastheadSecondary mastheadLinks={links} showSubscribe />,
      );
    });
    afterAll(() => {
      window.innerWidth = defaultWindowWidth;
    });

    it('should apply the inverse styles', () => {
      masthead.find('.ds-menu-disclosure').simulate('click');

      expect(
        masthead
          .find('MastheadSecondaryLinks')
          .first()
          .props().inverse,
      ).toEqual(true);
      expect(masthead.find('MenuDisclosure').props().inverse).toEqual(true);
      expect(
        masthead
          .find('CallToAction')
          .first()
          .props().inverse,
      ).toEqual(true);
    });
  });

  describe('user account login', () => {
    const loginUrl = 'www.test.com';
    const logoutUrl = 'www.logout.com';
    const masthead = shallow(
      <MastheadSecondary
        showLogin
        isLoggedIn
        loginUrl={loginUrl}
        username='Joe'
      />,
    );

    it('should pass the users login status to AccountLogin', () => {
      expect(masthead.find('AccountLogin').props().isLoggedIn).toEqual(true);
    });

    it('should pass the login url to AccountLogin', () => {
      expect(masthead.find('AccountLogin').props().loginUrl).toEqual(loginUrl);
    });

    it('should pass the logout url to AccountLogin if showNavigation prop is false', () => {
      const wrapper = shallow(
        <MastheadSecondary
          showAccountNav={false}
          showLogin
          isLoggedIn
          loginUrl={loginUrl}
          logoutUrl={logoutUrl}
          username='Joe'
        />,
      );
      expect(wrapper.find('AccountLogin').props().logoutUrl).toEqual(logoutUrl);
    });

    it('should not pass the logout url to AccountLogin if showNavigation prop is true', () => {
      const wrapper = shallow(
        <MastheadSecondary
          showAccountNav
          showLogin
          isLoggedIn
          loginUrl={loginUrl}
          logoutUrl={logoutUrl}
          username='Joe'
        />,
      );
      expect(wrapper.find('AccountLogin').props().logoutUrl).toEqual(null);
    });

    it('should pass correct accountData to AccountNavigation for registered user', () => {
      const mastheadSecondary = shallow(
        <MastheadSecondary
          showLogin
          showAccountNav
          isLoggedIn
          showSubscribe
          logoutUrl='/log-out'
          gsClientAdminUrl='/client-admin'
          mySubscriptionUrl='/my-subscription'
          personalInfoUrl='/personal-info'
          billingUrl='/billing'
          newslettersAndPreferencesUrl='/newsletters-and-preferences'
          giftSubscriptionsUrl='/gift-subscriptions'
          helpUrl='/help'
        />,
      );
      const accountNavigationAccountData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in'];
      const accountNavigationAccountMainData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in-main'];

      expect(masthead.find('AccountLogin').props().isLoggedIn).toEqual(true);

      expect(accountNavigationAccountData.length).toEqual(8);
      expect(accountNavigationAccountData[0].name).toEqual('My Economist');
      expect(accountNavigationAccountData[0].url).toEqual('/my-economist');
      expect(accountNavigationAccountData[1].name).toEqual('Saved stories');
      expect(accountNavigationAccountData[1].url).toEqual('/saved-stories');
      expect(accountNavigationAccountData[2].name).toEqual('My subscription');
      expect(accountNavigationAccountData[2].url).toEqual('/my-subscription');
      expect(accountNavigationAccountData[3].name).toEqual(
        'Personal information',
      );
      expect(accountNavigationAccountData[3].url).toEqual('/personal-info');
      expect(accountNavigationAccountData[4].name).toEqual(
        'Newsletters & preferences',
      );
      expect(accountNavigationAccountData[4].url).toEqual(
        '/newsletters-and-preferences',
      );
      expect(accountNavigationAccountData[5].name).toEqual(
        'Gift subscriptions',
      );
      expect(accountNavigationAccountData[5].url).toEqual(
        '/gift-subscriptions',
      );
      expect(accountNavigationAccountData[6].name).toEqual('Help');
      expect(accountNavigationAccountData[6].url).toEqual('/help');
      expect(accountNavigationAccountData[7].name).toEqual('Log out');
      expect(accountNavigationAccountData[7].url).toEqual('/log-out');

      expect(accountNavigationAccountMainData.length).toEqual(3);
      expect(accountNavigationAccountMainData[0].name).toEqual('Saved stories');
      expect(accountNavigationAccountMainData[0].url).toEqual('/saved-stories');
      expect(accountNavigationAccountMainData[1].name).toEqual('Account');
      expect(accountNavigationAccountMainData[1].url).toEqual('/my-economist');
      expect(accountNavigationAccountMainData[2].name).toEqual('Log out');
      expect(accountNavigationAccountMainData[2].url).toEqual('/log-out');
    });

    it('should pass correct accountData to AccountNavigation for B2C subscriber', () => {
      const mastheadSecondary = shallow(
        <MastheadSecondary
          showLogin
          showAccountNav
          isLoggedIn
          showSubscribe={false}
          logoutUrl='/log-out'
          gsClientAdminUrl='/client-admin'
          mySubscriptionUrl='/my-subscription'
          personalInfoUrl='/personal-info'
          billingUrl='/billing'
          newslettersAndPreferencesUrl='/newsletters-and-preferences'
          giftSubscriptionsUrl='/gift-subscriptions'
          helpUrl='/help'
        />,
      );
      const accountNavigationAccountData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in'];
      const accountNavigationAccountMainData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in-main'];

      expect(masthead.find('AccountLogin').props().isLoggedIn).toEqual(true);

      expect(accountNavigationAccountData.length).toEqual(9);
      expect(accountNavigationAccountData[0].name).toEqual('My Economist');
      expect(accountNavigationAccountData[0].url).toEqual('/my-economist');
      expect(accountNavigationAccountData[1].name).toEqual('Saved stories');
      expect(accountNavigationAccountData[1].url).toEqual('/saved-stories');
      expect(accountNavigationAccountData[2].name).toEqual('My subscription');
      expect(accountNavigationAccountData[2].url).toEqual('/my-subscription');
      expect(accountNavigationAccountData[3].name).toEqual(
        'Personal information',
      );
      expect(accountNavigationAccountData[3].url).toEqual('/personal-info');
      expect(accountNavigationAccountData[4].name).toEqual('Billing');
      expect(accountNavigationAccountData[4].url).toEqual('/billing');

      expect(accountNavigationAccountData[5].name).toEqual(
        'Newsletters & preferences',
      );
      expect(accountNavigationAccountData[5].url).toEqual(
        '/newsletters-and-preferences',
      );
      expect(accountNavigationAccountData[6].name).toEqual(
        'Gift subscriptions',
      );
      expect(accountNavigationAccountData[6].url).toEqual(
        '/gift-subscriptions',
      );
      expect(accountNavigationAccountData[7].name).toEqual('Help');
      expect(accountNavigationAccountData[7].url).toEqual('/help');
      expect(accountNavigationAccountData[8].name).toEqual('Log out');
      expect(accountNavigationAccountData[8].url).toEqual('/log-out');

      expect(accountNavigationAccountMainData.length).toEqual(3);
      expect(accountNavigationAccountMainData[0].name).toEqual('Saved stories');
      expect(accountNavigationAccountMainData[0].url).toEqual('/saved-stories');
      expect(accountNavigationAccountMainData[1].name).toEqual('Account');
      expect(accountNavigationAccountMainData[1].url).toEqual('/my-economist');
      expect(accountNavigationAccountMainData[2].name).toEqual('Log out');
      expect(accountNavigationAccountMainData[2].url).toEqual('/log-out');
    });

    it('should pass correct accountData to AccountNavigation for B2B subscriber', () => {
      const mastheadSecondary = shallow(
        <MastheadSecondary
          showLogin
          showAccountNav
          isLoggedIn
          showSubscribe={false}
          logoutUrl='/log-out'
          gsClientAdminUrl='/client-admin'
          mySubscriptionUrl='/my-subscription'
          personalInfoUrl='/personal-info'
          billingUrl='/billing'
          newslettersAndPreferencesUrl='/newsletters-and-preferences'
          giftSubscriptionsUrl='/gift-subscriptions'
          helpUrl='/help'
          isGroupSubscriber
        />,
      );
      const accountNavigationAccountData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in'];
      const accountNavigationAccountMainData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in-main'];

      expect(masthead.find('AccountLogin').props().isLoggedIn).toEqual(true);

      expect(accountNavigationAccountData.length).toEqual(7);
      expect(accountNavigationAccountData[0].name).toEqual('My Economist');
      expect(accountNavigationAccountData[0].url).toEqual('/my-economist');
      expect(accountNavigationAccountData[1].name).toEqual('Saved stories');
      expect(accountNavigationAccountData[1].url).toEqual('/saved-stories');
      expect(accountNavigationAccountData[2].name).toEqual('My subscription');
      expect(accountNavigationAccountData[2].url).toEqual('/my-subscription');
      expect(accountNavigationAccountData[3].name).toEqual(
        'Personal information',
      );
      expect(accountNavigationAccountData[3].url).toEqual('/personal-info');

      expect(accountNavigationAccountData[4].name).toEqual(
        'Newsletters & preferences',
      );
      expect(accountNavigationAccountData[4].url).toEqual(
        '/newsletters-and-preferences',
      );
      expect(accountNavigationAccountData[5].name).toEqual('Help');
      expect(accountNavigationAccountData[5].url).toEqual('/help');
      expect(accountNavigationAccountData[6].name).toEqual('Log out');
      expect(accountNavigationAccountData[6].url).toEqual('/log-out');

      expect(accountNavigationAccountMainData.length).toEqual(3);
      expect(accountNavigationAccountMainData[0].name).toEqual('Saved stories');
      expect(accountNavigationAccountMainData[0].url).toEqual('/saved-stories');
      expect(accountNavigationAccountMainData[1].name).toEqual('Account');
      expect(accountNavigationAccountMainData[1].url).toEqual('/my-economist');
      expect(accountNavigationAccountMainData[2].name).toEqual('Log out');
      expect(accountNavigationAccountMainData[2].url).toEqual('/log-out');
    });

    it('should pass correct accountData to AccountNavigation for client admin user', () => {
      const mastheadSecondary = shallow(
        <MastheadSecondary
          showLogin
          showAccountNav
          isLoggedIn
          showSubscribe={false}
          logoutUrl='/log-out'
          gsClientAdminUrl='/client-admin'
          mySubscriptionUrl='/my-subscription'
          personalInfoUrl='/personal-info'
          billingUrl='/billing'
          newslettersAndPreferencesUrl='/newsletters-and-preferences'
          giftSubscriptionsUrl='/gift-subscriptions'
          helpUrl='/help'
          isClientAdmin
        />,
      );
      const accountNavigationAccountData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in'];
      const accountNavigationAccountMainData = mastheadSecondary
        .find('AccountNavigation')
        .props().accountData['account-logged-in-main'];

      expect(masthead.find('AccountLogin').props().isLoggedIn).toEqual(true);

      expect(accountNavigationAccountData.length).toEqual(8);
      expect(accountNavigationAccountData[0].name).toEqual('My Economist');
      expect(accountNavigationAccountData[0].url).toEqual('/my-economist');
      expect(accountNavigationAccountData[1].name).toEqual('Saved stories');
      expect(accountNavigationAccountData[1].url).toEqual('/saved-stories');
      expect(accountNavigationAccountData[2].name).toEqual('Admin account');
      expect(accountNavigationAccountData[2].url).toEqual('/client-admin');
      expect(accountNavigationAccountData[3].name).toEqual('My subscription');
      expect(accountNavigationAccountData[3].url).toEqual('/my-subscription');
      expect(accountNavigationAccountData[4].name).toEqual(
        'Personal information',
      );
      expect(accountNavigationAccountData[4].url).toEqual('/personal-info');

      expect(accountNavigationAccountData[5].name).toEqual(
        'Newsletters & preferences',
      );
      expect(accountNavigationAccountData[5].url).toEqual(
        '/newsletters-and-preferences',
      );
      expect(accountNavigationAccountData[6].name).toEqual('Help');
      expect(accountNavigationAccountData[6].url).toEqual('/help');
      expect(accountNavigationAccountData[7].name).toEqual('Log out');
      expect(accountNavigationAccountData[7].url).toEqual('/log-out');

      expect(accountNavigationAccountMainData.length).toEqual(4);
      expect(accountNavigationAccountMainData[0].name).toEqual('Saved stories');
      expect(accountNavigationAccountMainData[0].url).toEqual('/saved-stories');
      expect(accountNavigationAccountMainData[1].name).toEqual('Admin account');
      expect(accountNavigationAccountMainData[1].url).toEqual('/client-admin');
      expect(accountNavigationAccountMainData[2].name).toEqual('Account');
      expect(accountNavigationAccountMainData[2].url).toEqual('/my-economist');
      expect(accountNavigationAccountMainData[3].name).toEqual('Log out');
      expect(accountNavigationAccountMainData[3].url).toEqual('/log-out');
    });
  });

  describe('subscribe', () => {
    const subscribeUrl = 'www.testsubscribe.com';

    it('should use the default subscribe url', () => {
      const masthead = shallow(<MastheadSecondary showSubscribe isLoggedIn />);
      expect(masthead.find('CallToAction').props().href).toEqual(
        'https://subscription.economist.com/DE/EngCore/Ecom/Masthead',
      );
    });

    it('should use a custom subscrbe url if passed as a prop', () => {
      const masthead = shallow(
        <MastheadSecondary
          showSubscribe
          isLoggedIn
          subscribeUrl={subscribeUrl}
        />,
      );
      expect(masthead.find('CallToAction').props().href).toEqual(subscribeUrl);
    });

    it('should use "Subscribe" for text on CTA if subscribeCtaText not passed', () => {
      const masthead = shallow(<MastheadSecondary showSubscribe />);
      expect(masthead.find('CallToAction').props().text).toEqual('Subscribe');
    });

    it('should use "Upgrade" for text on CTA if passed via subscribeCtaText', () => {
      const masthead = shallow(
        <MastheadSecondary showSubscribe subscribeCtaText='Upgrade' />,
      );
      expect(masthead.find('CallToAction').props().text).toEqual('Upgrade');
    });
  });

  describe('selected link', () => {
    it('should pass the selected link to MastheadLinks if passed as a prop', () => {
      const masthead = shallow(
        <MastheadSecondary mastheadLinks={links} selectedLink={0} />,
      );
      expect(
        masthead.find('MastheadSecondaryLinks').props().selectedLink,
      ).toEqual(0);
    });
  });

  describe('MenuDisclosure', () => {
    it('should not be rendered if the props mastheadLinks or showLogin are null', () => {
      const masthead = shallow(<MastheadSecondary />);
      expect(masthead.find('MenuDisclosure').length).toEqual(0);
    });
    it('should be rendered if the prop mastheadLinks is passed', () => {
      const masthead = shallow(<MastheadSecondary mastheadLinks={links} />);
      expect(masthead.find('MenuDisclosure').length).toEqual(1);
    });
  });

  describe('give a gift', () => {
    it('should show the give a gift button when showSubscribe is false and showGiftGivingButton is true', () => {
      const masthead = shallow(
        <MastheadSecondary
          showGiftGivingButton
          isLoggedIn
          showSubscribe={false}
        />,
      );
      expect(masthead.find('CallToAction').props().href).toEqual(
        'https://subscribenow.economist.com/gift',
      );
    });
  });
});
