From 4f89e32b125fe3fe3dfe2a18e79d5c6c89197327 Mon Sep 17 00:00:00 2001 From: Bradley Shellnut Date: Tue, 6 Dec 2022 23:00:48 -0800 Subject: [PATCH] Update zod validation, update tests a bit, and checking type with zod in add to collection and wishlist. --- src/lib/components/game/index.svelte | 2 +- src/lib/components/header/Header.svelte | 4 ++-- src/lib/util/gameMapper.ts | 1 - src/lib/util/manipulateCollection.ts | 19 +++++++++++++---- src/lib/zodValidation.ts | 2 +- src/routes/search/+page.server.ts | 1 - tests/test.ts | 27 ++++++++++++++++++++++++- 7 files changed, 45 insertions(+), 11 deletions(-) diff --git a/src/lib/components/game/index.svelte b/src/lib/components/game/index.svelte index ee19148..fc6ce6c 100644 --- a/src/lib/components/game/index.svelte +++ b/src/lib/components/game/index.svelte @@ -51,7 +51,7 @@ class="thumbnail" href={`/game/${game.id}`} title={`View ${game.name}`} - data-sveltekit-prefetch + data-sveltekit-preload-data > {`Image diff --git a/src/lib/components/header/Header.svelte b/src/lib/components/header/Header.svelte index be547f7..b9319e8 100644 --- a/src/lib/components/header/Header.svelte +++ b/src/lib/components/header/Header.svelte @@ -11,8 +11,8 @@ diff --git a/src/lib/util/gameMapper.ts b/src/lib/util/gameMapper.ts index a06afe7..69f90ac 100644 --- a/src/lib/util/gameMapper.ts +++ b/src/lib/util/gameMapper.ts @@ -14,7 +14,6 @@ export function mapSavedGameToGame(game: SavedGameType): GameType { const { id, name, thumb_url, players, playtime } = game; console.log({ id, name, thumb_url, players, playtime }); - return { id, handle: '', diff --git a/src/lib/util/manipulateCollection.ts b/src/lib/util/manipulateCollection.ts index 903cb7e..ee87199 100644 --- a/src/lib/util/manipulateCollection.ts +++ b/src/lib/util/manipulateCollection.ts @@ -2,13 +2,24 @@ import { collectionStore } from '$lib/stores/collectionStore'; import { toast } from '$lib/components/toast/toast'; import { ToastType, type GameType, type SavedGameType } from '$lib/types'; import { convertToSavedGame } from './gameMapper'; +import { saved_game_schema } from '../zodValidation'; export function addToCollection(game: GameType | SavedGameType) { - collectionStore.add(convertToSavedGame(game)); - toast.send("Added to collection", { duration: 3000, type: ToastType.INFO }); + try { + saved_game_schema.parse(game); + collectionStore.add(convertToSavedGame(game)); + toast.send("Added to collection", { duration: 3000, type: ToastType.INFO }); + } catch (error) { + toast.send('Error adding to collection', { duration: 3000, type: ToastType.ERROR }); + } } export function removeFromCollection(game: GameType | SavedGameType) { - collectionStore.remove(game.id); - toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO }); + try { + saved_game_schema.parse(game); + collectionStore.remove(game.id); + toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO }); + } catch (error) { + toast.send('Error removing from collection', { duration: 3000, type: ToastType.ERROR }); + } } diff --git a/src/lib/zodValidation.ts b/src/lib/zodValidation.ts index a1f7ef3..c74ca8f 100644 --- a/src/lib/zodValidation.ts +++ b/src/lib/zodValidation.ts @@ -13,7 +13,7 @@ export const saved_game_schema = z.object({ name: z.string(), thumb_url: z.string(), players: z.string(), - playtime: z.string() + playtime: IntegerString(z.number()), }); // https://github.com/colinhacks/zod/discussions/330 diff --git a/src/routes/search/+page.server.ts b/src/routes/search/+page.server.ts index b6f2985..9ea773f 100644 --- a/src/routes/search/+page.server.ts +++ b/src/routes/search/+page.server.ts @@ -43,7 +43,6 @@ export const actions: Actions = { queryParams.random = random; } else { try { - console.log('Parsed', search_schema.parse(formData)) const { name, minAge, diff --git a/tests/test.ts b/tests/test.ts index 14cf298..12a0e76 100644 --- a/tests/test.ts +++ b/tests/test.ts @@ -2,5 +2,30 @@ import { expect, test } from '@playwright/test'; test('about page has expected h1', async ({ page }) => { await page.goto('/about'); - expect(await page.textContent('h1')).toBe('About this app'); + expect(await page.textContent('h1')).toBe('About Bored Game'); }); + +test('base page has title and header links', async ({ page }) => { + await page.goto('/'); + + // Expect a title "to contain" a substring + await expect(page).toHaveTitle(/Bored Game \|/); + + // create a locator collection + const collectionLink = page.getByRole('link', { name: 'COLLECTION' }); + + // Expect an attribute "to be strictly equal" to the value. + await expect(collectionLink).toHaveAttribute('href', '/collection'); + + // create a locator collection + const wishlistLink = page.getByRole('link', { name: 'WISHLIST' }); + + // Expect an attribute "to be strictly equal" to the value. + await expect(wishlistLink).toHaveAttribute('href', '/wishlist'); + + // // Click the get started link. + // await getStarted.click(); + + // // Expects the URL to contain intro. + // await expect(page).toHaveURL(/.*intro/); +}) \ No newline at end of file