Skip to content

Commit 652ff38

Browse files
feat: new tests and enhanced test-setup file
1 parent 54536ce commit 652ff38

File tree

18 files changed

+542
-3
lines changed

18 files changed

+542
-3
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Game } from '../Game'
2+
import { render, screen } from '@/test-setup'
3+
4+
describe('Game', () => {
5+
it('renders correctly', async () => {
6+
const initialState = {
7+
game: {
8+
lines: 6,
9+
linesPerMillisecond: 2,
10+
skills: {},
11+
},
12+
}
13+
14+
render(<Game />, { preloadedState: initialState })
15+
16+
expect(screen.getByText(/6 lines/)).toBeInTheDocument()
17+
expect(screen.getByText(/per second: 20/)).toBeInTheDocument()
18+
expect(screen.getByText(/Skills/)).toBeInTheDocument()
19+
expect(screen.getByText(/Store/)).toBeInTheDocument()
20+
21+
await screen.findByText(/8 lines/, undefined, { timeout: 150 })
22+
await screen.findByText(/10 lines/, undefined, { timeout: 150 })
23+
})
24+
})
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { render, screen, fireEvent } from '@/test-setup'
2+
import { Gitcoin } from '../Gitcoin'
3+
import { click } from '@/modules/game'
4+
5+
const mockDispatch = vi.fn()
6+
7+
vi.mock('react-redux', async () => ({
8+
useDispatch: () => mockDispatch,
9+
}))
10+
11+
describe('<Gitcoin />', () => {
12+
it('renders correctly', () => {
13+
render(<Gitcoin />)
14+
15+
expect(screen.getByRole('button')).toBeInTheDocument()
16+
expect(screen.getByAltText(/Gitcoin/i)).toBeInTheDocument()
17+
})
18+
19+
it('dispatches the click action when clicked', () => {
20+
render(<Gitcoin />)
21+
22+
fireEvent.click(screen.getByRole('button'))
23+
24+
expect(mockDispatch).toHaveBeenCalledTimes(1)
25+
expect(mockDispatch).toHaveBeenCalledWith(click())
26+
})
27+
})
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Score } from '../Score'
2+
import { render, screen } from '@/test-setup'
3+
4+
describe('Score', () => {
5+
it('should display the number of lines', () => {
6+
const initialState = {
7+
game: { lines: 6, linesPerMillisecond: 2, skills: {} },
8+
}
9+
10+
render(<Score />, { preloadedState: initialState })
11+
12+
expect(screen.getByText(/6 lines/i)).toBeInTheDocument()
13+
})
14+
})
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { render, screen } from '@testing-library/react'
2+
import { Section } from '../Section'
3+
4+
describe('Section', () => {
5+
it('displays the owned skills', () => {
6+
render(<Section itemName="Bash" number={3} />)
7+
8+
expect(screen.getByText('Bash')).toBeInTheDocument()
9+
expect(screen.getAllByAltText('Bash')).toHaveLength(3)
10+
})
11+
12+
it('render anything on unknown skill', () => {
13+
render(<Section itemName="Unknown" number={3} />)
14+
15+
expect(screen.queryByText('Unknown')).not.toBeInTheDocument()
16+
})
17+
})
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { render, screen } from '@/test-setup'
2+
import { Skills } from '../Skills'
3+
4+
describe('Skills', () => {
5+
it('renders correctly', () => {
6+
const initialState = {
7+
game: {
8+
lines: 6,
9+
linesPerMillisecond: 2,
10+
skills: { Bash: 2, Git: 3, Javascript: 4 },
11+
},
12+
}
13+
14+
render(<Skills />, { preloadedState: initialState })
15+
16+
expect(screen.getByText(/Bash/i)).toBeInTheDocument()
17+
expect(screen.getByText(/Git/i)).toBeInTheDocument()
18+
expect(screen.getByText(/Javascript/i)).toBeInTheDocument()
19+
})
20+
})
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import BashIcon from 'devicon/icons/bash/bash-original.svg'
2+
import { render, screen, fireEvent } from '@testing-library/react'
3+
import { Item } from '../Item'
4+
5+
describe('Item', () => {
6+
it('Renders a buyable item', () => {
7+
const item = {
8+
name: 'Bash',
9+
price: 10,
10+
linesPerMillisecond: 0.1,
11+
icon: BashIcon,
12+
}
13+
14+
const onBuy = vi.fn()
15+
16+
render(
17+
<Item
18+
item={item}
19+
lines={150}
20+
onBuy={onBuy}
21+
/>,
22+
)
23+
24+
expect(screen.getByText(/Bash/i)).toBeInTheDocument()
25+
expect(screen.getByText(/1 lines per second/i)).toBeInTheDocument()
26+
expect(screen.getByRole('button')).not.toBeDisabled()
27+
28+
fireEvent.click(screen.getByRole('button'))
29+
30+
expect(onBuy).toHaveBeenCalledWith(item)
31+
})
32+
33+
it('Renders a non buyable item', () => {
34+
const item = {
35+
name: 'Bash',
36+
price: 10,
37+
linesPerMillisecond: 0.1,
38+
icon: BashIcon,
39+
}
40+
41+
const onBuy = vi.fn()
42+
43+
render(
44+
<Item
45+
item={item}
46+
lines={0}
47+
onBuy={onBuy}
48+
/>,
49+
)
50+
51+
expect(screen.getByText(/Bash/i)).toBeInTheDocument()
52+
expect(screen.getByText(/1 lines per second/i)).toBeInTheDocument()
53+
expect(screen.getByRole('button')).toBeDisabled()
54+
55+
fireEvent.click(screen.getByRole('button'))
56+
57+
expect(onBuy).not.toHaveBeenCalledWith(item)
58+
})
59+
})
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { render, screen } from '@/test-setup'
2+
import { Store } from '../Store'
3+
4+
describe('Store', () => {
5+
it('renders correctly', () => {
6+
const initialState = {
7+
game: {
8+
lines: 6,
9+
linesPerMillisecond: 2,
10+
skills: {},
11+
},
12+
}
13+
14+
render(<Store />, { preloadedState: initialState })
15+
16+
expect(screen.getByText(/Bash/i)).toBeInTheDocument()
17+
expect(screen.getByText(/Git/i)).toBeInTheDocument()
18+
expect(screen.getByText(/Javascript/i)).toBeInTheDocument()
19+
expect(screen.getByText(/React/i)).toBeInTheDocument()
20+
expect(screen.getByText(/Vim/i)).toBeInTheDocument()
21+
})
22+
})
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { render } from '@/test-setup'
2+
import { Navbar } from '../Navbar'
3+
4+
describe('Navbar page', () => {
5+
it('renders correctly', () => {
6+
const { asFragment } = render(<Navbar />)
7+
8+
expect(asFragment()).toMatchSnapshot()
9+
})
10+
})
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`Navbar page > renders correctly 1`] = `
4+
<DocumentFragment>
5+
<header
6+
class="MuiPaper-root MuiPaper-elevation MuiPaper-elevation4 MuiAppBar-root MuiAppBar-colorPrimary MuiAppBar-positionRelative css-1lri64z-MuiPaper-root-MuiAppBar-root"
7+
style="--Paper-shadow: 0px 2px 4px -1px rgba(0,0,0,0.2),0px 4px 5px 0px rgba(0,0,0,0.14),0px 1px 10px 0px rgba(0,0,0,0.12);"
8+
>
9+
<div
10+
class="MuiToolbar-root MuiToolbar-gutters MuiToolbar-regular css-1ygil4i-MuiToolbar-root"
11+
>
12+
<a
13+
class="navbar-link"
14+
data-discover="true"
15+
href="/"
16+
>
17+
<svg
18+
aria-hidden="true"
19+
class="MuiSvgIcon-root MuiSvgIcon-fontSizeMedium css-1umw9bq-MuiSvgIcon-root"
20+
data-testid="GitHubIcon"
21+
focusable="false"
22+
viewBox="0 0 24 24"
23+
>
24+
<path
25+
d="M12 1.27a11 11 0 00-3.48 21.46c.55.09.73-.28.73-.55v-1.84c-3.03.64-3.67-1.46-3.67-1.46-.55-1.29-1.28-1.65-1.28-1.65-.92-.65.1-.65.1-.65 1.1 0 1.73 1.1 1.73 1.1.92 1.65 2.57 1.2 3.21.92a2 2 0 01.64-1.47c-2.47-.27-5.04-1.19-5.04-5.5 0-1.1.46-2.1 1.2-2.84a3.76 3.76 0 010-2.93s.91-.28 3.11 1.1c1.8-.49 3.7-.49 5.5 0 2.1-1.38 3.02-1.1 3.02-1.1a3.76 3.76 0 010 2.93c.83.74 1.2 1.74 1.2 2.94 0 4.21-2.57 5.13-5.04 5.4.45.37.82.92.82 2.02v3.03c0 .27.1.64.73.55A11 11 0 0012 1.27"
26+
/>
27+
</svg>
28+
<h6
29+
class="MuiTypography-root MuiTypography-h6 MuiTypography-noWrap css-1tl44qx-MuiTypography-root"
30+
>
31+
Gitclicker
32+
</h6>
33+
</a>
34+
</div>
35+
</header>
36+
</DocumentFragment>
37+
`;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { render } from '@/test-setup'
2+
import GitClicker from '../GitClicker'
3+
4+
describe('GitClicker page', () => {
5+
it('renders correctly', () => {
6+
const { asFragment } = render(<GitClicker />)
7+
8+
expect(asFragment()).toMatchSnapshot()
9+
})
10+
})

0 commit comments

Comments
 (0)