import { GraphQLError } from 'graphql';

const schema = require('../../../fixtures/schema.json');

describe('Email Verifcation', () => {
  beforeEach(() => {
    cy.server();
  });

  it('forwards to homepage if confirmation key is correct', () => {
    cy.viewport('macbook-15');
    cy.mockGraphql({
      schema: schema,
      operations: {
        Coupon: {
          coupon: {
            success: true
          }
        },
      }
    });
    cy.apolloLogin('rahel.cueni', 'test');

    cy.visit('/license-activation');
    cy.redeemCoupon('12345asfd');
    cy.assertStartPage();
  });

  it('displays error if input is missing', () => {
    cy.viewport('macbook-15');
    cy.apolloLogin('rahel.cueni', 'test');

    cy.visit('/license-activation');
    cy.redeemCoupon('');
    cy.get('[data-cy="coupon-local-errors"]').contains('Coupon ist ein Pflichtfeld.');
  });

  it('displays error if coupon input is wrong', () => {
    cy.viewport('macbook-15');
    cy.mockGraphql({
      schema: schema,
      operations: {
        Coupon: new GraphQLError('invalid_coupon')
      }
    });
    cy.apolloLogin('rahel.cueni', 'test');

    cy.visit('/license-activation');
    cy.redeemCoupon('12345asfd');
    cy.get('[data-cy="coupon-remote-errors"]').contains('Der angegebene Coupon-Code ist ungültig.');
  });

  it('displays error if an error occures', () => {
    cy.viewport('macbook-15');
    cy.mockGraphql({
      schema: schema,
      operations: {
        Coupon: new GraphQLError('unknown_error')
      }
    });
    cy.apolloLogin('rahel.cueni', 'test');

    cy.visit('/license-activation');
    cy.redeemCoupon('12345asfd');
    cy.get('[data-cy="coupon-remote-errors"]').contains('Es ist ein Fehler aufgetreten. Bitte versuchen Sie es nochmals oder kontaktieren Sie den Administrator.');
  });
});
