Skip to content

Commit

Permalink
fix(svelte5): synchronously flush changes after mount and unmount (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcous authored Jul 25, 2024
1 parent f5b7fe1 commit 75a3f38
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
5 changes: 1 addition & 4 deletions src/__tests__/mount.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, render, screen } from '@testing-library/svelte'
import { render, screen } from '@testing-library/svelte'
import { describe, expect, test, vi } from 'vitest'

import Mounter from './fixtures/Mounter.svelte'
Expand All @@ -14,20 +14,17 @@ describe('mount and destroy', () => {
const content = screen.getByRole('button')

expect(content).toBeInTheDocument()
await act()
expect(onMounted).toHaveBeenCalledTimes(1)
})

test('component is destroyed', async () => {
const { unmount } = renderSubject()

await act()
unmount()

const content = screen.queryByRole('button')

expect(content).not.toBeInTheDocument()
await act()
expect(onDestroyed).toHaveBeenCalledTimes(1)
})
})
3 changes: 2 additions & 1 deletion src/core/modern.svelte.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const mount = (Component, options) => {
const props = $state(options.props ?? {})
const component = Svelte.mount(Component, { ...options, props })

Svelte.flushSync()
propsByComponent.set(component, props)

return component
Expand All @@ -34,7 +35,7 @@ const mount = (Component, options) => {
/** Remove the component from the DOM. */
const unmount = (component) => {
propsByComponent.delete(component)
Svelte.unmount(component)
Svelte.flushSync(() => Svelte.unmount(component))
}

/**
Expand Down

0 comments on commit 75a3f38

Please sign in to comment.