Skip to content

Commit

Permalink
Run test in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
kitloong committed May 12, 2024
1 parent 0ac9969 commit 107f292
Show file tree
Hide file tree
Showing 11 changed files with 1,362 additions and 58 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ jobs:
- run: pnpm install
- run: pnpm lint
- run: pnpm build
- run: pnpm test
10 changes: 10 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { defineConfig } from 'cypress'

export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
baseUrl: 'http://localhost:3000',
},
})
84 changes: 84 additions & 0 deletions cypress/e2e/login.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
describe('login spec', () => {
it('Login page rendered', () => {
cy.visit('/login')

// Check if the form from login.tsx is rendered
cy.get('input[name="username"]')
.should('be.visible')
cy.get('input[name="password"]')
.should('be.visible')
cy.get('button[type="submit"]')
.should('be.visible')

// Forgot password a tag with text "Forgot password?" should be visible
cy.contains('a', 'Forgot password?')
.should('be.visible')
cy.contains('a', 'Register Now!')
.should('be.visible')
})

it('Authentication error', () => {
cy.visit('/login')

// Fill in the form
cy.get('input[name="username"]')
.clear()
.type('Username')
cy.get('input[name="password"]')
.clear()
.type('wrong')

cy.intercept('POST', '/api/auth/callback/credentials', {
statusCode: 401,
body: { url: 'http://localhost:3000/api/auth/error?error=Invalid%20username%20or%20password' },
})

// Submit
cy.get('button[type="submit"]')
.click()

// See error message after submit
cy.contains('Invalid username or password')
.should('be.visible')
})

it('Authenticated', () => {
cy.visit('/login')

// Fill in the form
cy.get('input[name="username"]')
.clear()
.type('Username')
cy.get('input[name="password"]')
.clear()
.type('Password')

// Submit
cy.get('button[type="submit"]')
.click()

// Redirected to '/' after submit
cy.url()
.should('eq', `${Cypress.config().baseUrl}/`)
})

it('Authenticated and redirected to callback URL', () => {
cy.visit('/login?callbackUrl=%2Fpokemons')

// Fill in the form
cy.get('input[name="username"]')
.clear()
.type('Username')
cy.get('input[name="password"]')
.clear()
.type('Password')

// Submit
cy.get('button[type="submit"]')
.click()

// Redirected to '/' after submit
cy.url()
.should('eq', `${Cypress.config().baseUrl}/pokemons`)
})
})
5 changes: 5 additions & 0 deletions cypress/fixtures/example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "Using fixtures to represent data",
"email": "[email protected]",
"body": "Fixtures are a great way to mock data for responses to routes"
}
37 changes: 37 additions & 0 deletions cypress/support/commands.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/// <reference types="cypress" />
// ***********************************************
// 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<void>
// drag(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// dismiss(subject: string, options?: Partial<TypeOptions>): Chainable<Element>
// visit(originalFn: CommandOriginalFn, url: string, options: Partial<VisitOptions>): Chainable<Element>
// }
// }
// }
20 changes: 20 additions & 0 deletions cypress/support/e2e.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ***********************************************************
// This example support/e2e.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands'

// Alternatively you can use CommonJS syntax:
// require('./commands')
7 changes: 6 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
"lint": "next lint",
"cypress:open": "cypress open",
"cypress:run": "cypress run",
"test": "start-server-and-test dev http://localhost:3000 cypress:run"
},
"dependencies": {
"@formatjs/intl-localematcher": "^0.5.4",
Expand Down Expand Up @@ -42,6 +45,7 @@
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@typescript-eslint/parser": "^6.21.0",
"cypress": "^13.9.0",
"eslint": "^8.57.0",
"eslint-config-airbnb": "^19.0.4",
"eslint-config-airbnb-typescript": "^17.1.0",
Expand All @@ -52,6 +56,7 @@
"eslint-plugin-react-hooks": "^4.6.2",
"json-server": "^0.17.4",
"sass": "^1.77.0",
"start-server-and-test": "^2.0.3",
"typescript": "^5.4.5"
}
}
Loading

0 comments on commit 107f292

Please sign in to comment.