-
Notifications
You must be signed in to change notification settings - Fork 0
/
tug-o-war.test.tsx
163 lines (143 loc) · 4.35 KB
/
tug-o-war.test.tsx
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { ws } from 'msw'
import { render, screen, waitFor } from '@testing-library/react'
import { userEvent } from '@testing-library/user-event'
import { server } from '~/mocks/node.ts'
import { TugOWar } from './tug-o-war.tsx'
import { ClientMessageType, GameState, ServerMessageType } from '~/messages.ts'
const game = ws.link(`ws://*`)
it('starts in a waiting mode', async () => {
render(
<TugOWar
initialGameState={GameState.IDLE}
initialScore={0}
initialTimeElapsed={0}
/>,
)
const pullRightButton = screen.getByRole('button', { name: /pull right/i })
const pullLeftButton = screen.getByRole('button', { name: /pull left/i })
// The pull buttons are disabled until the game starts.
expect(pullRightButton).toBeDisabled()
expect(pullLeftButton).toBeDisabled()
})
it('enables pulling buttons once the server starts the game', async () => {
server.use(
game.on('connection', ({ client }) => {
client.send(
JSON.stringify({
type: 'game/state-change',
payload: {
nextState: GameState.PLAYING,
},
} satisfies ServerMessageType),
)
}),
)
render(
<TugOWar
initialGameState={GameState.IDLE}
initialScore={0}
initialTimeElapsed={0}
/>,
)
const pullRightButton = screen.getByRole('button', { name: /pull right/i })
const pullLeftButton = screen.getByRole('button', { name: /pull left/i })
// Once the server starts the game, the pull buttons are enabled.
await waitFor(() => {
expect(pullRightButton).toBeEnabled()
expect(pullLeftButton).toBeEnabled()
})
})
it('emits the "pull" event when pulling the rope to the right', async () => {
const clientMessageListener = vi.fn()
server.use(
game.on('connection', ({ client, server }) => {
client.addEventListener('message', (event) => {
clientMessageListener(JSON.parse(event.data.toString()))
})
}),
)
render(
<TugOWar
initialGameState={GameState.PLAYING}
initialScore={0}
initialTimeElapsed={0}
/>,
)
const pullRightButton = screen.getByRole('button', { name: /pull right/i })
await userEvent.click(pullRightButton)
// Clicking on the pull button emits the event to the server.
await waitFor(() => {
expect(clientMessageListener).toHaveBeenLastCalledWith({
type: 'pull',
payload: {
team: 'team-right',
},
} satisfies ClientMessageType)
})
})
it('emits the "pull" event when pulling the rope to the left', async () => {
const clientMessageListener = vi.fn()
server.use(
game.on('connection', ({ client }) => {
client.addEventListener('message', (event) => {
clientMessageListener(JSON.parse(event.data.toString()))
})
}),
)
render(
<TugOWar
initialGameState={GameState.PLAYING}
initialScore={0}
initialTimeElapsed={0}
/>,
)
const pullLeftButton = screen.getByRole('button', { name: /pull left/i })
await userEvent.click(pullLeftButton)
// Clicking on the pull button emits the event to the server.
await waitFor(() => {
expect(clientMessageListener).toHaveBeenLastCalledWith({
type: 'pull',
payload: {
team: 'team-left',
},
} satisfies ClientMessageType)
})
})
it('shows the winning screen once one side wins', async () => {
server.use(
game.on('connection', ({ client }) => {
// Send the game state change from the server on the
// next tick to isolate the component's behavior.
queueMicrotask(() => {
client.send(
JSON.stringify({
type: 'game/state-change',
payload: {
nextState: GameState.END,
winningTeam: 'team-right',
},
} satisfies ServerMessageType),
)
})
}),
)
render(
<TugOWar
initialGameState={GameState.PLAYING}
initialScore={0}
initialTimeElapsed={0}
/>,
)
// Wait for the UI to show the winning message.
expect(
await screen.findByRole('heading', { name: /That's the game!/i }),
).toBeVisible()
expect(screen.getByText(/"SPACES" has won/i)).toBeVisible()
// Pulling buttons are not present on the winning screen.
expect(
screen.queryByRole('button', { name: /pull right/i }),
).not.toBeInTheDocument()
expect(
screen.queryByRole('button', { name: /pull left/i }),
).not.toBeInTheDocument()
})