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

Add TypeScript Examples to vue-testing-library #1463

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions docs/vue-testing-library/examples.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,16 @@ id: examples
title: Example
---

import Tabs from '@theme/Tabs'
import TabItem from '@theme/TabItem'

## Basic example

<Tabs groupId="test-utils" defaultValue="js" values={[ {label: 'JavaScript',
value: 'js'}, {label: 'TypeScript', value: 'ts'}, ]}>

<TabItem value="js">

```html
<template>
<div>
Expand Down Expand Up @@ -50,8 +58,66 @@ test('increments value on click', async () => {
})
```

</TabItem>

<TabItem value="ts">

```html
<template>
<div>
<p>Times clicked: {{ count }}</p>
<button @click="increment">increment</button>
</div>
</template>

<script lang="ts">
export default {
data: (): {count: number} => ({
count: 0,
}),

methods: {
increment(): void {
this.count++
},
},
}
</script>
```

```ts
import {render, fireEvent, screen} from '@testing-library/vue'
import Component from './Component.vue'

test('increments value on click', async () => {
render(Component)

// screen has all queries that you can use in your tests.
// getByText returns the first matching node for the provided text, and
// throws an error if no elements match or if more than one match is found.
screen.getByText('Times clicked: 0')

const button = screen.getByText('increment')

// Dispatch a native click event to our button element.
await fireEvent.click(button)
await fireEvent.click(button)

screen.getByText('Times clicked: 2')
})
```

</TabItem>

</Tabs>

## Example using `v-model`:

<Tabs groupId="test-utils" defaultValue="js" values={[ {label: 'JavaScript',
value: 'js'}, {label: 'TypeScript', value: 'ts'}, ]}>

<TabItem value="js">

```html
<template>
<div>
Expand Down Expand Up @@ -92,6 +158,54 @@ test('properly handles v-model', async () => {
})
```

</TabItem>

<TabItem value="ts">

```html
<template>
<div>
<p>Hi, my name is {{ user }}</p>

<label for="username">Username:</label>
<input v-model="user" id="username" name="username" />
</div>
</template>

<script lang="ts">
export default {
data: (): {user: string} => ({
user: 'Alice',
}),
}
</script>
```

```ts
import {render, fireEvent, screen} from '@testing-library/vue'
import Component from './Component.vue'

test('properly handles v-model', async () => {
render(Component)

// Asserts initial state.
screen.getByText('Hi, my name is Alice')

// Get the input DOM node by querying the associated label.
const usernameInput = screen.getByLabelText(/username/i)

// Updates the <input> value and triggers an `input` event.
// fireEvent.input() would make the test fail.
await fireEvent.update(usernameInput, 'Bob')

screen.getByText('Hi, my name is Bob')
})
```

</TabItem>

</Tabs>

## More examples

You'll find examples of testing with different libraries in
Expand Down