Skip to content

Commit

Permalink
chore: add post-setup test and seed flight
Browse files Browse the repository at this point in the history
  • Loading branch information
johanohly committed Nov 20, 2024
1 parent 8788b3f commit b50894e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"test:unit": "vitest --run --passWithNoTests",
"test:unit:watch": "vitest --watch --passWithNoTests",
"pretest:e2e": "bun scripts/other/confirm.js \"Are you sure you want to run e2e tests? This will reset your database (yes/no)\"",
"test:e2e": "bun run db:reset && bunx playwright test",
"test:e2e": "bun run db:reset && bunx playwright test && bun run db:reset",
"test:e2e:results": "bunx playwright show-report",
"coverage": "vitest --run --coverage --passWithNoTests",
"db:migrate-dev": "prisma migrate dev --skip-seed",
Expand Down
2 changes: 1 addition & 1 deletion prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'node:path';
import { CamelCasePlugin, Kysely, PostgresDialect } from 'kysely';
import pg from 'pg';

import type { DB } from '../src/lib/db/schema';
import type { DB } from '$lib/db/schema';

const pool = new pg.Pool({ connectionString: process.env.DB_URL });
const dialect = new PostgresDialect({ pool });
Expand Down
39 changes: 39 additions & 0 deletions prisma/seed/flight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { format } from 'date-fns';
import { Kysely } from 'kysely';

import type { DB } from '../../src/lib/db/schema';
import type { CreateFlight } from '../../src/lib/db/types';

export const seedFlight = async (db: Kysely<DB>, userId: string) => {
await createFlight(db, {
seats: [
{ userId, seat: 'window', seatNumber: '11F', seatClass: 'economy' },
],
from: 'EKCH',
to: 'ESSA',
date: format(new Date(), 'yyyy-MM-dd'),
duration: 70 * 60,
});
};

const createFlight = async (db: Kysely<DB>, data: CreateFlight) => {
await db.transaction().execute(async (trx) => {
const { seats, ...flightData } = data;
const resp = await trx
.insertInto('flight')
.values(flightData)
.returning('id')
.executeTakeFirstOrThrow();

const seatData = seats.map((seat) => ({
flightId: resp.id,
userId: seat.userId,
guestName: seat.guestName,
seat: seat.seat,
seatNumber: seat.seatNumber,
seatClass: seat.seatClass,
}));

await trx.insertInto('seat').values(seatData).executeTakeFirstOrThrow();
});
};
4 changes: 3 additions & 1 deletion prisma/seed/initial-seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { Kysely } from 'kysely';

import type { DB } from '../../src/lib/db/schema';

import { seedFlight } from './flight';
import { seedUser } from './user';

export const seedDatabase = async (db: Kysely<DB>) => {
await seedUser(db);
const user = await seedUser(db);
await seedFlight(db, user.id);
};
5 changes: 3 additions & 2 deletions prisma/seed/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export const SEED_USER = {
} as const;

export const seedUser = async (db: Kysely<DB>) => {
await db
return await db
.insertInto('user')
.values({
...SEED_USER,
id: generateId(15),
password: await hashPassword(SEED_USER.password),
})
.execute();
.returning('id')
.executeTakeFirstOrThrow();
};
3 changes: 3 additions & 0 deletions tests/e2e/fixtures/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const isPathname = (pathname: string) => {
return (url: URL) => url.pathname === pathname;
};
4 changes: 2 additions & 2 deletions tests/e2e/user/access.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { expect, test } from '@playwright/test';

import { signin } from '../fixtures/authentication';
import { isPathname } from '../fixtures/url';

test('no unauthorized access', async ({ page }) => {
await page.goto('/');
const url = new URL(page.url());
expect(url.pathname).toBe('/login');
await page.waitForURL(isPathname('/login'));
});

test('can sign in', async ({ page }) => {
Expand Down
8 changes: 7 additions & 1 deletion tests/e2e/user/setup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { SEED_USER } from '../../../prisma/seed/user';
import { test } from '../fixtures/db';
import { isPathname } from '../fixtures/url';

test('can complete set up', async ({ page, db }) => {
await db.deleteFrom('user').execute();
Expand All @@ -12,5 +13,10 @@ test('can complete set up', async ({ page, db }) => {
await page.fill('input[name="displayName"]', SEED_USER.displayName);
await page.click('button[type="submit"]');

await page.waitForURL((url) => url.pathname === '/');
await page.waitForURL(isPathname('/'));
});

test('cannot complete set up if user already exists', async ({ page }) => {
await page.goto('/setup');
await page.waitForURL(isPathname('/login'));
});
2 changes: 1 addition & 1 deletion vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { o7Icon } from '@o7/icon/vite';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
import { o7Icon } from '@o7/icon/vite';

export default defineConfig({
plugins: [o7Icon(), sveltekit()],
Expand Down

0 comments on commit b50894e

Please sign in to comment.