Skip to content

Instantly share code, notes, and snippets.

@nathansmith
Last active September 8, 2022 14:58
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathansmith/4f3e93aba1990e6d39b56ae0aaeee576 to your computer and use it in GitHub Desktop.
Save nathansmith/4f3e93aba1990e6d39b56ae0aaeee576 to your computer and use it in GitHub Desktop.
Svelte component, to help test "slot" content
<script lang="ts">
// ======
// Props.
// ======
const { Component, slot, ...otherProps } = $$props;
</script>
<!--
=======
Render.
=======
-->
<Component {...otherProps}>
{slot}
</Component>
<script lang="ts">
// ======
// Props.
// ======
export let handleClick: () => void;
</script>
<style>
/* Etc. */
</style>
<!--
=======
Render.
=======
-->
<button class="my-button" type="button" on:click={handleClick}>
<slot />
</button>
import { fireEvent, render } from '@testing-library/svelte';
// Mocks.
import { SlotWrapper } from '../__mocks__';
// Test subject.
import MyButton from './MyButton.svelte';
// ====================
// Describe `fileName`.
// ====================
describe('MyButton.svelte', (): void => {
// ======================
// Test default scenario.
// ======================
test('handles default scenario', (): void => {
// Dummy props.
const props = {
Component: MyButton,
slot: 'EXAMPLE_SLOT_CONTENT',
handleClick: jest.fn(),
};
// Render.
render(SlotWrapper, { props });
// Get elements.
const parent = document.querySelector('.my-button');
// Test assertions.
expect(parent.textContent).toBe(props.slot);
// Fire events.
fireEvent.click(parent);
// Test assertions.
expect(props.handleClick).toBeCalled();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment