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

Chore: autocleanup #85

Merged
merged 6 commits into from
May 11, 2024
Merged
Show file tree
Hide file tree
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
6 changes: 2 additions & 4 deletions src/lib/components/FloatingArrow/FloatingArrow.test.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { afterEach, describe, expect, it } from 'vitest';
import { screen, cleanup, render } from '@testing-library/svelte';
import { describe, expect, it } from 'vitest';
import { screen, render } from '@testing-library/svelte/svelte5';
import { withEffect } from '$lib/utils/test.svelte.js';

import FloatingArrow from './FloatingArrow.svelte';
import { useFloating } from '$lib/hooks/useFloating/index.svelte.js';

describe('FloatingArrow', () => {
afterEach(() => cleanup());

it(
'renders the component to default props',
withEffect(() => {
Expand Down
32 changes: 1 addition & 31 deletions src/lib/hooks/useClick/index.test.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { cleanup, fireEvent, render, screen } from '@testing-library/svelte';
import { fireEvent, render, screen } from '@testing-library/svelte/svelte5';
import App from './App.test.svelte';

describe('useClick', () => {
Expand All @@ -26,8 +26,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});
it('disables the hook when `enabled` is `false`', async () => {
render(App, { enabled: false });
Expand All @@ -39,8 +37,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

cleanup();
});
});

Expand All @@ -55,8 +51,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});

it('opens on click when `event` is set to `click`', async () => {
Expand All @@ -69,8 +63,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});

it('opens on mousedown when `event` is set to `mousedown`', async () => {
Expand All @@ -83,8 +75,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});
});

Expand All @@ -105,8 +95,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

cleanup();
});

it('when set to `true` changes `open` state to both `true` and `false`', async () => {
Expand All @@ -125,8 +113,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

cleanup();
});

it('when set to `true` changes `open` state to `false` when `open` is initially set to `true`', async () => {
Expand All @@ -139,8 +125,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();
});

cleanup();
});

it('when set to `false` changes `open` state to `true` and not back to `false`', async () => {
Expand All @@ -159,8 +143,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});
it('when set to `false` does not change `open` state back to `false` when `open` is initially set to `true`', async () => {
render(App, { toggle: false, open: true });
Expand All @@ -172,8 +154,6 @@ describe('useClick', () => {
await vi.waitFor(() => {
expect(screen.queryByRole('tooltip')).toBeInTheDocument();
});

cleanup();
});
});

Expand All @@ -184,8 +164,6 @@ describe('useClick', () => {
await fireEvent.keyDown(screen.getByRole('button'), { key: 'Enter' });

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});

