Skip to content

Commit

Permalink
feat: expose instance property from render
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Apr 1, 2022
1 parent 18952cd commit 0fd1e22
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions src/__tests__/render.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ test("fails when rerendering", async () => {
await expect(
rerender({ name: "Dylan" })
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Components cannot re-render on the server side"`
`"Components cannot re-render on the server side."`
);
});

test("fails when checking emitted events", async () => {
const { emitted } = await render(Clickable);
expect(() => emitted("button-click")).toThrowErrorMatchingInlineSnapshot(
`"Components should not emit events on the server side"`
`"Components should not emit events on the server side."`
);
});

Expand All @@ -74,3 +74,10 @@ test("fails when emitting events", async () => {
`"Cannot perform client side interaction tests on the server side. Please use @marko/testing-library in a browser environment."`
);
});

test("fails when trying to read instance", async () => {
const result = await render(Clickable);
expect(() => result.instance).toThrowErrorMatchingInlineSnapshot(
`"Cannot access component instance for server side tests."`
);
});
1 change: 1 addition & 0 deletions src/index-browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function render<T extends Template>(

return {
container,
instance,
emitted<N extends string = "*">(
type?: N extends InternalEventNames ? never : N
) {
Expand Down
10 changes: 8 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export async function render<T extends Template>(
): Promise<
BoundFunctions<typeof Queries> & {
container: HTMLElement | DocumentFragment;
instance: any;
debug: typeof testingLibraryScreen["debug"];
emitted<N extends string = "*">(
type?: N extends InternalEventNames ? never : N
Expand Down Expand Up @@ -83,16 +84,21 @@ export async function render<T extends Template>(

return {
container,
get instance(): any {
throw new Error(
"Cannot access component instance for server side tests."
);
},
emitted<N extends string = "*">(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type?: N extends InternalEventNames ? never : N
): NonNullable<EventRecord[N]> {
throw new Error("Components should not emit events on the server side");
throw new Error("Components should not emit events on the server side.");
},
// eslint-disable-next-line @typescript-eslint/no-unused-vars
rerender(newInput?: typeof input): Promise<void> {
return Promise.reject(
new Error("Components cannot re-render on the server side")
new Error("Components cannot re-render on the server side.")
);
},
cleanup() {
Expand Down

0 comments on commit 0fd1e22

Please sign in to comment.