Skip to content

Commit

Permalink
test: add tests covering basic functionality (#2)
Browse files Browse the repository at this point in the history
* 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
grigorischristainas authored May 22, 2024
1 parent 125b869 commit 72d3cde
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 3 deletions.
Binary file modified bun.lockb
Binary file not shown.
2 changes: 2 additions & 0 deletions bunfig.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[test]
preload = "./happydom.ts"
3 changes: 3 additions & 0 deletions happydom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GlobalRegistrator } from '@happy-dom/global-registrator';

GlobalRegistrator.register();
9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,17 @@
"devDependencies": {
"@biomejs/biome": "1.7.1",
"@changesets/cli": "^2.27.1",
"@happy-dom/global-registrator": "^14.10.1",
"@testing-library/react": "^15.0.7",
"@testing-library/user-event": "^14.5.2",
"@types/bun": "^1.1.0",
"@types/lodash": "^4.17.0",
"@types/react": "^18.2.20",
"react": "^18.2.0",
"tsup": "^8.0.2",
"type-testing": "^0.2.0",
"typescript": "^5.4.5",
"zustand": "^4.5.2",
"@types/react": "^18.2.20",
"react": "^18.2.0"
"zustand": "^4.5.2"
},
"peerDependencies": {
"zustand": ">= 4",
Expand Down
96 changes: 96 additions & 0 deletions src/tests/component.test.tsx
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();
});

0 comments on commit 72d3cde

Please sign in to comment.