it('when set to `true` returns a `Space` keyup event handler', async () => {
Expand All @@ -195,8 +173,6 @@ describe('useClick', () => {
await fireEvent.keyUp(screen.getByRole('button'), { key: ' ' });

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});

it('when applied to a typable reference does not return a `Space` key event handler', async () => {
Expand All @@ -206,8 +182,6 @@ describe('useClick', () => {
await fireEvent.keyUp(screen.getByRole('button'), { key: ' ' });

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();

cleanup();
});

it('when applied to a typable reference does not return a `Enter` key event handler', async () => {
Expand All @@ -216,8 +190,6 @@ describe('useClick', () => {
await fireEvent.keyDown(screen.getByRole('button'), { key: 'Enter' });

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});
});
it('does not change `open` state to `false` on mouseleave when paired with `useHover`', async () => {
Expand All @@ -228,7 +200,5 @@ describe('useClick', () => {
await fireEvent.mouseLeave(screen.getByRole('button'));

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});
});
22 changes: 1 addition & 21 deletions src/lib/hooks/useHover/index.test.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, expect, it, vi } from 'vitest';
import { render, screen, fireEvent, cleanup, act } from '@testing-library/svelte';
import { render, screen, fireEvent, act } from '@testing-library/svelte/svelte5';
import App from './App.test.svelte';

vi.useFakeTimers();
Expand All @@ -10,8 +10,6 @@ describe('useHover', () => {

await fireEvent.mouseEnter(screen.getByRole('button'));
expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});

it('closes on mouseleave', async () => {
Expand All @@ -20,8 +18,6 @@ describe('useHover', () => {
await fireEvent.mouseEnter(screen.getByRole('button'));
await fireEvent.mouseLeave(screen.getByRole('button'));
expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();

cleanup();
});

describe('delay', () => {
Expand All @@ -41,8 +37,6 @@ describe('useHover', () => {
});

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});
it('delays only open when only open is provided a value', async () => {
render(App, { delay: { open: 100 } });
Expand All @@ -60,8 +54,6 @@ describe('useHover', () => {
});

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});
it('delays only close when only close is provided a value', async () => {
render(App, { delay: { close: 100 } });
Expand All @@ -80,8 +72,6 @@ describe('useHover', () => {
});

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();

cleanup();
});
});

Expand Down Expand Up @@ -110,8 +100,6 @@ describe('useHover', () => {
});

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});

it('restMs + nullish open delay should respect restMs', async () => {
Expand All @@ -128,8 +116,6 @@ describe('useHover', () => {
await act(async () => {
vi.advanceTimersByTime(1);
});

cleanup();
});

it.skip('ignores restMs on touch pointers', async () => {
Expand All @@ -141,8 +127,6 @@ describe('useHover', () => {
await act(async () => {});

expect(screen.queryByRole('tooltip')).toBeInTheDocument();

cleanup();
});

it('ignores restMs on touch pointers when mouseOnly is true ', async () => {
Expand All @@ -154,8 +138,6 @@ describe('useHover', () => {
await act(async () => {});

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();

cleanup();
});
});

Expand All @@ -174,7 +156,5 @@ describe('useHover', () => {
});

expect(screen.queryByRole('tooltip')).not.toBeInTheDocument();

cleanup();
});
});
8 changes: 1 addition & 7 deletions src/lib/hooks/useRole/index.test.svelte.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest';
import { cleanup, render, screen } from '@testing-library/svelte';
import { render, screen } from '@testing-library/svelte/svelte5';
import App from './App.test.svelte';

const ARIA_ROLES = ['grid', 'listbox', 'menu', 'tree', 'tooltip', 'alertdialog', 'dialog'] as const;
Expand All @@ -8,14 +8,12 @@ describe('useRole', () => {
it('by default applies the "dialog" role to the floating element', () => {
render(App, { role: undefined, open: true });
expect(screen.queryByRole('dialog')).toBeInTheDocument();
cleanup();
});

for (const role of ARIA_ROLES) {
it(`applies the "${role}" role to the floating element`, () => {
render(App, { role, open: true });
expect(screen.queryByRole(role)).toBeInTheDocument();
cleanup();
});
}

Expand All @@ -31,8 +29,6 @@ describe('useRole', () => {
await rerender({ role: 'tooltip', open: false });

expect(screen.getByRole('buton')).not.toHaveAttribute('aria-describedby');

cleanup();
});
});

Expand All @@ -48,8 +44,6 @@ describe('useRole', () => {
await rerender({ role: 'tooltip', open: false });

expect(screen.getByRole('buton')).not.toHaveAttribute('aria-labelledby');

cleanup();
});
});

Expand Down
1 change: 0 additions & 1 deletion src/vitest-setup.js

This file was deleted.

15 changes: 15 additions & 0 deletions src/vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Injects additional jest matchers, see: https://testing-library.com/docs/svelte-testing-library/setup#vitest
*/
import '@testing-library/jest-dom/vitest';

/**
* Injects a global afterEach to cleanup, see: https://testing-library.com/docs/svelte-testing-library/api#cleanup
*/
import { afterEach } from 'vitest';
import { act, cleanup } from '@testing-library/svelte/svelte5';

afterEach(async () => {
await act();
cleanup();
});
5 changes: 1 addition & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@ export default defineConfig({
plugins: [sveltekit(), svelteTesting()],
test: {
include: ['./src/lib/**/*.{test,test.svelte}.{js,ts}'],
setupFiles: ['./src/vitest-setup.js'],
setupFiles: ['./src/vitest.setup.ts'],
environment: 'jsdom',
coverage: {
reporter: ['html', 'text'],
include: ['./src/lib/**/*.{js,ts}'],
},
alias: {
'@testing-library/svelte': '@testing-library/svelte/svelte5',
},
},
});