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

import makeFakeUser from '../../factories/user';

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

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

  describe('When user has earnings', () => {
    it('should show earnings', () => {
      cy.intercept('GET', /\S+\/api\/affiliate_partners\/payments/, {
        body: {
          payments: [
            {
              payment_type: 'monthly',
              year: 2021,
              month: 9,
              subscriptions: [
                {
                  plan: 'jamsubsilver',
                  count: 2
                }
              ],
              jamtracks_sold: 3,
              due_amount_in_cents: 5000
            },
            {
              payment_type: 'monthly',
              year: 2021,
              month: 8,
              subscriptions: [
                {
                  plan: 'jamsubgold',
                  count: 1
                },
                {
                  plan: 'jamsubplatinum',
                  count: 2
                }
              ],
              jamtracks_sold: 2,
              due_amount_in_cents: 3000
            }
          ],
          next: null
        }
      }).as('fetchAllEarnings');
      cy.visit('/affiliate/earnings');
      cy.wait('@fetchAllEarnings');
      cy.get('[data-testid=affiliateEarningsList] tbody tr:first-child td')
        .eq(0)
        .should('have.text', 'September 2021');
      cy.get('[data-testid=affiliateEarningsList] tbody tr:first-child td')
        .eq(1)
        .should('have.text', 'Silver - 2');
      cy.get('[data-testid=affiliateEarningsList] tbody tr:first-child td')
        .eq(2)
        .should('have.text', '3');
      cy.get('[data-testid=affiliateEarningsList] tbody tr:first-child td')
        .eq(3)
        .should('have.text', '$50.00');
    });
  });

  describe('in mobile', () => {
    beforeEach(() => {
      cy.viewport('iphone-6');
    });

    it('should show earnings', () => {
      cy.intercept('GET', /\S+\/api\/affiliate_partners\/payments/, {
        body: {
          payments: [
            {
              payment_type: 'monthly',
              year: 2021,
              month: 9,
              subscriptions: [
                {
                  plan: 'jamsubsilver',
                  count: 2
                }
              ],
              jamtracks_sold: 3,
              due_amount_in_cents: 5000
            },
            {
              payment_type: 'monthly',
              year: 2021,
              month: 8,
              subscriptions: [
                {
                  plan: 'jamsubgold',
                  count: 1
                },
                {
                  plan: 'jamsubplatinum',
                  count: 2
                }
              ],
              jamtracks_sold: 2,
              due_amount_in_cents: 3000
            }
          ],
          next: null
        }
      }).as('fetchAllEarningsOnMobile');
      cy.visit('/affiliate/earnings');
      cy.wait('@fetchAllEarningsOnMobile');
      cy.contains('September 2021');
      cy.contains('Silver - 2');
      cy.contains('JamTracks Sold: 3');
      cy.contains('Earnings: $50.00');
    });
  });


});
