generated from DataStax-Examples/battlestax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameSlice.test.js
59 lines (53 loc) · 1.84 KB
/
gameSlice.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { generateGameId } from "../util/random";
import fetchMock from "fetch-mock";
import store from "./index";
import reducer, {
initialState,
setId,
setIdLoading,
setIdError,
selectGame,
createGame,
} from "./gameSlice";
describe("game slice", () => {
afterEach(() => {
fetchMock.restore();
});
it("should return the initial state on first run", () => {
const nextState = initialState;
const result = reducer(undefined, {});
expect(result).toEqual(nextState);
});
it("should set a game id", () => {
const gameId = generateGameId();
const nextState = reducer(initialState, setId(gameId));
const rootState = { game: nextState };
expect(selectGame(rootState).id).toEqual(gameId);
});
it("should set the game id loading flag", () => {
const gameId = generateGameId();
const nextState = reducer(initialState, setIdLoading(true));
const rootState = { game: nextState };
expect(selectGame(rootState).idLoading).toEqual(true);
});
it("should set the game id error", () => {
const nextState = reducer(initialState, setIdError("nope"));
const rootState = { game: nextState };
expect(selectGame(rootState).idError).toEqual("nope");
});
it("should create a new game", async () => {
fetchMock.postOnce("*", {
body: { documentId: "DANG" },
});
store.dispatch(createGame());
const initialState = store.getState();
expect(selectGame(initialState).idError).toEqual("");
expect(selectGame(initialState).idLoading).toEqual(true);
expect(selectGame(initialState).id).toEqual("");
await new Promise((resolve) => setTimeout(resolve, 500));
const finalState = store.getState();
expect(selectGame(finalState).idError).toEqual("");
expect(selectGame(finalState).idLoading).toEqual(false);
expect(selectGame(finalState).id).toEqual("DANG");
});
});