|
| 1 | +import { expect, testStep, dtl } from '@epic-web/workshop-utils/test' |
| 2 | +import type VanillaTilt from 'vanilla-tilt' |
| 3 | +const { screen, fireEvent, waitFor } = dtl |
| 4 | + |
| 5 | +interface HTMLVanillaTiltElement extends HTMLDivElement { |
| 6 | + vanillaTilt?: VanillaTilt |
| 7 | +} |
| 8 | + |
| 9 | +import './index.tsx' |
| 10 | + |
| 11 | +const tiltElement = await testStep('Initialize tilt element', async () => { |
| 12 | + const result = await waitFor(() => { |
| 13 | + const element = document.querySelector('.tilt-root') |
| 14 | + expect(element).toBeInTheDocument() |
| 15 | + return element |
| 16 | + }) |
| 17 | + await waitFor(() => { |
| 18 | + expect(result).toHaveProperty('vanillaTilt') |
| 19 | + }) |
| 20 | + return result as HTMLVanillaTiltElement |
| 21 | +}) |
| 22 | + |
| 23 | +const countButton = await testStep('Find count button', async () => { |
| 24 | + const button = await screen.findByRole('button', { name: /0/i }) |
| 25 | + expect(button).toBeInTheDocument() |
| 26 | + return button as HTMLButtonElement |
| 27 | +}) |
| 28 | + |
| 29 | +const maxInput = await testStep('Find max input', async () => { |
| 30 | + const input = (await screen.findByLabelText('Max:')) as HTMLInputElement |
| 31 | + expect(input).toBeInTheDocument() |
| 32 | + return input as HTMLInputElement |
| 33 | +}) |
| 34 | + |
| 35 | +await testStep('Tilt effect persists after count increment', async () => { |
| 36 | + const initialVanillaTilt = tiltElement.vanillaTilt |
| 37 | + fireEvent.click(countButton) |
| 38 | + await screen.findByRole('button', { name: /1/i }) |
| 39 | + expect(tiltElement.vanillaTilt, 'vanilla tilt was reinitialized').toBe( |
| 40 | + initialVanillaTilt, |
| 41 | + ) |
| 42 | +}) |
| 43 | + |
| 44 | +await testStep('Tilt effect resets when options change', async () => { |
| 45 | + const initialVanillaTilt = tiltElement.vanillaTilt |
| 46 | + fireEvent.change(maxInput, { target: { value: '30' } }) |
| 47 | + await waitFor(() => { |
| 48 | + expect(tiltElement.vanillaTilt).not.toBe(initialVanillaTilt) |
| 49 | + }) |
| 50 | +}) |
| 51 | + |
| 52 | +await testStep('Tilt effect uses updated options', async () => { |
| 53 | + const newMax = 35 |
| 54 | + fireEvent.change(maxInput, { target: { value: newMax.toString() } }) |
| 55 | + await waitFor(() => { |
| 56 | + // @ts-expect-error this is not exposed |
| 57 | + expect(tiltElement.vanillaTilt?.settings.max).toBe(newMax) |
| 58 | + }) |
| 59 | +}) |
0 commit comments