-
Notifications
You must be signed in to change notification settings - Fork 33
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
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f4b56e0
Update component props with rerender
basarbk b7f78ee
Update rerender return type
basarbk b9d5fc2
Merge branch 'main' into rerender-update-props
yanick 827186f
Merge remote-tracking branch 'origin/main' into rerender-update-props
yanick 8bf0f96
add rerender test
yanick ba6549a
provide a warning for deprecated use
yanick 4e18992
prettier changes
yanick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Followup on my other suggestion; I think this would become
Suggested change
|
||||||
unmount: () => void | ||||||
} & { [P in keyof Q]: BoundFunction<Q[P]> } | ||||||
|
||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 usingrerender
, 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.