This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
/
reducer.main.spec.ts
85 lines (76 loc) · 2.81 KB
/
reducer.main.spec.ts
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { Dungeon } from '../world/dungeon';
import { Thing } from '../world/thing';
import { Room } from '../world/room';
import { Directions } from '../world/directions';
import { IInventoryAction, IRoomAction, IWonAction } from '../actions/createAction';
import { ACTION_TEXT, ACTION_MOVE, ACTION_GET, ACTION_WON } from '../actions/ActionList';
import { ITextAction } from '../actions/createAction';
import { mainReducer } from './reducer.main';
import { freezeRoom } from './freeze.room.spec';
describe('main', () => {
let dungeon: Dungeon = null,
room1: Room = null,
room2: Room = null,
thing1: Thing = null;
beforeEach(() => {
dungeon = new Dungeon();
room1 = new Room();
room2 = new Room();
thing1 = new Thing();
thing1.name = 'Magic test thing';
room1.name = 'Room 1';
room2.name = 'Room 2';
room2.things.push(thing1);
room1.setDirection(Directions.East, room2);
room2.setDirection(Directions.West, room1);
dungeon.rooms.push(room1);
dungeon.rooms.push(room2);
dungeon.currentRoomIdx = 0;
dungeon.trophyCount = 2;
Room.setIds(dungeon.rooms);
Object.freeze(thing1);
freezeRoom(room1);
freezeRoom(room2);
Object.freeze(dungeon.console);
Object.freeze(dungeon.inventory);
Object.freeze(dungeon.rooms);
Object.freeze(dungeon);
});
it('should update the console for a text action', () => {
let newState = mainReducer(dungeon, {
type: ACTION_TEXT,
text: 'test'
} as ITextAction);
expect(newState.rooms).toEqual(dungeon.rooms);
expect(newState.console.length).toBe(1);
expect(newState.console[0]).toEqual('test');
});
it('should update the current room on a move', () => {
let newState = mainReducer(dungeon, {
type: ACTION_MOVE,
direction: Directions.East,
newRoom: room2
} as IRoomAction);
expect(newState.currentRoom.idx).toEqual(room2.idx);
expect(newState.currentRoom.visited).toBe(true);
});
it('should transfer inventory on a get', () => {
let newState = mainReducer(dungeon, {
type: ACTION_GET,
item: thing1,
room: room2
} as IInventoryAction);
expect(newState.inventory).toEqual([thing1]);
expect(newState.rooms[1].things).toEqual([]);
});
it('should transfer inventory and set the won flag on a win', () => {
let newState = mainReducer(dungeon, {
type: ACTION_WON,
item: thing1,
room: room2
} as IWonAction);
expect(newState.inventory).toEqual([thing1]);
expect(newState.rooms[1].things).toEqual([]);
expect(newState.won).toBe(true);
});
});