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

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

describe('forgot password', () => {
  beforeEach(() => {
    const currentUser = makeFakeUser({
      email: 'sam@example.com'
    });
    cy.clearCookie('remeber_token');
  });

  it('redirects to forgot password page', () => {
    cy.visit('/');
    cy.url().should('include', '/auth/login');
    cy.get('a')
      .contains('Forgot password?')
      .click();
    cy.url().should('include', '/auth/forget-password');
    cy.get('h5').contains('Forgot Your Password');
  });

  describe('validate forgot password form', () => {
    beforeEach(() => {
      cy.visit('/auth/forget-password');
      cy.get('[data-testid=email]').clear();
      cy.get('[data-testid=submit]').should('be.disabled');
    });

    //invalid email format
    it('invalid email format', () => {
      cy.get('[data-testid=email]').type('invalid-email-format@example');
      cy.get('[data-testid=submit]').click();
      cy.url().should('not.include', /\/auth\/confirm-mail?\S+/);
    });

    //valid email format but non-existing
    it('valid email format but non-existing', () => {
      cy.get('[data-testid=email]').type('valid-email-format@example.com');
      cy.get('[data-testid=submit]').click();
      cy.url().should('not.include', /\/auth\/confirm-mail?\S+/);
    });

    //valid and existing email
    it('valid and existing email', () => {
      cy.get('[data-testid=email]').type('nuwan@jamkazam.com');
      cy.get('[data-testid=submit]').click();
      cy.wait(3000);
      cy.contains('Please check your email!');
      cy.url().should('match', /\S+auth\/confirm-mail?\S+/);
      cy.contains('An email has been sent to nuwan@jamkazam.com.')
    });
  });
});
