Skip to content

Commit

Permalink
Merge pull request #1 from gastongaiduk/add-cypress-and-first-tests
Browse files Browse the repository at this point in the history
Add cypress and test login and recently played games
  • Loading branch information
gastongaiduk authored Dec 18, 2024
2 parents dc7976b + 355d0c8 commit 1f7584a
Show file tree
Hide file tree
Showing 14 changed files with 2,453 additions and 117 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Tests

on:
workflow_dispatch:
pull_request:
types: [ opened, synchronize, reopened ]

jobs:
cypress_run:
runs-on: ubuntu-24.04
steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Cypress run
uses: cypress-io/github-action@v6
with:
start: npm run dev:cypress
8 changes: 8 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { defineConfig } from "cypress";

export default defineConfig({
e2e: {
baseUrl: 'http://localhost:5173/retroleaderboards',
defaultCommandTimeout: 10000,
},
});
48 changes: 48 additions & 0 deletions cypress/e2e/login_page.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
describe('login page', () => {
beforeEach(() => {
cy.clearAllLocalStorage()
})

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

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('unauthorized')
cy.get('input[id="key"]').type('unauthorized-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { statusCode: 401 }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.statusCode')
.should('equal', 401)

cy.on('window:alert', (txt) => {
expect(txt).to.contains('Bad credentials');
})

cy.url().should('include', '/login')
cy.contains('Authenticate')
})

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

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.url().should('include', '/games')
})
})
80 changes: 80 additions & 0 deletions cypress/e2e/recently_played_games_page.cy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
describe('recently played games page', () => {
beforeEach(() => {
cy.clearAllLocalStorage()
})

it('no games played', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.url().should('include', '/games')
cy.contains('No games played yet')
})

it('recently played games list with refresh', () => {
cy.visit('/')

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('example')
cy.get('input[id="key"]').type('example-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'recently-played-games-1.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('have.length', 1)

cy.url().should('include', '/games')
cy.contains('Colin McRae Rally')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'recently-played-games-2.json' }).as('getRecentlyPlayedGames')

cy.get('.refresh-button').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('have.length', 2)

cy.get('.game-list li').eq(0).contains('Colin McRae Rally')
cy.get('.game-list li').eq(1).contains('Kirby & The Amazing Mirror')
})

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

cy.url().should('include', '/login')
cy.contains('Authenticate')

cy.get('input[id="username"]').type('player')
cy.get('input[id="key"]').type('player-secret')

cy.intercept('GET', '**/API/API_GetUserRecentlyPlayedGames.php*', { fixture: 'no-played-games.json' }).as('getRecentlyPlayedGames')

cy.contains('Submit').click()

cy.wait('@getRecentlyPlayedGames').its('response.body')
.should('deep.equal', [])

cy.contains('Logout').click()

cy.get('.confirm-button').click()

cy.getAllLocalStorage().should('be.empty')

cy.url().should('include', '/login')
})
})
1 change: 1 addition & 0 deletions cypress/fixtures/no-played-games.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
20 changes: 20 additions & 0 deletions cypress/fixtures/recently-played-games-1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[
{
"GameID": 16557,
"ConsoleID": 12,
"ConsoleName": "PlayStation",
"Title": "Colin McRae Rally",
"ImageIcon": "/Images/036840.png",
"ImageTitle": "/Images/036689.png",
"ImageIngame": "/Images/036690.png",
"ImageBoxArt": "/Images/065054.png",
"LastPlayed": "2024-12-16 22:22:54",
"AchievementsTotal": 104,
"NumPossibleAchievements": 104,
"PossibleScore": 436,
"NumAchieved": 53,
"ScoreAchieved": 173,
"NumAchievedHardcore": 53,
"ScoreAchievedHardcore": 173
}
]
38 changes: 38 additions & 0 deletions cypress/fixtures/recently-played-games-2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[
{
"GameID": 16557,
"ConsoleID": 12,
"ConsoleName": "PlayStation",
"Title": "Colin McRae Rally",
"ImageIcon": "/Images/036840.png",
"ImageTitle": "/Images/036689.png",
"ImageIngame": "/Images/036690.png",
"ImageBoxArt": "/Images/065054.png",
"LastPlayed": "2024-12-16 22:22:54",
"AchievementsTotal": 104,
"NumPossibleAchievements": 104,
"PossibleScore": 436,
"NumAchieved": 53,
"ScoreAchieved": 173,
"NumAchievedHardcore": 53,
"ScoreAchievedHardcore": 173
},
{
"GameID": 769,
"ConsoleID": 5,
"ConsoleName": "Game Boy Advance",
"Title": "Kirby & The Amazing Mirror",
"ImageIcon": "/Images/042405.png",
"ImageTitle": "/Images/033746.png",
"ImageIngame": "/Images/002643.png",
"ImageBoxArt": "/Images/014483.png",
"LastPlayed": "2024-11-24 21:31:43",
"AchievementsTotal": 55,
"NumPossibleAchievements": 55,
"PossibleScore": 311,
"NumAchieved": 21,
"ScoreAchieved": 31,
"NumAchievedHardcore": 21,
"ScoreAchievedHardcore": 31
}
]
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')
8 changes: 8 additions & 0 deletions cypress/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["es5", "dom"],
"types": ["cypress", "node"]
},
"include": ["**/*.ts"]
}
Loading

0 comments on commit 1f7584a

Please sign in to comment.