From 1d46f845bc5a56eab5c375c9f24e6768a84173ea Mon Sep 17 00:00:00 2001 From: julio2505 Date: Mon, 10 Jun 2024 22:51:20 -0600 Subject: [PATCH 1/2] chore: Added configurations for cypress login and subscription functions --- cypress/e2e/CreateChallenge.cy.ts | 25 +---------- cypress/support/commands.ts | 73 ++++++++++++++++--------------- cypress/support/e2e.ts | 2 +- cypress/support/index.d.ts | 9 ++++ 4 files changed, 49 insertions(+), 60 deletions(-) create mode 100644 cypress/support/index.d.ts diff --git a/cypress/e2e/CreateChallenge.cy.ts b/cypress/e2e/CreateChallenge.cy.ts index 788f9b7..d60c967 100644 --- a/cypress/e2e/CreateChallenge.cy.ts +++ b/cypress/e2e/CreateChallenge.cy.ts @@ -1,29 +1,8 @@ -/// - describe('CreateChallenge Component', () => { beforeEach(() => { - // Intercepta la llamada de autenticación de Google y devuelve una respuesta simulada - cy.intercept('GET', '/api/auth/session', (req) => { - req.reply({ - statusCode: 200, - body: { - user: { - name: 'Test User', - email: 'test@example.com', - }, - expires: '2099-01-01T00:00:00.000Z', - }, - }) - }).as('googleLogin') - - cy.visit('/login') - - // Simula el inicio de sesión con Google - cy.get('button').contains('Continuar con Google').click() - - // Espera a que la solicitud de inicio de sesión sea interceptada y respondida - cy.wait('@googleLogin') + cy.login() cy.visit('/home/createChallenge') + cy.getSubscription() }) it('should submit the form with valid input', () => { diff --git a/cypress/support/commands.ts b/cypress/support/commands.ts index 698b01a..4282048 100644 --- a/cypress/support/commands.ts +++ b/cypress/support/commands.ts @@ -1,37 +1,38 @@ /// -// *********************************************** -// This example commands.ts shows you how to -// create various custom commands and overwrite -// existing commands. -// -// For more comprehensive examples of custom -// commands please read more here: -// https://on.cypress.io/custom-commands -// *********************************************** -// -// -// -- This is a parent command -- -// Cypress.Commands.add('login', (email, password) => { ... }) -// -// -// -- This is a child command -- -// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... }) -// -// -// -- This is a dual command -- -// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... }) -// -// -// -- This will overwrite an existing command -- -// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... }) -// -// declare global { -// namespace Cypress { -// interface Chainable { -// login(email: string, password: string): Chainable -// drag(subject: string, options?: Partial): Chainable -// dismiss(subject: string, options?: Partial): Chainable -// visit(originalFn: CommandOriginalFn, url: string, options: Partial): Chainable -// } -// } -// } \ No newline at end of file + +Cypress.Commands.add('login', () => { + // Intercepta la llamada de autenticación de Google y devuelve una respuesta simulada + cy.intercept('GET', '/api/auth/session', (req) => { + req.reply({ + statusCode: 200, + body: { + user: { + name: 'Test User', + email: 'test@example.com', + }, + expires: '2099-01-01T00:00:00.000Z', + }, + }) + }).as('googleLogin') + + cy.visit('/login') + + // Simula el inicio de sesión con Google + cy.get('button').contains('Continuar con Google').click() + + // Espera a que la solicitud de inicio de sesión sea interceptada y respondida + cy.wait('@googleLogin') +}) + +Cypress.Commands.add('getSubscription', () => { + cy.intercept('GET', '/api/subscription', (req) => { + req.reply({ + statusCode: 200, + body: { + plan: 'Bienestar Total', + status: 'active', + end: null, + }, + }) + }) +}) diff --git a/cypress/support/e2e.ts b/cypress/support/e2e.ts index f80f74f..ed5730d 100644 --- a/cypress/support/e2e.ts +++ b/cypress/support/e2e.ts @@ -17,4 +17,4 @@ import './commands' // Alternatively you can use CommonJS syntax: -// require('./commands') \ No newline at end of file +// require('./commands') diff --git a/cypress/support/index.d.ts b/cypress/support/index.d.ts new file mode 100644 index 0000000..7982266 --- /dev/null +++ b/cypress/support/index.d.ts @@ -0,0 +1,9 @@ +// support/index.d.ts +/// + +declare namespace Cypress { + interface Chainable { + login(): void + getSubscription(): void + } +} From 6b81e95b7b90a896111ee7ad54917ec74234566d Mon Sep 17 00:00:00 2001 From: julio2505 Date: Mon, 10 Jun 2024 22:51:31 -0600 Subject: [PATCH 2/2] chore: Added cypress tests for sleep hours --- cypress/e2e/InputSleepHours.cy.ts | 90 +++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 cypress/e2e/InputSleepHours.cy.ts diff --git a/cypress/e2e/InputSleepHours.cy.ts b/cypress/e2e/InputSleepHours.cy.ts new file mode 100644 index 0000000..ad5d408 --- /dev/null +++ b/cypress/e2e/InputSleepHours.cy.ts @@ -0,0 +1,90 @@ +/// + +describe('Sleep hours', () => { + beforeEach(() => { + cy.login() + // Visitar página de ingresar horas de sueño + cy.visit('/sleep/records') + cy.getSubscription() + }) + + it('should submit the form with valid input', () => { + cy.intercept('POST', '/api/records', {}).as('sendRecord') + + // Llena los inputs con valores válidos + const sleepInput = cy + .get('label') + .contains('¿A qué hora te acostaste?') + .next() + .find('input') + sleepInput.type('22:00') + + const wakeupInput = cy + .get('label') + .contains('¿A qué hora te levantaste?') + .next() + .find('input') + wakeupInput.type('06:00') + + //Hacer click en el botón de enviar + cy.get('button').contains('Enviar').click() + + // Verifica que se muestre un mensaje de éxito después de enviar el formulario + cy.contains('Éxito').should('be.visible') + }) + + it('should calculate the correct amount of sleep hours for valid input', () => { + cy.intercept('POST', '/api/records', {}).as('sendRecord') + + // Llena los inputs con valores válidos + const sleepInput = cy + .get('label') + .contains('¿A qué hora te acostaste?') + .next() + .find('input') + sleepInput.type('22:00') + + const wakeupInput = cy + .get('label') + .contains('¿A qué hora te levantaste?') + .next() + .find('input') + wakeupInput.type('06:00') + + //Hacer click en el botón de enviar + cy.get('button').contains('Enviar').click() + + // Verifica que se muestre la cantidad de horas correcta + cy.contains('8.0 horas').should('be.visible') + }) + + it('should return an error if the second time is less than the first time', () => { + cy.intercept('POST', '/api/records', {}).as('sendRecord') + + // Llenar los inputs con una hora de hoy + const sleepInput = cy + .get('label') + .contains('¿A qué hora te acostaste?') + .next() + .find('input') + sleepInput.type('22:00') + sleepInput.siblings('button').contains('Hoy').click() + + // Llenar los inputs con una hora de ayer + const wakeupInput = cy + .get('label') + .contains('¿A qué hora te levantaste?') + .next() + .find('input') + wakeupInput.type('06:00') + wakeupInput.siblings('button').contains('Ayer').click() + + //Hacer click en el botón de enviar + cy.get('button').contains('Enviar').click() + + // Verifica que se muestre un mensaje de error + cy.contains( + 'La cantidad de horas de sueño debe ser mayor o igual a 0', + ).should('be.visible') + }) +})