Skip to content
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: add missing supported Svelte options to SvelteComponentOptions #212 #213

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
// Definitions by: Rahim Alwer <https://github.com/mihar-22>

import {queries, Queries, BoundFunction, EventType} from '@testing-library/dom'
import { SvelteComponent, ComponentProps } from 'svelte'

import { SvelteComponent, ComponentProps, ComponentConstructorOptions } from 'svelte'

export * from '@testing-library/dom'

type SvelteComponentOptions<C extends SvelteComponent> = ComponentProps<C> | {props: ComponentProps<C>}
type SvelteComponentOptions<C extends SvelteComponent> = ComponentProps<C> | Pick<ComponentConstructorOptions<ComponentProps<C>>, "anchor" | "props" | "hydrate" | "intro" | "context">
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think 'accessors' should also be in that list of picked options?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this line and this unit test, I'd say it definetely should be there because it's a valid option; however not as a Pick I think, because it does not exist on ComponentConstructorOptions.

Maybe something like

type SvelteComponentOptions<C extends SvelteComponent> = ComponentProps<C> | Pick<ComponentConstructorOptions<ComponentProps<C>>, "anchor" | "props" | "hydrate" | "intro" | "context"> & { accessors?: boolean }

?

Copy link
Contributor

@ysitbon ysitbon Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey!

It should not. The accessors is not part of the API and does nothing. The test mentioned above works only because it use the <svelte:options accessors /> as L1. See official doc about <svelte:options> here and about svelte/compiler options here.

The accessors config has no impact here. You can set it to false in your test case, it should still pass the test assertions. As mentioned in the doc, Svelte component accessors are built at compile time and not at runtime. For example with a component defined like this:

<script>
  export let testId;
</script>
<div data-testid={testId} />

Svelte compiler generates this:

// ...generated code for fragment and instance management

// generated class
class Component extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, create_fragment, safe_not_equal, { testId: 0 });
	}
}

export default Component;

Now, if you add <svelte:options accessors /> to the component, the compiler generates this:

// ...generated code for fragment and instance management

// generated class
class Component extends SvelteComponent {
	constructor(options) {
		super();
		init(this, options, instance, create_fragment, safe_not_equal, { testId: 0 });
	}

	get testId() {
		return this.$$.ctx[0];
	}

	set testId(testId) {
		this.$$set({ testId });
		flush();
	}
}

export default Component;

Hope it helps!


type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

Expand Down