Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add tests covering basic functionality #2

Merged
merged 3 commits into from
May 22, 2024

Conversation

grigorischristainas
Copy link
Contributor

Hey, I came across this project recently and thought that we could add some tests covering the basic functionality. This was the first time using Bun, so please let me know if any further adjustments are needed.

@grigorischristainas
Copy link
Contributor Author

@fredericoo did you have the chance to take a look?

tests/mocks.ts Outdated
Comment on lines 1 to 2
export const allCats = ['Salem', 'Mimo', 'Tapi'];
export const newCat = 'Millie';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: would just move this to the test itself, theres no need for a whole file with two variables

package.json Outdated
@@ -4,8 +4,8 @@
"description": "Create react contexts with zustand",
"scripts": {
"build": "tsup --dts --dts-resolve",
"test": "bun test src",
"check": "bunx @biomejs/biome check --apply ./src",
"test": "bun test src tests",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just move tests into src and remove those? I’d normally recommend we keep tests next to the code so we don’t feel disconnected from the tests!

Comment on lines 46 to 51
const renderApp = () =>
render(
<CatsProvider initialValue={{ cats: allCats }}>
<CatsList />
</CatsProvider>,
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would opt for repetition instead of this; it’s just indirection that saves us nothing and makes tests harder to grok

@fredericoo fredericoo changed the title Test: add tests covering basic functionality test: add tests covering basic functionality May 22, 2024

renderApp();

await user.click(screen.getByRole('button', { name: 'Add new cat' }));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing as we’re only calling one event per test, we can just use userEvent.click instead of setting it up as user.

Are there any advantages to setting up userEvent first?

Suggested change
await user.click(screen.getByRole('button', { name: 'Add new cat' }));
await userEvent.click(screen.getByRole('button', { name: 'Add new cat' }));

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback, I used it this way because it is recommended in the docs:
https://testing-library.com/docs/user-event/intro/#writing-tests-with-userevent

You can have a look at the last paragraph of this section, in which it is recommended to use the methods on the instances returned by userEvent.setup(), instead of directly invoking APIs such as userEvent.click().

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good to know it's a testing library v14 change that's just supported as they migrate away from it. I've just learnt a new thing and now wanna adjust all my unit tests at work 🤣

@fredericoo
Copy link
Owner

thanks for adding tests! they’re pretty good and cover getting and setting ❤

@grigorischristainas
Copy link
Contributor Author

Thank you for your suggestions! I have implemented the changes you recommended, except for the userEvent. I replied to your comment regarding that, please let me know if you agree or if you would like me to integrate this change as well.

Copy link
Owner

@fredericoo fredericoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@fredericoo fredericoo merged commit 72d3cde into fredericoo:main May 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants