From 63f092eafedb908d3d2616718a9cdee83afa5f90 Mon Sep 17 00:00:00 2001 From: Meghan Date: Sat, 18 May 2024 10:58:49 -0700 Subject: [PATCH] Fix npm tests --- wordle-web/package-lock.json | 43 +++++++++++++++++++++++++++++++++++ wordle-web/package.json | 1 + wordle-web/tests/game.test.ts | 27 +++++++++++++++++++--- 3 files changed, 68 insertions(+), 3 deletions(-) diff --git a/wordle-web/package-lock.json b/wordle-web/package-lock.json index bda64c29..4b2de1f0 100644 --- a/wordle-web/package-lock.json +++ b/wordle-web/package-lock.json @@ -17,6 +17,7 @@ "devDependencies": { "@nuxt/test-utils": "^3.12.0", "@vue/test-utils": "^2.4.5", + "axios-mock-adapter": "^1.22.0", "happy-dom": "^13.10.1", "playwright-core": "^1.43.0", "vite-plugin-vuetify": "^2.0.3", @@ -4304,6 +4305,19 @@ "proxy-from-env": "^1.1.0" } }, + "node_modules/axios-mock-adapter": { + "version": "1.22.0", + "resolved": "https://registry.npmjs.org/axios-mock-adapter/-/axios-mock-adapter-1.22.0.tgz", + "integrity": "sha512-dmI0KbkyAhntUR05YY96qg2H6gg0XMl2+qTW0xmYg6Up+BFBAJYRLROMXRdDEL06/Wqwa0TJThAYvFtSFdRCZw==", + "dev": true, + "dependencies": { + "fast-deep-equal": "^3.1.3", + "is-buffer": "^2.0.5" + }, + "peerDependencies": { + "axios": ">= 0.17.0" + } + }, "node_modules/b4a": { "version": "1.6.6", "resolved": "https://registry.npmjs.org/b4a/-/b4a-1.6.6.tgz", @@ -5733,6 +5747,12 @@ "node": ">=18" } }, + "node_modules/fast-deep-equal": { + "version": "3.1.3", + "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", + "integrity": "sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==", + "dev": true + }, "node_modules/fast-fifo": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/fast-fifo/-/fast-fifo-1.3.2.tgz", @@ -6495,6 +6515,29 @@ "node": ">=8" } }, + "node_modules/is-buffer": { + "version": "2.0.5", + "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-2.0.5.tgz", + "integrity": "sha512-i2R6zNFDwgEHJyQUtJEk0XFi1i0dPFn/oqjK3/vPCcDeJvW5NQ83V8QbicfF1SupOaB0h8ntgBC2YiE7dfyctQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "engines": { + "node": ">=4" + } + }, "node_modules/is-builtin-module": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/is-builtin-module/-/is-builtin-module-3.2.1.tgz", diff --git a/wordle-web/package.json b/wordle-web/package.json index 8ebcd2d9..882e972f 100644 --- a/wordle-web/package.json +++ b/wordle-web/package.json @@ -21,6 +21,7 @@ "devDependencies": { "@nuxt/test-utils": "^3.12.0", "@vue/test-utils": "^2.4.5", + "axios-mock-adapter": "^1.22.0", "happy-dom": "^13.10.1", "playwright-core": "^1.43.0", "vite-plugin-vuetify": "^2.0.3", diff --git a/wordle-web/tests/game.test.ts b/wordle-web/tests/game.test.ts index 2938c9c9..058ca2ca 100644 --- a/wordle-web/tests/game.test.ts +++ b/wordle-web/tests/game.test.ts @@ -1,7 +1,16 @@ // @vitest-environment nuxt -import { expect, test } from "vitest"; +import { beforeEach, expect, test } from "vitest"; import { Game } from "~/scripts/game"; import { LetterState } from "~/scripts/letter"; +import AxiosMockAdapter from "axios-mock-adapter"; +import axios from "axios"; +import { GameStats } from "~/scripts/gameStats"; + +let mock: AxiosMockAdapter; + +beforeEach(() => { + mock = new AxiosMockAdapter(axios); +}); test("game", () => { // create game and check if it's created @@ -12,7 +21,19 @@ test("game", () => { test("guess-word", () => { const game = new Game(); - game.startNewGame("autos"); + + const word = "autos"; + + const mockGameStats = new GameStats(); + mockGameStats.word = "autos"; + mockGameStats.averageGuesses = 0; + mockGameStats.totalTimesPlayed = 0; + mockGameStats.totalWins = 0; + mockGameStats.totalLosses = 0; + + mock.onPost().reply(200, mockGameStats); + + game.startNewGame(word); game.setGuessLetters("tangs"); game.submitGuess(); expect(game.guesses[0].letters[0].state).toBe(LetterState.Misplaced); @@ -21,7 +42,7 @@ test("guess-word", () => { expect(game.guesses[0].letters[3].state).toBe(LetterState.Wrong); expect(game.guesses[0].letters[4].state).toBe(LetterState.Correct); - game.setGuessLetters("autos"); + game.setGuessLetters(word); game.submitGuess(); expect(game.guesses[1].letters[0].state).toBe(LetterState.Correct); expect(game.guesses[1].letters[1].state).toBe(LetterState.Correct);