diff options
-rw-r--r-- | .vite.config.ts | 7 | ||||
-rw-r--r-- | GNUmakefile | 4 | ||||
-rw-r--r-- | controllers/games/root.js | 4 | ||||
-rw-r--r-- | database/games.js | 2 | ||||
-rw-r--r-- | fixtures.js | 25 | ||||
-rw-r--r-- | tests/root.spec.js | 28 |
6 files changed, 48 insertions, 22 deletions
diff --git a/.vite.config.ts b/.vite.config.ts new file mode 100644 index 0000000..1563558 --- /dev/null +++ b/.vite.config.ts @@ -0,0 +1,7 @@ +import { configDefaults, defineConfig } from 'vitest/config'; + +export default defineConfig({ + test: { + exclude: [...configDefaults.exclude, '*.db', 'GNUmakefile', 'LICENSE', '.eslintrc.json'], + }, +}); diff --git a/GNUmakefile b/GNUmakefile index badea96..47028dd 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -12,6 +12,8 @@ init: ## make init # initialize project dependencies .PHONY: serve serve: ## make serve # run a self reloading nodejs web server + rm -f jdm.db sessions.db + NODE_ENV=development node fixtures.js NODE_ENV=development nodemon main.js .PHONY: run @@ -22,7 +24,7 @@ run: ## make run # run a production nodejs web server test: check ## make test # run tests @rm -f testjdm.db testsessions.db NODE_ENV=test node fixtures.js - NODE_ENV=test vitest + NODE_ENV=test vitest --config .vite.config.ts @rm -f testjdm.db testsessions.db help: diff --git a/controllers/games/root.js b/controllers/games/root.js index 0483f55..4a4e80d 100644 --- a/controllers/games/root.js +++ b/controllers/games/root.js @@ -19,9 +19,7 @@ function makePageData(user) { export function root_get(req, res) { let page = makePageData(req.session.user); - for (let i=0; i<page.games.length; i++) { - page.games[i].data = JSON.parse(page.games[i].data); - } + page.games.forEach(g => g.data = JSON.parse(g.data)); return res.render("games", page); } diff --git a/database/games.js b/database/games.js index dae3ce6..3517d0a 100644 --- a/database/games.js +++ b/database/games.js @@ -8,7 +8,6 @@ export function getGame(id) { try { return getGameStatement.get(id); } catch (err) { - console.log(err); return null; } } @@ -26,7 +25,6 @@ export function newGame(player1, player2, data) { try { return newGameStatement.run(player1, player2, JSON.stringify(data)).lastInsertRowid; } catch (err) { - console.log(err); return null; } } diff --git a/fixtures.js b/fixtures.js index bfc6b3c..f036f01 100644 --- a/fixtures.js +++ b/fixtures.js @@ -1,4 +1,23 @@ -import {createUser} from "./database/users.js"; +import { newGame } from './database/games.js'; +import { createUser } from './database/users.js'; +import { emptyBoard } from './utils/board.js'; -createUser("Alice", "Alice42!", "alice@example.com"); -createUser("Bob", "Bob42!", "bob@example.com"); +await createUser('Alice', 'Alice42!', 'alice@example.com'); +await createUser('Bob', 'Bob42!', 'bob@example.com'); +const data = { + board: emptyBoard, + name: 'Alice vs Bob', + player1: { + id: 1, + username: 'Alice', + score: 0, + letters: ['A', 'B', 'C', 'D', 'E', 'F', 'G'], + }, + player2: { + id: 2, + username: 'Bob', + score: 0, + letters: ['T', 'U', 'V', 'W', 'X', 'Y', 'Z'], + }, +}; +newGame(1, 2, data); diff --git a/tests/root.spec.js b/tests/root.spec.js index ffceda5..308d35c 100644 --- a/tests/root.spec.js +++ b/tests/root.spec.js @@ -5,14 +5,14 @@ import app from "../main.js"; const request = supertest(app); -describe.concurrent("Root handlers tests", async function() { - describe.concurrent("When not logged in", async function() { - it("/", async function() { request.get("/").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); - it("/login", async function() { request.get("/login").expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); }); - it("/logout", async function() { request.get("/logout").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); }); +describe.concurrent("Root handlers tests", function() { + describe.concurrent("When not logged in", function() { + it("GET /", async function() { await request.get("/").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); + it("GET /login", async function() { await request.get("/login").expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); }); + it("GET /logout", async function() { await request.get("/logout").expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); }); }); - describe.concurrent("With valid credentials", async function() { + describe.concurrent("With valid credentials", function() { beforeEach(async function(ctx) { const authResponse = await request.post("/login") .send("username=Alice&password=Alice42!") @@ -21,14 +21,16 @@ describe.concurrent("Root handlers tests", async function() { .expect(302, /Redirecting to \/games$/); ctx.cookie = authResponse.get("Set-Cookie"); }); - it("/", async function(ctx) { request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); }); - it("/login", async function(ctx) { request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); }); - describe("logout", async function(ctx) { - it("/logout", async function() { request.get("/logout").set("cookie", ctx.cookie).expect("content-type", /text\/plain/).expect("set-cookie", /JDMSessionId=;/).expect(302, /Redirecting to \/$/); }); + it("GET /", async function(ctx) { await request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); }); + it("GET /login", async function(ctx) { await request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/games$/); }); + describe("logout", function() { + beforeEach(async function(ctx) { + await request.get("/logout").set("cookie", ctx.cookie).expect("content-type", /text\/plain/).expect("set-cookie", /JDMSessionId=;/).expect(302, /Redirecting to \/$/); + }); describe.concurrent("all handlers with the now invalid cookie", async function() { - it("/", async function() { request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); - it("/", async function() { request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); }); - it("/", async function() { request.get("/logout").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); }); + it("GET /", async function(ctx) { await request.get("/").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/login$/); }); + it("GET /login", async function(ctx) { await request.get("/login").set("Cookie", ctx.cookie).expect("Content-Type", /text\/html/).expect(200, /<form action="\/login" method="post">/); }); + it("GET /logout", async function(ctx) { await request.get("/logout").set("Cookie", ctx.cookie).expect("Content-Type", /text\/plain/).expect(302, /Redirecting to \/$/); }); }); }); }); |