-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add simple typing tests (#298)
* test: add simple typing tests --------- Co-authored-by: Yanick Champoux <[email protected]>
- Loading branch information
Showing
6 changed files
with
90 additions
and
3 deletions.
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
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
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,5 @@ | ||
<script lang="ts"> | ||
export let name: string | ||
</script> | ||
|
||
<h1>hello {name}</h1> |
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,10 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "node16", | ||
"noEmit": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"types": ["svelte", "vite/client", "vitest", "vitest/globals"], | ||
}, | ||
"include": ["src", "types"], | ||
} |
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,58 @@ | ||
import { expectTypeOf } from 'expect-type' | ||
import type { ComponentProps, SvelteComponent } from 'svelte' | ||
import { describe, test } from 'vitest' | ||
|
||
import Simple from '../src/__tests__/fixtures/Simple.svelte' | ||
import * as subject from './index.js' | ||
|
||
describe('types', () => { | ||
test('render is a function that accepts a Svelte component', () => { | ||
subject.render(Simple, { name: 'Alice' }) | ||
subject.render(Simple, { props: { name: 'Alice' } }) | ||
}) | ||
|
||
test('invalid prop types are rejected', () => { | ||
// @ts-expect-error: name should be a string | ||
subject.render(Simple, { name: 42 }) | ||
|
||
// @ts-expect-error: name should be a string | ||
subject.render(Simple, { props: { name: 42 } }) | ||
}) | ||
|
||
test('render result has container and component', () => { | ||
const result = subject.render(Simple, { name: 'Alice' }) | ||
|
||
expectTypeOf(result).toMatchTypeOf<{ | ||
container: HTMLElement | ||
component: SvelteComponent<{ name: string }> | ||
debug: (el?: HTMLElement) => void | ||
rerender: (options: ComponentProps<Simple>) => void | ||
unmount: () => void | ||
}>() | ||
}) | ||
|
||
test('render result has default queries', () => { | ||
const result = subject.render(Simple, { name: 'Alice' }) | ||
|
||
expectTypeOf(result.getByRole).parameters.toMatchTypeOf< | ||
[role: subject.ByRoleMatcher, options?: subject.ByRoleOptions] | ||
>() | ||
}) | ||
|
||
test('render result can have custom queries', () => { | ||
const [getByVibes] = subject.buildQueries( | ||
(_container: HTMLElement, vibes: string) => { | ||
throw new Error(`unimplemented ${vibes}`) | ||
}, | ||
() => '', | ||
() => '' | ||
) | ||
const result = subject.render( | ||
Simple, | ||
{ name: 'Alice' }, | ||
{ queries: { getByVibes } } | ||
) | ||
|
||
expectTypeOf(result.getByVibes).parameters.toMatchTypeOf<[vibes: string]>() | ||
}) | ||
}) |