Skip to content

Commit

Permalink
➖ Remove chai-as-promised
Browse files Browse the repository at this point in the history
  • Loading branch information
coyotte508 committed Nov 26, 2023
1 parent 3b8af05 commit f38d431
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 262 deletions.
7 changes: 2 additions & 5 deletions apps/api/app/models/gamenotification.spec.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import { expect } from "chai";
import mongoose, { Types } from "mongoose";
import { defaultKarma, Game, GameNotification, GamePreferences, maxKarma, User } from "./index";

const { ObjectId } = Types;

chai.use(chaiAsPromised);

describe("GameNotification", () => {
const userId = new ObjectId();
const userId2 = new ObjectId();
Expand Down Expand Up @@ -159,7 +156,7 @@ describe("GameNotification", () => {

await GameNotification.create({ kind: "gameEnded", game: "testCancelled" });

await expect(GameNotification.processGameEnded()).to.eventually.be.fulfilled;
await GameNotification.processGameEnded();
});
});
});
Expand Down
107 changes: 54 additions & 53 deletions apps/api/app/routes/game/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import axios, { AxiosError, AxiosRequestConfig } from "axios";
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";
import { expect } from "chai";
import mongoose, { Types } from "mongoose";
import env from "../../config/env";
import { Game, GameInfo, GamePreferences, JwtRefreshToken, User } from "../../models";

chai.use(chaiAsPromised);

describe("Game API", () => {
const userId = new Types.ObjectId();
let axiosConfig: AxiosRequestConfig = {};
let headers: Record<string, string>;

before(async () => {
await User.create({
Expand All @@ -35,33 +31,34 @@ describe("Game API", () => {
});
const refresh = await JwtRefreshToken.create({ user: userId });
const token = await refresh.createAccessToken(["all"], false);
axiosConfig = {
headers: {
Authorization: `Bearer ${token}`,
},
baseURL: `http://localhost:${env.listen.port.api}`,
};
headers = {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
}
});

it("should not be able to create a game without ownership", async () => {
const createP = axios.post(
"/api/game/new-game",
const resp = await fetch(
`http://localhost:${env.listen.port.api}/api/game/new-game`,
{
gameId: "test",
game: { game: "test", version: 1 },
timePerMove: 5000,
timePerGame: 5000,
players: 2,
options: { join: true },
},
axiosConfig
body:
JSON.stringify({
gameId: "test",
game: { game: "test", version: 1 },
timePerMove: 5000,
timePerGame: 5000,
players: 2,
options: { join: true }
})

,
headers,
method: "POST",
}
);

const err: AxiosError = await createP.then(
(err) => Promise.reject(err),
(err) => err
);
expect(err.response.data?.message.includes("own the game")).to.be.true;
expect(resp.ok).to.be.false;
expect((await resp.json()).message.includes("own the game")).to.be.true;
});

it("should be able to create a game with ownership", async () => {
Expand All @@ -73,41 +70,45 @@ describe("Game API", () => {
},
});

const createP = axios.post(
"/api/game/new-game",
const resp = await fetch(
`http://localhost:${env.listen.port.api}/api/game/new-game`,
{
gameId: "test",
body: JSON.stringify({
gameId: "test",
game: { game: "test", version: 1 },
timePerMove: 5000,
timePerGame: 5000,
players: 2,
options: { join: true },
},
axiosConfig
options: { join: true }
}),
method: "POST",
headers
}
);

await expect(createP).to.be.fulfilled;
expect(resp.ok).to.be.true;
});

it("should not be able to create a game with the wrong number of players", async () => {
const createP = axios.post(
"/api/game/new-game",
const resp =await fetch(
`http://localhost:${env.listen.port.api}/api/game/new-game`,
{
gameId: "test-fail",
game: { game: "test", version: 1 },
timePerMove: 5000,
timePerGame: 5000,
players: 3,
options: { join: true },
},
axiosConfig
);

const err: AxiosError = await createP.then(
(err) => Promise.reject(err),
(err) => err
);
expect(err.response.data?.message).to.equal("Wrong number of players");
body: JSON.stringify({
gameId: "test-fail",
game: { game: "test", version: 1 },
timePerMove: 5000,
timePerGame: 5000,
players: 3,
options: { join: true },
}),
method: "POST",
headers
});

expect(resp.ok).to.be.false;

const err = await resp.json();
expect(err.message).to.equal("Wrong number of players");
});

// it ("should not be able to create a game without the join option", async () => {
Expand All @@ -126,9 +127,9 @@ describe("Game API", () => {
// });

it("should be able to leave the game", async () => {
const unjoinP = axios.post("/api/game/test/unjoin", {}, axiosConfig);
const resp = await fetch(`http://localhost:${env.listen.port.api}/api/game/test/unjoin`, {headers, method: "POST"});

await expect(unjoinP).to.be.fulfilled;
expect(resp.ok).to.be.true;

expect(await Game.countDocuments({ _id: "test" })).to.equal(0, "Game should be deleted after creator unjoins");
});
Expand Down
6 changes: 1 addition & 5 deletions apps/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,14 @@
"@types/ws": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^4.29.3",
"@typescript-eslint/parser": "^4.29.3",
"axios": "^0.21.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
"cross-env": "^7.0.3",
"eslint": "^7.32.0",
"koa-cookie": "^1.0.0",
"luxon": "^1.23.0",
"mocha": "^7.1.1",
"ts-node": "^10.4.0",
"ts-node-dev": "^1.1.8",
"tsconfig-paths": "^3.12.0",
"typescript": "^4.5.5"
"tsconfig-paths": "^3.12.0"
},
"scripts": {
"start": "ts-node -T -r tsconfig-paths/register server.ts || npm start",
Expand Down
39 changes: 0 additions & 39 deletions apps/api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f38d431

Please sign in to comment.