Skip to content

Commit

Permalink
test: add onMount test and replace onDestory test
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous committed Jan 29, 2024
1 parent 0524dcf commit d59d56f
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 77 deletions.
11 changes: 11 additions & 0 deletions src/__tests__/fixtures/Mounter.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<script>
import { onMount, onDestroy } from 'svelte'

Check failure on line 2 in src/__tests__/fixtures/Mounter.svelte

View workflow job for this annotation

GitHub Actions / Node 16, Svelte 3

Run autofix to sort these imports!

Check failure on line 2 in src/__tests__/fixtures/Mounter.svelte

View workflow job for this annotation

GitHub Actions / Node 16, Svelte 4

Run autofix to sort these imports!

Check failure on line 2 in src/__tests__/fixtures/Mounter.svelte

View workflow job for this annotation

GitHub Actions / Node 18, Svelte 4

Run autofix to sort these imports!

Check failure on line 2 in src/__tests__/fixtures/Mounter.svelte

View workflow job for this annotation

GitHub Actions / Node 20, Svelte 3

Run autofix to sort these imports!

Check failure on line 2 in src/__tests__/fixtures/Mounter.svelte

View workflow job for this annotation

GitHub Actions / Node 20, Svelte 4

Run autofix to sort these imports!
export let onMounted
export let onDestroyed
onMount(() => onMounted())
onDestroy(() => onDestroyed())
</script>

<button />
40 changes: 0 additions & 40 deletions src/__tests__/fixtures/Stopwatch.svelte

This file was deleted.

33 changes: 33 additions & 0 deletions src/__tests__/mount.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { describe, expect, test, vi } from 'vitest'

import { act, render, screen } from '..'
import Mounter from './fixtures/Mounter.svelte'

describe('mount and destroy', () => {
const handleMount = vi.fn()
const handleDestroy = vi.fn()

test('component is mounted', async () => {
await act(() => {
render(Mounter, { onMounted: handleMount, onDestroyed: handleDestroy })
})

const content = screen.getByRole('button')

expect(handleMount).toHaveBeenCalledOnce()
expect(content).toBeInTheDocument()
})

test('component is destroyed', async () => {
const { unmount } = render(Mounter, {
onMounted: handleMount,
onDestroyed: handleDestroy,
})

await act(() => unmount())
const content = screen.queryByRole('button')

expect(handleDestroy).toHaveBeenCalledOnce()
expect(content).not.toBeInTheDocument()
})
})
35 changes: 0 additions & 35 deletions src/__tests__/unmount.test.js

This file was deleted.

9 changes: 7 additions & 2 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ import { svelte } from '@sveltejs/vite-plugin-svelte'
import { defineConfig } from 'vite'

// https://vitejs.dev/config/
export default defineConfig({
export default defineConfig(({ mode }) => ({
plugins: [svelte()],
resolve: {
conditions: mode === 'test' ? ['browser'] : [],
},
test: {
environment: 'jsdom',
setupFiles: ['./src/__tests__/_vitest-setup.js'],
mockReset: true,
unstubGlobals: true,
coverage: {
provider: 'v8',
include: ['src'],
},
},
})
}))

0 comments on commit d59d56f

Please sign in to comment.