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

NEXT Implement Svelte component unit tests #2968

Open
wants to merge 18 commits into
base: next
Choose a base branch
from

Conversation

Hugos68
Copy link
Contributor

@Hugos68 Hugos68 commented Nov 15, 2024

Linked Issue

Closes #2363

Description

  • Implements Vitest + Testing Library for the Svelte components package.
  • Implements test cases for all current components.

Checklist

Please read and apply all contribution requirements.

  • Your branch should be prefixed with: docs/, feature/, chore/, bugfix/
  • Skeleton v3 contributions must target the next branch (NEVER dev or master)
  • Documentation should be updated to describe all relevant changes
  • Run pnpm check in the root of the monorepo
  • Run pnpm format in the root of the monorepo
  • Run pnpm lint in the root of the monorepo
  • Run pnpm test in the root of the monorepo
  • If you modify /package projects, please supply a Changeset

Changsets

View our documentation to learn more about Changesets. To create a Changeset:

  1. Navigate to the root of the monorepo in your terminal
  2. Run pnpm changeset and follow the prompts
  3. Commit and push the changeset before flagging your PR review for review.

Copy link

changeset-bot bot commented Nov 15, 2024

⚠️ No Changeset found

Latest commit: 025e1cb

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Nov 15, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
skeleton-docs ❌ Failed (Inspect) Nov 15, 2024 10:55pm
skeleton-themes ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 15, 2024 10:55pm

Copy link

vercel bot commented Nov 15, 2024

@Hugos68 is attempting to deploy a commit to the Skeleton Labs Team on Vercel.

A member of the Team first needs to authorize it.

@endigo9740 endigo9740 changed the title Chore: Svelte Tests NEXT Implement Svelte component unit tests Nov 15, 2024
@endigo9740
Copy link
Contributor

@Hugos68 I can't tag you as a reviewer, but I'd welcome a review from you if you have time this weekend!

Copy link
Contributor Author

@Hugos68 Hugos68 left a comment

Choose a reason for hiding this comment

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

Looks great, just a handful of small things.

<script lang="ts">
import { Accordion } from '$lib/index.js';
let { childProps = {} } = $props();
let chilPropsSpread = childProps ?? {};
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Any reason why you keep creating these xSpread props? This won't be reactive so we should watch out for that aswell.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I mean to bring this up before all the issues today - when you try to spread the fallback {} there's a type error with Svelte check. This was the only way I was able to work around it. I'm open to suggestions. Just revert the change and run pnpm check to see what I mean.

it(`Correctly applies the \`${prop}\` prop`, () => {
const value = 'bg-green-500';
render(AccordionTest, { childProps: { [prop]: value } });
const component = screen.getAllByTestId(testId)[0];
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This should be getByTestId right?

Copy link
Contributor

Choose a reason for hiding this comment

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

There's multiple instances of the child components in Accordion.test.svelte. This queries them all, but then we can narrow this down by index. This is useful for some components like Segment Controllers and Tabs, where we can select between the first or second child, which might be active/inactive/etc.

getByTestId would probably be equivalent for grabbing the first instance on the page. But I like the explicit that we're selecting on of many.

}
});

describe('NavRail > NavTile', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Instead of NavRail > NavTile you can do this:

describe('NavRail', () => {
  // The tests for `NavRail`

  describe('NavTile', () => {
  
  });
});

Nesting describes will then add those arrows in the console output and group them nicely.

Copy link
Contributor

Choose a reason for hiding this comment

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

I suppose that's true, but I liked all the Tile tests next to one another. Either works for me tbh.

import { describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/svelte';

// import { mockSnippet } from '$lib/internal/test-utils.js';
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Commented import, can be removed

import type { Snippet } from 'svelte';

/** Use `mockSnippet(value)` to create `<span>{value}</span>` snippet content. */
export function mockSnippet(key: string): Snippet<[]> {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

No critique, just wanted to say this is really cool!

Copy link
Contributor

Choose a reason for hiding this comment

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

It really is!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants