Skip to content
This repository was archived by the owner on Sep 20, 2024. It is now read-only.

Commit de85a27

Browse files
committed
docs(testing): add docs for testing
1 parent 6fe09d7 commit de85a27

File tree

2 files changed

+91
-16
lines changed

2 files changed

+91
-16
lines changed

CONTRIBUTING.md

Lines changed: 90 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,36 +19,43 @@ cd chakra-ui
1919
```
2020

2121
3. Install dependencies and bootstrap the project
22+
2223
```sh
2324
pnpm install
2425
```
2526

2627
4. Start development server for packages
28+
2729
```sh
2830
pnpm dev
2931
```
3032

3133
5. Open component playground
34+
3235
```sh
3336
pnpm playground:dev
3437
```
3538

36-
> If you run into any issues during these steps, kindly reach out to the Chakra UI
37-
> Vue team here:[https://discord.gg/cMpMfvxa](https://discord.gg/cMpMfvxa)
39+
> If you run into any issues during these steps, kindly reach out to the Chakra
40+
> UI Vue team here:[https://discord.gg/cMpMfvxa](https://discord.gg/cMpMfvxa)
3841
3942
## For Windows OS Users
40-
There may be some trouble specific to the local setup in Windows. The following are suggestions in ensuring the local environment boots up successfully:
4143

42-
- The package dependencies and scripts should work with Node `v16.16.0 and higher`
44+
There may be some trouble specific to the local setup in Windows. The following
45+
are suggestions in ensuring the local environment boots up successfully:
4346

47+
- The package dependencies and scripts should work with Node
48+
`v16.16.0 and higher`
4449

4550
## Development
4651

4752
To improve our development process, we've set up tooling and systems. Chakra UI
4853
uses a monorepo structure and we treat each component as an independent package
4954
that can be consumed in isolation.
5055

51-
If you are looking to build a new component, and it has been approved by the team, head over to the [components-guide.md](./docs/guides/component-guide.md) to help you get started!
56+
If you are looking to build a new component, and it has been approved by the
57+
team, head over to the [components-guide.md](./docs/guides/component-guide.md)
58+
to help you get started!
5259

5360
### Tooling
5461

@@ -66,7 +73,8 @@ If you are looking to build a new component, and it has been approved by the tea
6673
**`pnpm install`**: bootstraps the entire project, symlinks all dependencies for
6774
cross-component development and builds all components.
6875

69-
**`pnpm playground:dev`**: starts components playground server and loads stories in SFCs in the `packages/**/examples/*.vue` file.
76+
**`pnpm playground:dev`**: starts components playground server and loads stories
77+
in SFCs in the `packages/**/examples/*.vue` file.
7078

7179
**`pnpm docs:dev`**: run the documentation site locally.
7280

@@ -87,8 +95,9 @@ to run commands within component packages directly from the root.
8795
Each component is named this way: `@chakra-ui/[component]`. Let's say we want to
8896
build the button component. Here's how to do it:
8997

90-
> *Take note that in order to prevent template tags name clashing with HTML elements or other Vue library components,*
91-
> *we prefix all component names with a `c-` in kebab-case or a capital `C` in PascalCase.*
98+
> _Take note that in order to prevent template tags name clashing with HTML
99+
> elements or other Vue library components,_ > _we prefix all component names
100+
> with a `c-` in kebab-case or a capital `C` in PascalCase._
92101
93102
```bash
94103
pnpm workspace @chakra-ui/c-button build
@@ -99,6 +108,7 @@ lerna run build --scope @chakra-ui/c-button
99108
```
100109

101110
**Shortcut:**
111+
102112
```bash
103113
# to build
104114
pnpm pkg @chakra-ui/c-tabs build
@@ -114,14 +124,15 @@ and want to avoid running the command for all components.
114124

115125
### Documentation
116126

117-
The documentation site is built with Vite.js on SSR. If you'd like to contribute to the
118-
docs, simply run `pnpm build`, and `pnpm docs:dev`
127+
The documentation site is built with Vite.js on SSR. If you'd like to contribute
128+
to the docs, simply run `pnpm build`, and `pnpm docs:dev`
119129

120130
### Components Development Playground
121131

122132
Build components in isolation with Vite using `pnpm playground:dev`
123133

124-
Run `pnpm start` in a separate terminal first so the packages are built and a watcher set up for changes.
134+
Run `pnpm start` in a separate terminal first so the packages are built and a
135+
watcher set up for changes.
125136

126137
## Think you found a bug?
127138

@@ -131,8 +142,8 @@ link.
131142

132143
You may wish to use our starters to help you get going:
133144

134-
TODO: Add Typescript starter for `@chakra-ui/vue` v1
135-
TODO: Add Javascript starter for `@chakra-ui/vue` v1
145+
TODO: Add Typescript starter for `@chakra-ui/vue` v1 TODO: Add Javascript
146+
starter for `@chakra-ui/vue` v1
136147

137148
## Proposing new or changed API?
138149

@@ -200,9 +211,73 @@ https://www.conventionalcommits.org/ or check out the
200211
> `pnpm changeset add --empty` to generate an empty changeset file to document
201212
> your changes.
202213
203-
### Tests
214+
## Writing Tests
215+
216+
We recommend all new code that is added to the codebase or any refactors that
217+
may modify the behaviour of a given component or module are accompanied with
218+
some simple tests. This ensures that the behaviour of the component is protected
219+
against any accidental regressions in the future, or ensures that the new
220+
refactor does not change the behaviour of a given component.
221+
222+
At the time of writing this this document, unit tests authored in the Chakra UI
223+
Vue codebase are categorized into two types:
224+
225+
1. Component Tests (Behavioural Focused)
226+
2. Black Box Tests (Input-Output Focused)
227+
228+
### Component Tests
204229

205-
All commits that fix bugs or add features need a test.
230+
Authoring component tests typically is aimed at ensuring the behaviour of a
231+
given component based on its public API. Depending on the nature of the
232+
component you are testing, you may want to use either
233+
[Cypress component testing]() which excels this kind of behavioural testing of
234+
with [Jest](), which is mostly used for black-box testing.
235+
236+
#### Testing with Cypress
237+
238+
1. Create your `<COMPONENT>.cy.tsx` file in the `packages/<COMPONENT>/tests/`
239+
directory; where `<COMPONENT>` is the name of the component you are testing.
240+
2. Add the Cypress type references at the top of the file as shown in the
241+
example below. This will allow typescript to correctly reference the Cypress
242+
globals.
243+
244+
```tsx
245+
/// <reference types="../../../@types/cypress" />
246+
247+
import { MyComponent } from "../src/my-component"
248+
249+
it("<MyComponent /> is accessible", () => {
250+
cy.mount(() => <MyComponent>Hello World</MyComponent>)
251+
cy.checkA11y()
252+
})
253+
```
254+
255+
#### Testing with Jest
256+
257+
Create your `<COMPONENT>.test.tsx` file in the `packages/<COMPONENT>/tests/`
258+
directory; where `<COMPONENT>` is the name of the component you are testing.
259+
260+
```tsx
261+
import { MyComponent } from "../src"
262+
import {
263+
render,
264+
userEvent,
265+
testA11y,
266+
TestRenderProps,
267+
} from "@chakra-ui/vue-test-utils"
268+
269+
const renderComponent = (options: Component = {}) =>
270+
render({
271+
components: { MyComponent },
272+
template: `<MyComponent>Hello World</MyComponent>`,
273+
...options,
274+
})
275+
276+
it("should render properly", () => {
277+
const { asFragment } = renderComponent()
278+
expect(asFragment()).toMatchSnapshot()
279+
})
280+
```
206281

207282
> **Dear Chakra UI Vue team:** Please do not merge code without tests
208283

packages/c-close-button/tests/c-close-button.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
userEvent,
55
testA11y,
66
TestRenderProps,
7-
} from "../../test-utils/src"
7+
} from "@chakra-ui/vue-test-utils"
88
import { nextTick } from "@vue/runtime-core"
99

1010
const renderComponent = ({

0 commit comments

Comments
 (0)