Skip to content
Source
Sign inSign up
TanStack/devtools
Open History to see the last commit

plugin.test.ts

44 lines1.6 KB

1import { describe, expect, it, vi } from 'vitest'
2import { createSveltePlugin } from './plugin'
3​
4// A stand-in for a real Svelte component β€” createSveltePlugin only stores the
5// reference, it never mounts it, so an opaque function is sufficient here.
6const FakeComponent = vi.fn() as any
7​
8describe('createSveltePlugin', () => {
9 it('returns a [Plugin, NoOpPlugin] tuple of factory functions', () => {
10 const result = createSveltePlugin('My Plugin', FakeComponent)
11 expect(result).toHaveLength(2)
12 const [Plugin, NoOpPlugin] = result
13 expect(typeof Plugin).toBe('function')
14 expect(typeof NoOpPlugin).toBe('function')
15 })
16​
17 it('Plugin() returns the name, the real component, and the forwarded props', () => {
18 const [Plugin] = createSveltePlugin('My Plugin', FakeComponent)
19 const props = { foo: 'bar' }
20 expect(Plugin(props)).toEqual({
21 name: 'My Plugin',
22 component: FakeComponent,
23 props,
24 })
25 })
26​
27 it('Plugin() works when no props are supplied', () => {
28 const [Plugin] = createSveltePlugin('My Plugin', FakeComponent)
29 expect(Plugin()).toEqual({
30 name: 'My Plugin',
31 component: FakeComponent,
32 props: undefined,
33 })
34 })
35​
36 it('NoOpPlugin() keeps the name but swaps in a no-op component (not the real one)', () => {
37 const [, NoOpPlugin] = createSveltePlugin('My Plugin', FakeComponent)
38 const noop = NoOpPlugin({ foo: 'bar' })
39 expect(noop.name).toBe('My Plugin')
40 expect(noop.component).not.toBe(FakeComponent)
41 expect(typeof noop.component).toBe('function')
42 expect(noop.props).toEqual({ foo: 'bar' })
43 })
44})