Merge pull request 'test: integrate testing library' (#56)
All checks were successful
container-scan / Container Scan (push) Successful in 6m45s
docker-build / docker (push) Successful in 9m7s
tests / Tests (push) Successful in 4m41s

Reviewed-on: #56
Reviewed-by: Maximilian Liebmann <lima@noreply.git.dominikstahl.dev>
This commit is contained in:
Maximilian Liebmann 2025-06-25 13:09:05 +00:00
commit 0e24f7917b
21 changed files with 1396 additions and 39 deletions

6
.env.test Normal file
View file

@ -0,0 +1,6 @@
AUTH_SECRET="auth_secret"
AUTH_URL="http://127.0.0.1:3000"
HOSTNAME="127.0.0.1"
DATABASE_URL="file:/tmp/dev.db"
AUTH_AUTHENTIK_ID="id"
AUTH_AUTHENTIK_ISSUER="issuer"

34
.github/workflows/tests.yml vendored Normal file
View file

@ -0,0 +1,34 @@
name: tests
on:
push:
branches:
- main
- renovate/*
pull_request:
jobs:
tests:
name: Tests
runs-on: docker
container:
image: cypress/browsers:latest
options: --user 1001
steps:
- name: Checkout
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
- name: Enable corepack
run: corepack enable
- name: Cypress run (e2e)
uses: https://github.com/cypress-io/github-action@v6
with:
build: yarn cypress:build
start: yarn cypress:start_server
e2e: true
wait-on: 'http://127.0.0.1:3000'
- name: Cypress run (component)
uses: https://github.com/cypress-io/github-action@v6
with:
component: true
install: false

6
.gitignore vendored
View file

@ -33,6 +33,7 @@ yarn-error.log*
# env files (can opt-in for committing if needed)
.env*
!.env.example
!.env.test
# vercel
.vercel
@ -45,3 +46,8 @@ next-env.d.ts
/prisma/*.db*
src/generated/*
data
# cypress
cypress/videos
cypress/screenshots
cypress/coverage

16
cypress.config.ts Normal file
View file

@ -0,0 +1,16 @@
import { defineConfig } from 'cypress';
export default defineConfig({
component: {
devServer: {
framework: 'next',
bundler: 'webpack',
},
},
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});

12
cypress/e2e/auth-user.ts Normal file
View file

@ -0,0 +1,12 @@
export default function authUser() {
cy.visit('http://127.0.0.1:3000/login');
cy.getBySel('login-header').should('exist');
cy.getBySel('login-form').should('exist');
cy.getBySel('email-input').should('exist');
cy.getBySel('password-input').should('exist');
cy.getBySel('login-button').should('exist');
cy.getBySel('email-input').type('cypress@example.com');
cy.getBySel('password-input').type('Password123!');
cy.getBySel('login-button').click();
cy.url().should('include', '/home');
}

View file

@ -0,0 +1,9 @@
import authUser from './auth-user';
describe('event creation', () => {
it('loads', () => {
authUser();
// cy.visit('http://127.0.0.1:3000/events/new'); // TODO: Add event creation tests
});
});

45
cypress/e2e/login.cy.ts Normal file
View file

@ -0,0 +1,45 @@
describe('login and register', () => {
it('loads', () => {
cy.visit('http://127.0.0.1:3000/');
cy.getBySel('login-header').should('exist');
});
it('shows register form', () => {
cy.visit('http://127.0.0.1:3000/');
cy.getBySel('register-switch').click();
cy.getBySel('register-form').should('exist');
cy.getBySel('first-name-input').should('exist');
cy.getBySel('last-name-input').should('exist');
cy.getBySel('email-input').should('exist');
cy.getBySel('username-input').should('exist');
cy.getBySel('password-input').should('exist');
cy.getBySel('confirm-password-input').should('exist');
cy.getBySel('register-button').should('exist');
});
it('allows to register', async () => {
cy.visit('http://127.0.0.1:3000/');
cy.getBySel('register-switch').click();
cy.getBySel('first-name-input').type('Test');
cy.getBySel('last-name-input').type('User');
cy.getBySel('email-input').type('test@example.com');
cy.getBySel('username-input').type('testuser');
cy.getBySel('password-input').type('Password123!');
cy.getBySel('confirm-password-input').type('Password123!');
cy.getBySel('register-button').click();
cy.getBySel('login-header').should('exist');
cy.getBySel('login-form').should('exist');
cy.getBySel('email-input').should('exist');
cy.getBySel('password-input').should('exist');
cy.getBySel('login-button').should('exist');
cy.getBySel('email-input').type('test@example.com');
cy.getBySel('password-input').type('Password123!');
cy.getBySel('login-button').click();
cy.url().should('include', '/home');
});
});

29
cypress/e2e/seed.ts Normal file
View file

@ -0,0 +1,29 @@
import { PrismaClient } from '../../src/generated/prisma';
const prisma = new PrismaClient();
export default async function requireUser() {
await prisma.$transaction(async (tx) => {
const { id } = await tx.user.create({
data: {
email: 'cypress@example.com',
name: 'cypress',
password_hash:
'$2a$10$FmkVRHXzMb63dLHHwG1mDOepZJirL.U964wU/3Xr7cFis8XdRh8sO',
first_name: 'Cypress',
last_name: 'Tester',
emailVerified: new Date(),
},
});
await tx.account.create({
data: {
userId: id,
type: 'credentials',
provider: 'credentials',
providerAccountId: id,
},
});
});
}
requireUser();

View file

@ -0,0 +1,62 @@
/// <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>
// }
// }
// }
Cypress.Commands.add('getBySel', (selector, ...args) => {
return cy.get(`[data-cy=${selector}]`, ...args);
});
Cypress.Commands.add('getBySelLike', (selector, ...args) => {
return cy.get(`[data-cy*=${selector}]`, ...args);
});
declare global {
namespace Cypress {
interface Chainable {
getBySel(
selector: string,
...args: any[]
): Chainable<JQuery<HTMLElement>>;
getBySelLike(
selector: string,
...args: any[]
): Chainable<JQuery<HTMLElement>>;
}
}
}
export {};

View file

@ -0,0 +1,14 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width,initial-scale=1.0" />
<title>Components App</title>
<!-- Used by Next.js to inject CSS. -->
<div id="__next_css__DO_NOT_USE__"></div>
</head>
<body>
<div data-cy-root></div>
</body>
</html>

View file

@ -0,0 +1,38 @@
// ***********************************************************
// This example support/component.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 '@/app/globals.css';
// Import commands.js using ES2015 syntax:
import './commands';
import { mount } from 'cypress/react';
// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount;
}
}
}
Cypress.Commands.add('mount', mount);
// Example use:
// cy.mount(<MyComponent />)

17
cypress/support/e2e.ts Normal file
View file

@ -0,0 +1,17 @@
// ***********************************************************
// 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';

View file

@ -22,16 +22,15 @@ async function exportSwagger() {
);
await Promise.all(
filesToImport.map((file) => {
return import(file)
.then((module) => {
if (module.default) {
module.default(registry);
}
})
.catch((error) => {
console.error(`Error importing ${file}:`, error);
});
filesToImport.map(async (file) => {
try {
const moduleImp = await import(file);
if (moduleImp.default) {
moduleImp.default(registry);
}
} catch (error) {
console.error(`Error importing ${file}:`, error);
}
}),
);

View file

@ -5,7 +5,7 @@
"scripts": {
"dev": "next dev --turbopack",
"build": "prettier --check . && next build",
"start": "next start",
"start": "node .next/standalone/server.js",
"lint": "next lint",
"format": "prettier --write .",
"prisma:migrate": "dotenv -e .env.local -- prisma migrate dev",
@ -15,7 +15,11 @@
"prisma:migrate:reset": "dotenv -e .env.local -- prisma migrate reset",
"dev_container": "docker compose -f docker-compose.dev.yml up --watch --build",
"swagger:generate": "ts-node -r tsconfig-paths/register exportSwagger.ts",
"orval:generate": "orval"
"orval:generate": "orval",
"cypress:build": "rm -rf /tmp/dev.db && DATABASE_URL=\"file:/tmp/dev.db\" yarn prisma:generate && yarn swagger:generate && yarn orval:generate && DATABASE_URL=\"file:/tmp/dev.db\" yarn prisma:db:push && prettier --check . && NODE_ENV=test next build",
"cypress:start_server": "DATABASE_URL=\"file:/tmp/dev.db\" ts-node cypress/e2e/seed.ts && cp .env.test .next/standalone && cp public .next/standalone/ -r && cp .next/static/ .next/standalone/.next/ -r && NODE_ENV=test HOSTNAME=\"0.0.0.0\" dotenv -e .env.test -- node .next/standalone/server.js",
"cypress:open": "cypress open",
"cypress:run": "cypress run"
},
"dependencies": {
"@asteasolutions/zod-to-openapi": "^8.0.0-beta.4",
@ -63,6 +67,7 @@
"@types/react-dom": "19.1.6",
"@types/swagger-ui-react": "5",
"@types/webpack-env": "1.18.8",
"cypress": "14.5.0",
"dotenv-cli": "8.0.0",
"eslint": "9.29.0",
"eslint-config-next": "15.3.4",

View file

@ -33,7 +33,10 @@ export default async function LoginPage() {
</div>
<div className='mt-auto mb-auto'>
<Card className='w-[350px] max-w-screen;'>
<CardHeader className='grid place-items-center'>
<CardHeader
className='grid place-items-center'
data-cy='login-header'
>
<Logo colorType='colored' logoType='secondary'></Logo>
</CardHeader>
<CardContent className='gap-6 flex flex-col items-center'>
@ -46,6 +49,7 @@ export default async function LoginPage() {
key={provider.id}
provider={provider.id}
providerDisplayName={provider.name}
data-cy={'sso-login-button_' + provider.name.toLowerCase()}
/>
))}
</CardContent>

View file

@ -5,10 +5,11 @@ import { faOpenid } from '@fortawesome/free-brands-svg-icons';
export default function SSOLogin({
provider,
providerDisplayName,
...props
}: {
provider: string;
providerDisplayName: string;
}) {
} & React.HTMLAttributes<HTMLButtonElement>) {
return (
<form
className='flex flex-col items-center w-full'
@ -22,6 +23,7 @@ export default function SSOLogin({
type='submit'
variant='secondary'
icon={faOpenid}
{...props}
>
Login with {providerDisplayName}
</IconButton>

View file

@ -48,13 +48,18 @@ function LoginFormElement({
});
return (
<form className='flex flex-col gap-5 w-full' onSubmit={onSubmit}>
<form
className='flex flex-col gap-5 w-full'
onSubmit={onSubmit}
data-cy='login-form'
>
<LabeledInput
type='text'
label='E-Mail or Username'
placeholder='What you are known as'
error={formState.errors.email?.message}
{...register('email')}
data-cy='email-input'
/>
<LabeledInput
type='password'
@ -62,9 +67,10 @@ function LoginFormElement({
placeholder="Let's hope you remember it"
error={formState.errors.password?.message}
{...register('password')}
data-cy='password-input'
/>
<div className='grid grid-rows-2 gap-2'>
<Button type='submit' variant='primary'>
<Button type='submit' variant='primary' data-cy='login-button'>
Login
</Button>
<Button
@ -74,6 +80,7 @@ function LoginFormElement({
formRef?.current?.reset();
setIsSignUp((v) => !v);
}}
data-cy='register-switch'
>
Sign Up
</Button>
@ -129,6 +136,7 @@ function RegisterFormElement({
ref={formRef}
className='flex flex-col gap-5 w-full'
onSubmit={onSubmit}
data-cy='register-form'
>
<LabeledInput
type='text'
@ -137,6 +145,7 @@ function RegisterFormElement({
autocomplete='given-name'
error={formState.errors.firstName?.message}
{...register('firstName')}
data-cy='first-name-input'
/>
<LabeledInput
type='text'
@ -145,6 +154,7 @@ function RegisterFormElement({
autocomplete='family-name'
error={formState.errors.lastName?.message}
{...register('lastName')}
data-cy='last-name-input'
/>
<LabeledInput
type='email'
@ -153,6 +163,7 @@ function RegisterFormElement({
autocomplete='email'
error={formState.errors.email?.message}
{...register('email')}
data-cy='email-input'
/>
<LabeledInput
type='text'
@ -161,6 +172,7 @@ function RegisterFormElement({
autocomplete='username'
error={formState.errors.username?.message}
{...register('username')}
data-cy='username-input'
/>
<LabeledInput
type='password'
@ -169,6 +181,7 @@ function RegisterFormElement({
autocomplete='new-password'
error={formState.errors.password?.message}
{...register('password')}
data-cy='password-input'
/>
<LabeledInput
type='password'
@ -177,9 +190,10 @@ function RegisterFormElement({
autocomplete='new-password'
error={formState.errors.confirmPassword?.message}
{...register('confirmPassword')}
data-cy='confirm-password-input'
/>
<div className='grid grid-rows-2 gap-2'>
<Button type='submit' variant='primary'>
<Button type='submit' variant='primary' data-cy='register-button'>
Sign Up
</Button>
<Button

View file

@ -0,0 +1,41 @@
import React from 'react';
import { ThemePicker } from '@/components/misc/theme-picker';
import { ThemeProvider } from '@/components/wrappers/theme-provider';
describe('<ThemePicker />', () => {
it('renders', () => {
cy.mount(<ThemePicker />);
});
it('toggle open and close', () => {
cy.mount(<ThemePicker />);
cy.getBySel('theme-picker').click();
cy.getBySel('theme-picker-content').should('exist');
cy.get('html').click();
cy.getBySel('theme-picker-content').should('not.exist');
});
it('enable dark mode', () => {
cy.mount(
<ThemeProvider>
<ThemePicker />
</ThemeProvider>,
);
cy.getBySel('theme-picker').click();
cy.getBySel('dark-theme').click();
cy.get('html').should('have.attr', 'data-theme', 'dark');
});
it('enable light mode', () => {
cy.mount(
<ThemeProvider>
<ThemePicker />
</ThemeProvider>,
);
cy.getBySel('theme-picker').click();
cy.getBySel('light-theme').click();
cy.get('html').should('have.attr', 'data-theme', 'light');
});
});

View file

@ -18,20 +18,26 @@ export function ThemePicker() {
return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='outline_primary' size='icon'>
<Button variant='outline_primary' size='icon' data-cy='theme-picker'>
<Sun className='h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0' />
<Moon className='absolute h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100' />
<span className='sr-only'>Toggle theme</span>
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
<DropdownMenuItem onClick={() => setTheme('light')}>
<DropdownMenuContent align='end' data-cy='theme-picker-content'>
<DropdownMenuItem
onClick={() => setTheme('light')}
data-cy='light-theme'
>
Light
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('dark')}>
<DropdownMenuItem onClick={() => setTheme('dark')} data-cy='dark-theme'>
Dark
</DropdownMenuItem>
<DropdownMenuItem onClick={() => setTheme('system')}>
<DropdownMenuItem
onClick={() => setTheme('system')}
data-cy='system-theme'
>
System
</DropdownMenuItem>
</DropdownMenuContent>

View file

@ -20,7 +20,8 @@
],
"paths": {
"@/*": ["./src/*"]
}
},
"types": ["node", "cypress", "@types/webpack-env"]
},
"ts-node": {
"compilerOptions": {

1029
yarn.lock

File diff suppressed because it is too large Load diff