From 33b18125526845c848ca34c278a32f09d7fb64fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Schr=C3=B6ter?= Date: Fri, 27 Dec 2024 19:49:40 +0100 Subject: [PATCH] fix tests --- bun.lockb | Bin 734420 -> 734420 bytes .../src/components/carousel/carousel.test.tsx | 43 +++++++++++ .../components/carousel/examples/basic.tsx | 2 +- .../src/components/carousel/tests/basic.tsx | 28 ------- .../carousel/tests/carousel.test.tsx | 71 ------------------ packages/react/src/setup-test.ts | 13 ++++ 6 files changed, 57 insertions(+), 100 deletions(-) create mode 100644 packages/react/src/components/carousel/carousel.test.tsx delete mode 100644 packages/react/src/components/carousel/tests/basic.tsx delete mode 100644 packages/react/src/components/carousel/tests/carousel.test.tsx diff --git a/bun.lockb b/bun.lockb index e2c25046e6a9859c1a1e4d54a98215bb4b508f86..056f89de7451e1fc6fb2c46ca73b752102d6b3ae 100755 GIT binary patch delta 2202 zcmXYyX>3$g6oB8n>3e5pI;EDaPz0J#DUqrbQL4d>7!&meTUlCA3kor6V&jU@I4nbe zC=ms@T2VlvR!I~?;YCH%uoaL+s2d6mf?xsJ3oZft&Tww}y*c;mx%b_B&&+!*Z51tT z6;rdZte=f%^Rhux*w}i{x&klP8UoMriBhoH+#+0zem-n5_l~(VZmYR>)wxrx;f_p3 z^Jyzkd3<(*ndX{oXm{K+bE|M6{SwHU`x2*1=>fCNePv^>#LY6d)?80qA)rgVuaUZk zUeNKf^fu#ysAhk-GM>pJs~G<%chz}@LSXJOoX{Tzp}8kgndTlKo`ciU%rf^N@vAm= zj=4JAT%4BcWpfjVyWPq~OEVYApEnV7GIjG2x|4^Xx4DHlo%`WjKiYUGd`U9*Vzh3* zQ7rxE2Dn87+FqO<+eR3K(;Bs!+eAFT^7h-s`+-<*J^{{}`;k}|sjeNT3;YS1 z^`;Zyyvd)5^|;mjW9}E?QqUd#XKoMi+n}|)fYVz43U8V765J%}c``ha$du#s$Unfb zPj4s}-GrX*hvtUi#?jZ-Xl^)8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ngGMy|~5U91JPs4&!(g{&5Qe zDRNDLl#V*WZ$u~PZ^c3AfG@b-H_!sDupRz}fITH(7jYYGg9G{QQXr>;uOs(cLcVmz zL#cD?@}x1^l%Smn*DI9iVJB5P>Rt%tc{ehW+TN?!%|ciNwQ!$X9La<3LL{aBT`m-< zy7L?xIS;4d82kZe;5eLuv+z6o370c&l*rIjKQ>nkecf!4v0<7p-+eCfu;1TZ6RF4y z2tBU^2ErhBM^X;Dyp+uJXSrD^c{G}@MNGyUQ!*`Z1qHIgy;~sd;b7Jo { + afterAll(() => { + cleanup() + }) + + render() + + it.each(getParts(carouselAnatomy))('should render part %s', async (part) => { + expect(document.querySelector(part)).toBeInTheDocument() + }) + + it.each(getExports(carouselAnatomy))('should export %s', async (part) => { + expect(Carousel[part]).toBeDefined() + }) +}) + +describe('Carousel', () => { + afterEach(() => { + cleanup() + }) + + it('should have no a11y violations', async () => { + const { container } = render() + const results = await axe(container) + + expect(results).toHaveNoViolations() + }) + + it('should have the correct disabled / enabled states for control buttons', async () => { + render() + const prevButton = screen.getByRole('button', { name: 'Previous slide' }) + const nextButton = screen.getByRole('button', { name: 'Next slide' }) + + await waitFor(() => expect(prevButton).toBeDisabled()) + await waitFor(() => expect(nextButton).toBeEnabled()) + }) +}) diff --git a/packages/react/src/components/carousel/examples/basic.tsx b/packages/react/src/components/carousel/examples/basic.tsx index b742fa31ba..e1226dbf42 100644 --- a/packages/react/src/components/carousel/examples/basic.tsx +++ b/packages/react/src/components/carousel/examples/basic.tsx @@ -4,7 +4,7 @@ const images = Array(5).fill('https://picsum.photos/seed/a/500/300') export const Basic = () => { return ( - + Play or Pause Previous diff --git a/packages/react/src/components/carousel/tests/basic.tsx b/packages/react/src/components/carousel/tests/basic.tsx deleted file mode 100644 index ab8f7072f6..0000000000 --- a/packages/react/src/components/carousel/tests/basic.tsx +++ /dev/null @@ -1,28 +0,0 @@ -import { Carousel } from '../' - -const images = Array(5).fill('https://picsum.photos/seed/a/500/300') - -export const ComponentUnderTest = (props: Carousel.RootProps) => { - return ( - - - Previous - Next - - - {images.map((_, index) => ( - - {index + 1} - - ))} - - - {images.map((image, index) => ( - - Slide # - - ))} - - - ) -} diff --git a/packages/react/src/components/carousel/tests/carousel.test.tsx b/packages/react/src/components/carousel/tests/carousel.test.tsx deleted file mode 100644 index 0f69aee3f1..0000000000 --- a/packages/react/src/components/carousel/tests/carousel.test.tsx +++ /dev/null @@ -1,71 +0,0 @@ -import { cleanup, render, screen } from '@testing-library/react/pure' -import user from '@testing-library/user-event' -import { axe } from 'vitest-axe' -import { Carousel, carouselAnatomy } from '../' -import { getExports, getParts } from '../../../setup-test' -import { ComponentUnderTest } from './basic' - -describe('Carousel / Parts & Exports', () => { - afterAll(() => { - cleanup() - }) - - render() - - it.each(getParts(carouselAnatomy))('should render part %s', async (part) => { - expect(document.querySelector(part)).toBeInTheDocument() - }) - - it.each(getExports(carouselAnatomy))('should export %s', async (part) => { - expect(Carousel[part]).toBeDefined() - }) -}) - -describe('Carousel', () => { - afterEach(() => { - cleanup() - }) - - it('should have no a11y violations', async () => { - const { container } = render() - const results = await axe(container) - - expect(results).toHaveNoViolations() - }) - - it('should have the correct disabled / enabled states for control buttons', async () => { - render() - const prevButton = screen.getByRole('button', { name: 'Previous Slide' }) - const nextButton = screen.getByRole('button', { name: 'Next Slide' }) - - // first slide - expect(prevButton).toBeDisabled() - expect(nextButton).toBeEnabled() - - // second slide - await user.click(nextButton) - expect(prevButton).toBeEnabled() - expect(nextButton).toBeEnabled() - - // last slide - await user.click(nextButton) - expect(prevButton).toBeEnabled() - expect(nextButton).toBeDisabled() - }) - - it('goes to the indicated slide on indicator click', async () => { - render() - const indicators = screen.getAllByTestId('indicator') - const slides = screen.getAllByTestId('item') - - expect(slides[0]).toHaveAttribute('data-current', '') - expect(indicators[0]).toHaveAttribute('data-current', '') - - await user.click(indicators[2]) - - expect(indicators[0]).not.toHaveAttribute('data-current', '') - expect(slides[0]).not.toHaveAttribute('data-current', '') - expect(indicators[2]).toHaveAttribute('data-current', '') - expect(slides[2]).toHaveAttribute('data-current', '') - }) -}) diff --git a/packages/react/src/setup-test.ts b/packages/react/src/setup-test.ts index 3d8771f009..ead59f5857 100644 --- a/packages/react/src/setup-test.ts +++ b/packages/react/src/setup-test.ts @@ -6,10 +6,23 @@ import { vi } from 'vitest' import 'vitest-axe/extend-expect' const { window } = new JSDOM() +/** + * IntersectionObserver + */ +const IntersectionObserverMock = vi.fn(() => ({ + disconnect: vi.fn(), + observe: vi.fn(), + takeRecords: vi.fn(), + unobserve: vi.fn(), +})) +vi.stubGlobal('IntersectionObserver', IntersectionObserverMock) +// biome-ignore lint/complexity/useLiteralKeys: +window['IntersectionObserver'] = IntersectionObserverMock vi.stubGlobal('ResizeObserver', ResizeObserver) // biome-ignore lint/complexity/useLiteralKeys: window['ResizeObserver'] = ResizeObserver + window.Element.prototype.scrollTo = () => {} window.Element.prototype.scrollIntoView = () => {} window.requestAnimationFrame = (cb) => setTimeout(cb, 1000 / 60)