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

fix!: Rerender to only update props of component without destroying it #210

Merged
merged 7 commits into from
Feb 12, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
15 changes: 15 additions & 0 deletions src/__tests__/fixtures/Rerender.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<script>
import { getContext, onMount } from 'svelte'

export let name

const mountCounter = getContext('mountCounter')

onMount(() => {
mountCounter.update((i) => i + 1)
})
</script>

<h1 data-testid="test">Hello {name}!</h1>

<div data-testid="mount-counter">{$mountCounter}</div>
42 changes: 16 additions & 26 deletions src/__tests__/rerender.test.js
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
/**
* @jest-environment jsdom
*/
import { describe, expect, test } from 'vitest'
import { expect, test } from 'vitest'
import { writable } from 'svelte/store'

import { render } from '..'
import Comp from './fixtures/Comp.svelte'
import { render, waitFor } from '..'
import Comp from './fixtures/Rerender.svelte'

describe('rerender', () => {
test('mounts new component successfully', () => {
const { container, rerender } = render(Comp, { props: { name: 'World 1' } })
const mountCounter = writable(0)

expect(container.firstChild).toHaveTextContent('Hello World 1!')
rerender({ props: { name: 'World 2' } })
expect(container.firstChild).toHaveTextContent('Hello World 2!')
test('mounts new component successfully', async () => {
const { getByTestId, rerender } = render(Comp, {
props: { name: 'World 1' },
context: new Map(Object.entries({ mountCounter })),
})

test('destroys old component', () => {
let isDestroyed

const { rerender, component } = render(Comp, { props: { name: '' } })

component.$$.on_destroy.push(() => {
isDestroyed = true
})
rerender({ props: { name: '' } })
expect(isDestroyed).toBeTruthy()
await waitFor(() => {
expect(getByTestId('test')).toHaveTextContent('Hello World 1!')
expect(getByTestId('mount-counter')).toHaveTextContent('1')
})

test('destroys old components on multiple rerenders', () => {
const { rerender, queryByText } = render(Comp, { props: { name: 'Neil' } })

rerender({ props: { name: 'Alex' } })
expect(queryByText('Hello Neil!')).not.toBeInTheDocument()
rerender({ props: { name: 'Geddy' } })
expect(queryByText('Hello Alex!')).not.toBeInTheDocument()
rerender({ props: { name: 'World 2' } })
await waitFor(() => {
expect(getByTestId('test')).toHaveTextContent('Hello World 2!')
expect(getByTestId('mount-counter')).toHaveTextContent('1')
})
})
18 changes: 3 additions & 15 deletions src/pure.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,9 @@ const render = (
container,
component,
debug: (el = container) => console.log(prettyDOM(el)),
rerender: (options) => {
if (componentCache.has(component)) component.$destroy()

// eslint-disable-next-line no-new
component = new ComponentConstructor({
target,
...checkProps(options)
})

containerCache.add({ container, target, component })
componentCache.add(component)

component.$$.on_destroy.push(() => {
componentCache.delete(component)
})
rerender: async (options) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need to accept all options here, I think we should just accept props.

This would match the signature of vue-testing-library's rerender method, so I think the sibling consistency would be worthwhile

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree only passing props makes sense. However, this is a change that will break the code of anybody using rerender, so I'm adding a small check that will see if the user passed { props: { ... } } and warn them that's deprecated. And we can hard phase out that signature on the next major update.

component.$set(options.props)
await tick()
},
unmount: () => {
if (componentCache.has(component)) component.$destroy()
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type RenderResult<C extends SvelteComponent, Q extends Queries = typeof q
container: HTMLElement
component: C
debug: (el?: HTMLElement | DocumentFragment) => void
rerender: (options: SvelteComponentOptions<C>) => void
rerender: (options: SvelteComponentOptions<C>) => Promise<void>
Copy link
Collaborator

@mcous mcous Feb 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followup on my other suggestion; I think this would become

Suggested change
rerender: (options: SvelteComponentOptions<C>) => Promise<void>
rerender: (props: ComponentProps<C>) => Promise<void>

unmount: () => void
} & { [P in keyof Q]: BoundFunction<Q[P]> }

Expand Down
Loading