Skip to content

Commit 0f6ce3d

Browse files
committed
docs(svelte-testing-library): add FAQ entry for transition events
1 parent 6ae3bb1 commit 0f6ce3d

File tree

1 file changed

+66
-25
lines changed

1 file changed

+66
-25
lines changed

docs/svelte-testing-library/faq.mdx

Lines changed: 66 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,95 @@ sidebar_label: FAQ
55
---
66

77
- [Does this library also work with SvelteKit?](#does-this-library-also-work-with-sveltekit)
8-
- [`onMount` isn't called when rendering components](#onmount-isnt-called-when-rendering-compoments)
9-
- [Testing file upload component](#testing-file-upload-component)
8+
- [Why isn't `onMount` called when rendering components?](#why-isnt-onmount-called-when-rendering-components)
9+
- [How do I test file upload?](#how-do-i-test-file-upload)
10+
- [Why aren't transition events running?](#why-arent-transition-events-running)
1011

1112
---
1213

1314
## Does this library also work with SvelteKit?
1415

15-
Yes, it does. It requires the same [setup](setup.mdx) as a "normal" Svelte
16-
project.
16+
Yes, it does. It requires the same [setup][] as a "normal" Svelte project.
1717

18-
## `onMount` isn't called when rendering components
18+
## Why isn't `onMount` called when rendering components?
1919

2020
Since the test is running in a Node environment instead of a browser
21-
environment, it uses the SSR exports from Svelte, which declare
22-
all lifecycle events as no-ops.
21+
environment, it uses the SSR exports from Svelte, which declare all lifecycle
22+
events as no-ops.
2323

24-
One solution is to configure Vite to use browser resolutions in tests.
24+
One solution is to configure Vite to use browser resolutions in tests.
2525

2626
```js title="vite.config.js"
27+
import {svelte} from '@sveltejs/vite-plugin-svelte'
28+
import {defineConfig} from 'vite'
2729

28-
import { svelte } from '@sveltejs/vite-plugin-svelte';
29-
import { defineConfig } from 'vite';
30-
31-
export default defineConfig(({ mode }) => ({
30+
export default defineConfig(({mode}) => ({
3231
plugins: [svelte()],
3332
resolve: {
34-
// the default would be [ 'svelte', 'node' ]
33+
// the default would be [ 'svelte', 'node' ]
3534
// as set by vite-plugin-svelte and vitest
3635
conditions: mode === 'test' ? ['browser'] : [],
3736
},
3837
test: {
3938
environment: 'jsdom',
4039
},
41-
};
40+
}))
41+
```
42+
43+
See svelte-testing-library's [issue 222][] for more details.
44+
45+
[setup]: ./setup.mdx
46+
[issue 222]:
47+
https://github.com/testing-library/svelte-testing-library/issues/222
48+
49+
## How do I test file upload?
50+
51+
Use the [upload][] utility from `@testing-library/user-event` rather than
52+
`fireEvent`. It works well in both [jsdom][] and [happy-dom][].
53+
54+
```js
55+
test('upload file', async () => {
56+
const user = userEvent.setup()
4257

58+
render(Uploader)
59+
const file = new File(['hello'], 'hello.png', {type: 'image/png'})
60+
const input = screen.getByLabelText(/upload file/i)
61+
62+
await user.upload(input, file)
63+
64+
expect(input.files[0]).toBe(file)
65+
expect(input.files.item(0)).toBe(file)
66+
expect(input.files).toHaveLength(1)
67+
})
4368
```
4469

45-
See
46-
[svelte-testing-library's issue 222](https://github.com/testing-library/svelte-testing-library/issues/222)
47-
for more details.
70+
[upload]: https://testing-library.com/docs/user-event/utility#upload
71+
[jsdom]: https://github.com/jsdom/jsdom
72+
[happy-dom]: https://github.com/capricorn86/happy-dom
73+
74+
## Why aren't [transition events][] running?
75+
76+
The [jsdom][] `requestAnimationFrame` implementation can be unreliable in
77+
Vitest. To work around it, you can try a few things:
4878

49-
## Testing file upload component
79+
- Switch [happy-dom][], if you are able, which does not exhibit the same issues
80+
- Replace the implementation of `requestAnimationFrame`
81+
82+
```js
83+
beforeEach(() => {
84+
const raf = fn => setTimeout(() => fn(new Date()), 16)
85+
vi.stubGlobal('requestAnimationFrame', raf)
86+
})
87+
88+
// Alternatively, set `unstubGlobals: true` in vitest.config.js
89+
afterEach(() => {
90+
vi.unstubAllGlobals()
91+
})
92+
```
5093

51-
File upload handler not triggering? Use `happy-dom`, not `jsdom`, and make sure
52-
to use `fireEvent.change(...)` and not `fireEvent.input(...)`. It seems that
53-
jsdom (which is at v22.1.0 at the time of this writing) doesn't fake all the
54-
File object as it should.
94+
See svelte-testing-library's [issue 206][] for more details.
5595

56-
See
57-
[svelte-testing-library's issue 219](https://github.com/testing-library/svelte-testing-library/issues/219)
58-
for more details.
96+
[transition events]:
97+
https://svelte.dev/docs/element-directives#transition-events
98+
[issue 206]:
99+
https://github.com/testing-library/svelte-testing-library/issues/206

0 commit comments

Comments
 (0)