From b41c4d168b07a72ed734b5c23a47596af4138ed8 Mon Sep 17 00:00:00 2001 From: Michael Cousins Date: Sun, 9 Mar 2025 14:25:23 -0400 Subject: [PATCH] refactor(core): rename internal core modules for clarity --- package.json | 4 +++- src/__tests__/render-runes.test-d.ts | 5 +++-- src/__tests__/render-utilities.test-d.ts | 14 +++++++------- src/__tests__/render.test-d.ts | 3 ++- src/core/index.js | 11 ++++------- src/core/{legacy.js => mount-legacy.js} | 0 .../{modern.svelte.js => mount-modern.svelte.js} | 0 src/core/{validate-options.js => prepare.js} | 0 src/{component-types.d.ts => core/types.d.ts} | 0 src/pure.js | 14 +++++++------- 10 files changed, 26 insertions(+), 25 deletions(-) rename src/core/{legacy.js => mount-legacy.js} (100%) rename src/core/{modern.svelte.js => mount-modern.svelte.js} (100%) rename src/core/{validate-options.js => prepare.js} (100%) rename src/{component-types.d.ts => core/types.d.ts} (100%) diff --git a/package.json b/package.json index 77bba08..fb61906 100644 --- a/package.json +++ b/package.json @@ -72,7 +72,9 @@ "types": "svelte-check", "types:legacy": "svelte-check --tsconfig tsconfig.legacy.json", "validate": "npm-run-all test:vitest:* test:jest types build", - "build": "tsc -p tsconfig.build.json && cp src/component-types.d.ts types", + "build": "npm-run-all build:*", + "build:tsc": "tsc -p tsconfig.build.json", + "build:copy-dts": "cp src/core/types.d.ts types/core", "contributors:add": "all-contributors add", "contributors:generate": "all-contributors generate", "preview-release": "./scripts/preview-release", diff --git a/src/__tests__/render-runes.test-d.ts b/src/__tests__/render-runes.test-d.ts index 48d9d27..340c710 100644 --- a/src/__tests__/render-runes.test-d.ts +++ b/src/__tests__/render-runes.test-d.ts @@ -29,8 +29,9 @@ describe('types', () => { test('render result has container and component', () => { const result = subject.render(Component, { name: 'Alice', count: 42 }) - expectTypeOf(result).toMatchTypeOf<{ + expectTypeOf(result).toExtend<{ container: HTMLElement + baseElement: HTMLElement component: { hello: string } debug: (el?: HTMLElement) => void rerender: (props: { name?: string; count?: number }) => Promise @@ -55,7 +56,7 @@ describe('legacy component types', () => { count: 42, }) - expectTypeOf(component).toMatchTypeOf<{ hello: string }>() + expectTypeOf(component).toExtend<{ hello: string }>() // @ts-expect-error: Svelte 5 mount does not return `$set` component.$on('greeting', onGreeting) diff --git a/src/__tests__/render-utilities.test-d.ts b/src/__tests__/render-utilities.test-d.ts index b45614e..d1e3869 100644 --- a/src/__tests__/render-utilities.test-d.ts +++ b/src/__tests__/render-utilities.test-d.ts @@ -8,7 +8,7 @@ describe('render query and utility types', () => { test('render result has default queries', () => { const result = subject.render(Component, { name: 'Alice' }) - expectTypeOf(result.getByRole).parameters.toMatchTypeOf< + expectTypeOf(result.getByRole).parameters.toExtend< [role: subject.ByRoleMatcher, options?: subject.ByRoleOptions] >() }) @@ -27,25 +27,25 @@ describe('render query and utility types', () => { { queries: { getByVibes } } ) - expectTypeOf(result.getByVibes).parameters.toMatchTypeOf<[vibes: string]>() + expectTypeOf(result.getByVibes).parameters.toExtend<[vibes: string]>() }) test('act is an async function', () => { - expectTypeOf(subject.act).toMatchTypeOf<() => Promise>() + expectTypeOf(subject.act).toExtend<() => Promise>() }) test('act accepts a sync function', () => { - expectTypeOf(subject.act).toMatchTypeOf<(fn: () => void) => Promise>() + expectTypeOf(subject.act).toExtend<(fn: () => void) => Promise>() }) test('act accepts an async function', () => { - expectTypeOf(subject.act).toMatchTypeOf< + expectTypeOf(subject.act).toExtend< (fn: () => Promise) => Promise >() }) test('fireEvent is an async function', () => { - expectTypeOf(subject.fireEvent).toMatchTypeOf< + expectTypeOf(subject.fireEvent).toExtend< ( element: Element | Node | Document | Window, event: Event @@ -54,7 +54,7 @@ describe('render query and utility types', () => { }) test('fireEvent[eventName] is an async function', () => { - expectTypeOf(subject.fireEvent.click).toMatchTypeOf< + expectTypeOf(subject.fireEvent.click).toExtend< ( element: Element | Node | Document | Window, // eslint-disable-next-line @typescript-eslint/no-empty-object-type diff --git a/src/__tests__/render.test-d.ts b/src/__tests__/render.test-d.ts index 08cad70..76710f3 100644 --- a/src/__tests__/render.test-d.ts +++ b/src/__tests__/render.test-d.ts @@ -37,8 +37,9 @@ describe('types', () => { test('render result has container and component', () => { const result = subject.render(Component, { name: 'Alice', count: 42 }) - expectTypeOf(result).toMatchTypeOf<{ + expectTypeOf(result).toExtend<{ container: HTMLElement + baseElement: HTMLElement component: { hello: string } debug: (el?: HTMLElement) => void rerender: (props: { name?: string; count?: number }) => Promise diff --git a/src/core/index.js b/src/core/index.js index f4a40aa..1c638e8 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -5,15 +5,12 @@ * Will switch to legacy, class-based mounting logic * if it looks like we're in a Svelte <= 4 environment. */ -import * as LegacyCore from './legacy.js' -import * as ModernCore from './modern.svelte.js' -import { - createValidateOptions, - UnknownSvelteOptionsError, -} from './validate-options.js' +import * as MountLegacy from './mount-legacy.js' +import * as MountModern from './mount-modern.svelte.js' +import { createValidateOptions, UnknownSvelteOptionsError } from './prepare.js' const { mount, unmount, updateProps, allowedOptions } = - ModernCore.IS_MODERN_SVELTE ? ModernCore : LegacyCore + MountModern.IS_MODERN_SVELTE ? MountModern : MountLegacy /** Validate component options. */ const validateOptions = createValidateOptions(allowedOptions) diff --git a/src/core/legacy.js b/src/core/mount-legacy.js similarity index 100% rename from src/core/legacy.js rename to src/core/mount-legacy.js diff --git a/src/core/modern.svelte.js b/src/core/mount-modern.svelte.js similarity index 100% rename from src/core/modern.svelte.js rename to src/core/mount-modern.svelte.js diff --git a/src/core/validate-options.js b/src/core/prepare.js similarity index 100% rename from src/core/validate-options.js rename to src/core/prepare.js diff --git a/src/component-types.d.ts b/src/core/types.d.ts similarity index 100% rename from src/component-types.d.ts rename to src/core/types.d.ts diff --git a/src/pure.js b/src/pure.js index 875f87c..35a10fa 100644 --- a/src/pure.js +++ b/src/pure.js @@ -13,8 +13,8 @@ const componentCache = new Set() /** * Customize how Svelte renders the component. * - * @template {import('./component-types.js').Component} C - * @typedef {import('./component-types.js').Props | Partial>} SvelteComponentOptions + * @template {import('./core/types.js').Component} C + * @typedef {import('./core/types.js').Props | Partial>} SvelteComponentOptions */ /** @@ -30,15 +30,15 @@ const componentCache = new Set() /** * The rendered component and bound testing functions. * - * @template {import('./component-types.js').Component} C + * @template {import('./core/types.js').Component} C * @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries] * * @typedef {{ * container: HTMLElement * baseElement: HTMLElement - * component: import('./component-types.js').Exports + * component: import('./core/types.js').Exports * debug: (el?: HTMLElement | DocumentFragment) => void - * rerender: (props: Partial>) => Promise + * rerender: (props: Partial>) => Promise * unmount: () => void * } & { * [P in keyof Q]: import('@testing-library/dom').BoundFunction @@ -48,10 +48,10 @@ const componentCache = new Set() /** * Render a component into the document. * - * @template {import('./component-types.js').Component} C + * @template {import('./core/types.js').Component} C * @template {import('@testing-library/dom').Queries} [Q=typeof import('@testing-library/dom').queries] * - * @param {import('./component-types.js').ComponentType} Component - The component to render. + * @param {import('./core/types.js').ComponentType} Component - The component to render. * @param {SvelteComponentOptions} options - Customize how Svelte renders the component. * @param {RenderOptions} renderOptions - Customize how Testing Library sets up the document and binds queries. * @returns {RenderResult} The rendered component and bound testing functions.