-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests covering basic functionality (#2)
* test: added basic tests for component utilizing context * test: refactor component test, remove separate mocks file * refactor: move tests directory in src
- Loading branch information
1 parent
125b869
commit 72d3cde
Showing
5 changed files
with
107 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[test] | ||
preload = "./happydom.ts" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { GlobalRegistrator } from '@happy-dom/global-registrator'; | ||
|
||
GlobalRegistrator.register(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { afterEach, expect, test } from 'bun:test'; | ||
import { cleanup, render, screen } from '@testing-library/react'; | ||
import { userEvent } from '@testing-library/user-event'; | ||
import { create } from 'zustand'; | ||
import { createZustandContext } from '../context'; | ||
|
||
type Cat = string; | ||
|
||
type CatStore = { | ||
cats: Cat[]; | ||
addCat: (cat: Cat) => void; | ||
removeCat: (cat: Cat) => void; | ||
}; | ||
|
||
const allCats = ['Salem', 'Mimo', 'Tapi']; | ||
const newCat = 'Millie'; | ||
|
||
const [CatsProvider, useCatsStore] = createZustandContext((initialState: { cats: Cat[] }) => | ||
create<CatStore>((set) => ({ | ||
cats: initialState.cats, | ||
addCat: (cat) => set((state) => ({ cats: [...state.cats, cat] })), | ||
removeCat: (cat) => set((state) => ({ cats: state.cats.filter((c) => c !== cat) })), | ||
})), | ||
); | ||
|
||
const CatsList = () => { | ||
const cats = useCatsStore((state) => state.cats); | ||
const addCat = useCatsStore((state) => state.addCat); | ||
const removeCat = useCatsStore((state) => state.removeCat); | ||
|
||
return ( | ||
<div> | ||
<ul> | ||
{cats.map((cat) => ( | ||
<li key={cat}>{cat}</li> | ||
))} | ||
</ul> | ||
<button type="button" onClick={() => addCat(newCat)}> | ||
Add new cat | ||
</button> | ||
<button type="button" onClick={() => removeCat(newCat)}> | ||
Remove new cat | ||
</button> | ||
</div> | ||
); | ||
}; | ||
|
||
afterEach(cleanup); | ||
|
||
test('should initialize state correctly', () => { | ||
render( | ||
<CatsProvider initialValue={{ cats: allCats }}> | ||
<CatsList /> | ||
</CatsProvider>, | ||
); | ||
|
||
for (const cat of allCats) { | ||
expect(screen.getByText(cat)).toBeTruthy(); | ||
} | ||
}); | ||
|
||
test('should add new cat', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<CatsProvider initialValue={{ cats: allCats }}> | ||
<CatsList /> | ||
</CatsProvider>, | ||
); | ||
|
||
await user.click(screen.getByRole('button', { name: 'Add new cat' })); | ||
|
||
for (const cat of allCats) { | ||
expect(screen.getByText(cat)).toBeTruthy(); | ||
} | ||
|
||
expect(screen.getByText(newCat)).toBeTruthy(); | ||
}); | ||
|
||
test('should remove added cat', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( | ||
<CatsProvider initialValue={{ cats: allCats }}> | ||
<CatsList /> | ||
</CatsProvider>, | ||
); | ||
|
||
await user.click(screen.getByRole('button', { name: 'Remove new cat' })); | ||
|
||
for (const cat of allCats) { | ||
expect(screen.getByText(cat)).toBeTruthy(); | ||
} | ||
|
||
expect(screen.queryByText(newCat)).toBeFalsy(); | ||
}); |