Update zod validation, update tests a bit, and checking type with zod in add to collection and wishlist.

This commit is contained in:
Bradley Shellnut 2022-12-06 23:00:48 -08:00
parent f3d24a15bc
commit 4f89e32b12
7 changed files with 45 additions and 11 deletions

View file

@ -51,7 +51,7 @@
class="thumbnail" class="thumbnail"
href={`/game/${game.id}`} href={`/game/${game.id}`}
title={`View ${game.name}`} title={`View ${game.name}`}
data-sveltekit-prefetch data-sveltekit-preload-data
> >
<Image src={game.thumb_url} alt={`Image of ${game.name}`} /> <Image src={game.thumb_url} alt={`Image of ${game.name}`} />
<!-- <img src={game.thumb_url} alt={`Image of ${game.name}`} /> --> <!-- <img src={game.thumb_url} alt={`Image of ${game.name}`} /> -->

View file

@ -11,8 +11,8 @@
</div> </div>
<nav> <nav>
<a href="/collection" title="Go to your collection" data-sveltekit-prefetch>Collection</a> <a href="/collection" title="Go to your collection" data-sveltekit-preload-data>Collection</a>
<a href="/wishlist" title="Go to your collection" data-sveltekit-prefetch>Wishlist</a> <a href="/wishlist" title="Go to your wishlist" data-sveltekit-preload-data>Wishlist</a>
<Profile /> <Profile />
</nav> </nav>
</header> </header>

View file

@ -14,7 +14,6 @@ export function mapSavedGameToGame(game: SavedGameType): GameType {
const { id, name, thumb_url, players, playtime } = game; const { id, name, thumb_url, players, playtime } = game;
console.log({ id, name, thumb_url, players, playtime }); console.log({ id, name, thumb_url, players, playtime });
return { return {
id, id,
handle: '', handle: '',

View file

@ -2,13 +2,24 @@ import { collectionStore } from '$lib/stores/collectionStore';
import { toast } from '$lib/components/toast/toast'; import { toast } from '$lib/components/toast/toast';
import { ToastType, type GameType, type SavedGameType } from '$lib/types'; import { ToastType, type GameType, type SavedGameType } from '$lib/types';
import { convertToSavedGame } from './gameMapper'; import { convertToSavedGame } from './gameMapper';
import { saved_game_schema } from '../zodValidation';
export function addToCollection(game: GameType | SavedGameType) { export function addToCollection(game: GameType | SavedGameType) {
try {
saved_game_schema.parse(game);
collectionStore.add(convertToSavedGame(game)); collectionStore.add(convertToSavedGame(game));
toast.send("Added to collection", { duration: 3000, type: ToastType.INFO }); 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) { export function removeFromCollection(game: GameType | SavedGameType) {
try {
saved_game_schema.parse(game);
collectionStore.remove(game.id); collectionStore.remove(game.id);
toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO }); toast.send("Removed from collection", { duration: 3000, type: ToastType.INFO });
} catch (error) {
toast.send('Error removing from collection', { duration: 3000, type: ToastType.ERROR });
}
} }

View file

@ -13,7 +13,7 @@ export const saved_game_schema = z.object({
name: z.string(), name: z.string(),
thumb_url: z.string(), thumb_url: z.string(),
players: z.string(), players: z.string(),
playtime: z.string() playtime: IntegerString(z.number()),
}); });
// https://github.com/colinhacks/zod/discussions/330 // https://github.com/colinhacks/zod/discussions/330

View file

@ -43,7 +43,6 @@ export const actions: Actions = {
queryParams.random = random; queryParams.random = random;
} else { } else {
try { try {
console.log('Parsed', search_schema.parse(formData))
const { const {
name, name,
minAge, minAge,

View file

@ -2,5 +2,30 @@ import { expect, test } from '@playwright/test';
test('about page has expected h1', async ({ page }) => { test('about page has expected h1', async ({ page }) => {
await page.goto('/about'); 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/);
})