/// <reference types="cypress" />

import { day } from 'is_js';
import makeFakeUser from '../../factories/user';

describe('affiliate signups', () => { 
  beforeEach(() => {
    const currentUser = makeFakeUser();
    cy.stubAuthenticate({ id: currentUser.id });
    //cy.stubGonSubscriptionCodes();
  });

  describe('when user has no signups', () => {
    it('should show no signups message', () => {
      cy.intercept('GET', /\S+\/api\/affiliate_partners\/signups?\S+/, {
        body: [
          {
            traffics: [],
            next: null
          }
        ]
      }).as('fetchAllSignups');
      cy.visit('/affiliate/signups');
      cy.wait('@fetchAllSignups');
      cy.contains('There is no signup data yet ');
    });
  });

  describe('when user has signups', () => {
    it('should show signups', () => {
      cy.intercept('GET', /\S+\/api\/affiliate_partners\/signups?\S+/, {
        body: {
          traffics: [
            {
              day: '2021-09-01',
              visits: 100,
              signups: 10,
            },
            {
              day: '2021-09-02',
              visits: 200,
              signups: 20,
            }
          ]
        }
      }).as('fetchAllSignups');
      cy.visit('/affiliate/signups');
      cy.wait('@fetchAllSignups');
      cy.contains('Signups');
      cy.get('[data-testid=affiliateSignupList] tbody tr:first-child > td').eq(0).should('have.text', 'September 2021');
      cy.get('[data-testid=affiliateSignupList] tbody tr:first-child > td').eq(1).should('have.text', '300');
      cy.get('[data-testid=affiliateSignupList] tbody tr:first-child > td').eq(2).should('have.text', '30');
    });
  });

  describe('in mobile', () => { 
    it('should show signups in mobile', () => {
      cy.viewport('iphone-6');
      cy.intercept('GET', /\S+\/api\/affiliate_partners\/signups?\S+/, {
        body: {
          traffics: [
            {
              day: '2021-09-01',
              visits: 100,
              signups: 25,
            },
            {
              day: '2021-10-01',
              visits: 50,
              signups: 10,
            },
            {
              day: '2021-10-02',
              visits: 10,
              signups: 5,
            }
          ]
        }
      }).as('fetchAllSignups');
      cy.visit('/affiliate/signups');
      cy.wait('@fetchAllSignups');
      cy.contains('Signups');
      cy.get('[data-testid=affiliateSignupsSwiper] .swiper-slide').should('have.length', 2);  
      cy.get('[data-testid=affiliateSignupsSwiper] .swiper-button-next').click();
      cy.get('[data-testid=affiliateSignupsSwiper] .swiper-slide').contains('October 2021');
      cy.get('[data-testid=affiliateSignupsSwiper] .swiper-slide').contains('Visits: 60');
      cy.get('[data-testid=affiliateSignupsSwiper] .swiper-slide').contains('Signups: 15');
    });
   })
 })