-
Notifications
You must be signed in to change notification settings - Fork 33
fix!: align target
and baseElement
options with testing-library
#325
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
Merged
Merged
Changes from all commits
Commits
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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,23 +1,17 @@ | ||
<svelte:options accessors /> | ||
|
||
<script> | ||
import { getContext } from 'svelte' | ||
|
||
export let name | ||
|
||
let buttonText = 'Button' | ||
|
||
const contextName = getContext('name') | ||
|
||
function handleClick () { | ||
function handleClick() { | ||
buttonText = 'Button Clicked' | ||
} | ||
</script> | ||
|
||
<h1 data-testid="test">Hello {name}!</h1> | ||
|
||
<div>we have {contextName}</div> | ||
|
||
<button on:click={handleClick}>{buttonText}</button> | ||
|
||
<style></style> |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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,5 +1,7 @@ | ||
<script lang="ts"> | ||
export let name: string | ||
export let count: number | ||
</script> | ||
|
||
<h1>hello {name}</h1> | ||
<p>count: {count}</p> |
This file contains hidden or 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 hidden or 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,123 +1,88 @@ | ||
import { render } from '@testing-library/svelte' | ||
import { VERSION as SVELTE_VERSION } from 'svelte/compiler' | ||
import { beforeEach, describe, expect, test } from 'vitest' | ||
import { describe, expect, test } from 'vitest' | ||
|
||
import { act, render as stlRender } from '@testing-library/svelte' | ||
import Comp from './fixtures/Comp.svelte' | ||
import CompDefault from './fixtures/Comp2.svelte' | ||
|
||
describe('render', () => { | ||
let props | ||
|
||
const render = (additional = {}) => { | ||
return stlRender(Comp, { | ||
target: document.body, | ||
props, | ||
...additional, | ||
}) | ||
} | ||
|
||
beforeEach(() => { | ||
props = { | ||
name: 'World', | ||
} | ||
}) | ||
const props = { name: 'World' } | ||
|
||
test('renders component into the document', () => { | ||
const { getByText } = render() | ||
const { getByText } = render(Comp, { props }) | ||
|
||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
}) | ||
|
||
// Dear reader, this is not something you generally want to do in your tests. | ||
test('programmatically change props', async () => { | ||
const { component, getByText } = render() | ||
|
||
test('accepts props directly', () => { | ||
const { getByText } = render(Comp, props) | ||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
|
||
await act(() => { | ||
component.$set({ name: 'Worlds' }) | ||
}) | ||
|
||
expect(getByText('Hello Worlds!')).toBeInTheDocument() | ||
}) | ||
|
||
test('change props with accessors', async () => { | ||
const { component, getByText } = render( | ||
SVELTE_VERSION < '5' ? { accessors: true } : {} | ||
) | ||
|
||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
|
||
expect(component.name).toBe('World') | ||
|
||
await act(() => { | ||
component.value = 'Planet' | ||
}) | ||
|
||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
test('throws error when mixing svelte component options and props', () => { | ||
expect(() => { | ||
render(Comp, { props, name: 'World' }) | ||
}).toThrow(/Unknown options/) | ||
}) | ||
|
||
test('should accept props directly', () => { | ||
const { getByText } = stlRender(Comp, { name: 'World' }) | ||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
test('throws error when mixing target option and props', () => { | ||
expect(() => { | ||
render(Comp, { target: document.createElement('div'), name: 'World' }) | ||
}).toThrow(/Unknown options/) | ||
}) | ||
|
||
test.runIf(SVELTE_VERSION < '5')( | ||
'should accept svelte v4 component options', | ||
() => { | ||
const target = document.createElement('div') | ||
const div = document.createElement('div') | ||
document.body.appendChild(target) | ||
target.appendChild(div) | ||
const { container } = stlRender(Comp, { | ||
target, | ||
anchor: div, | ||
props: { name: 'World' }, | ||
context: new Map([['name', 'context']]), | ||
}) | ||
expect(container).toMatchSnapshot() | ||
} | ||
) | ||
|
||
test.runIf(SVELTE_VERSION >= '5')( | ||
'should accept svelte v5 component options', | ||
() => { | ||
const target = document.createElement('section') | ||
document.body.appendChild(target) | ||
|
||
const { container } = stlRender(Comp, { | ||
target, | ||
props: { name: 'World' }, | ||
context: new Map([['name', 'context']]), | ||
}) | ||
expect(container).toMatchSnapshot() | ||
} | ||
) | ||
test('should return a container object wrapping the DOM of the rendered component', () => { | ||
const { container, getByTestId } = render(Comp, props) | ||
const firstElement = getByTestId('test') | ||
|
||
test('should throw error when mixing svelte component options and props', () => { | ||
expect(() => { | ||
stlRender(Comp, { props: {}, name: 'World' }) | ||
}).toThrow(/Unknown options were found/) | ||
expect(container.firstChild).toBe(firstElement) | ||
}) | ||
|
||
test('should return a container object, which contains the DOM of the rendered component', () => { | ||
const { container } = render() | ||
test('should return a baseElement object, which holds the container', () => { | ||
const { baseElement, container } = render(Comp, props) | ||
|
||
expect(container.innerHTML).toBe(document.body.innerHTML) | ||
expect(baseElement).toBe(document.body) | ||
expect(baseElement.firstChild).toBe(container) | ||
}) | ||
|
||
test('correctly find component constructor on the default property', () => { | ||
const { getByText } = stlRender(CompDefault, { props: { name: 'World' } }) | ||
test('if target is provided, use it as container and baseElement', () => { | ||
const target = document.createElement('div') | ||
const { baseElement, container } = render(Comp, { props, target }) | ||
|
||
expect(getByText('Hello World!')).toBeInTheDocument() | ||
expect(container).toBe(target) | ||
expect(baseElement).toBe(target) | ||
}) | ||
|
||
test("accept the 'context' option", () => { | ||
const { getByText } = stlRender(Comp, { | ||
props: { name: 'Universe' }, | ||
context: new Map([['name', 'context']]), | ||
}) | ||
test('allow baseElement to be specified', () => { | ||
const customBaseElement = document.createElement('div') | ||
|
||
expect(getByText('we have context')).toBeInTheDocument() | ||
const { baseElement, container } = render( | ||
Comp, | ||
{ props }, | ||
{ baseElement: customBaseElement } | ||
) | ||
|
||
expect(baseElement).toBe(customBaseElement) | ||
expect(baseElement.firstChild).toBe(container) | ||
}) | ||
|
||
test.runIf(SVELTE_VERSION < '5')( | ||
'should accept anchor option in Svelte v4', | ||
() => { | ||
const baseElement = document.body | ||
const target = document.createElement('section') | ||
const anchor = document.createElement('div') | ||
baseElement.appendChild(target) | ||
target.appendChild(anchor) | ||
|
||
const { getByTestId } = render( | ||
Comp, | ||
{ props, target, anchor }, | ||
{ baseElement } | ||
) | ||
const firstElement = getByTestId('test') | ||
|
||
expect(target.firstChild).toBe(firstElement) | ||
expect(target.lastChild).toBe(anchor) | ||
} | ||
) | ||
}) |
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.
Uh oh!
There was an error while loading. Please reload this page.