Skip to content

Commit

Permalink
Passing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesnw committed Oct 23, 2024
1 parent f5a96a8 commit 74a15b4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 14 deletions.
1 change: 0 additions & 1 deletion eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export default [
'static/built/*',
'static/styleguide/*',
'yarn.lock',
'vite.config.ts.timestamp*',
],
},
js.configs.recommended,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@sveltejs/kit": "^2.7.2",
"@sveltejs/vite-plugin-svelte": "^4.0.0",
"@testing-library/jest-dom": "^6.5.0",
"@testing-library/svelte": "^5.2.3",
"@testing-library/svelte": "^5.2.4",
"@types/eslint-config-prettier": "^6.11.3",
"@types/eslint__js": "^8.42.3",
"@types/lodash": "^4.17.10",
Expand Down
4 changes: 2 additions & 2 deletions src/lib/components/util/Icon.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
let { name, size = null }: Props = $props();
const SvelteComponent_1 = $derived(icons[name] ?? null);
const IconComponent = $derived(icons[name] ?? null);
</script>

<SvelteComponent_1
<IconComponent
data-icon={name}
data-icon-size={size}
aria-hidden="true"
Expand Down
29 changes: 24 additions & 5 deletions test/lib/components/CopyButton.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { fireEvent, render, waitFor } from '@testing-library/svelte';

import CopyButton from '$lib/components/util/CopyButton.svelte';

function getFirstNonCommentChild(node: Node) {
return [...node.childNodes].find((x) => x.nodeType !== Node.COMMENT_NODE);
}

describe('Copy Button', () => {
beforeEach(() => {
vi.useFakeTimers();
Expand All @@ -17,7 +21,10 @@ describe('Copy Button', () => {
});
const button = getByRole('button');

expect(button.firstChild).toHaveAttribute('data-icon', 'clipboard');
expect(getFirstNonCommentChild(button)).toHaveAttribute(
'data-icon',
'clipboard',
);
});

it('copies content', async () => {
Expand All @@ -27,7 +34,10 @@ describe('Copy Button', () => {
});
const button = getByRole('button');

expect(button.firstChild).toHaveAttribute('data-icon', 'clipboard');
expect(getFirstNonCommentChild(button)).toHaveAttribute(
'data-icon',
'clipboard',
);

await fireEvent.click(button);

Expand All @@ -40,15 +50,24 @@ describe('Copy Button', () => {
});
const button = getByRole('button');

expect(button.firstChild).toHaveAttribute('data-icon', 'clipboard');
expect(getFirstNonCommentChild(button)).toHaveAttribute(
'data-icon',
'clipboard',
);

await fireEvent.click(button);

expect(button.firstChild).toHaveAttribute('data-icon', 'copy');
expect(getFirstNonCommentChild(button)).toHaveAttribute(
'data-icon',
'copy',
);

vi.runAllTimers();
await waitFor(() => {
expect(button.firstChild).toHaveAttribute('data-icon', 'clipboard');
expect(getFirstNonCommentChild(button)).toHaveAttribute(
'data-icon',
'clipboard',
);
});
});
});
2 changes: 2 additions & 0 deletions test/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ beforeAll(() => {
// @ts-expect-error Clipboard isn't writable...
// eslint-disable-next-line @typescript-eslint/no-empty-function
window.navigator.clipboard = { writeText: () => {} };
// @ts-expect-error Allow partial MediaQueryList response
window.matchMedia = () => ({ matches: true });
});
2 changes: 2 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { svelteTesting } from '@testing-library/svelte/vite';
import {
coverageConfigDefaults,
defineConfig,
Expand All @@ -12,6 +13,7 @@ import viteConfig from './vite.config.js';
export default mergeConfig(
viteConfig,
defineConfig({
plugins: [svelteTesting()],
test: {
include: ['./test/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
globals: true,
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ __metadata:
languageName: node
linkType: hard

"@testing-library/svelte@npm:^5.2.3":
version: 5.2.3
resolution: "@testing-library/svelte@npm:5.2.3"
"@testing-library/svelte@npm:^5.2.4":
version: 5.2.4
resolution: "@testing-library/svelte@npm:5.2.4"
dependencies:
"@testing-library/dom": "npm:^10.0.0"
peerDependencies:
Expand All @@ -802,7 +802,7 @@ __metadata:
optional: true
vitest:
optional: true
checksum: 10/db39a364f2d5f9cfa3c0479f2ef91da58b73599e884aeedd6059401c5f519878d37398bf14786b29cee201e8ff9c4536f24118f12274f38fbd83c788ba9c610e
checksum: 10/b2c2c7d3dcfcc271ffcc8c74b41fb32cce934d73ff13326c12ad9c352ab3a73b9e6b38465f56f9fab1d9836cded33fd6deff4071dbd0a81fd2827cf7c9703be9
languageName: node
linkType: hard

Expand Down Expand Up @@ -5955,7 +5955,7 @@ __metadata:
"@sveltejs/kit": "npm:^2.7.2"
"@sveltejs/vite-plugin-svelte": "npm:^4.0.0"
"@testing-library/jest-dom": "npm:^6.5.0"
"@testing-library/svelte": "npm:^5.2.3"
"@testing-library/svelte": "npm:^5.2.4"
"@types/eslint-config-prettier": "npm:^6.11.3"
"@types/eslint__js": "npm:^8.42.3"
"@types/lodash": "npm:^4.17.10"
Expand Down

0 comments on commit 74a15b4

Please sign in to comment.