|
| 1 | +import { expect, test } from 'vitest'; |
| 2 | +import { render } from '@testing-library/react'; |
| 3 | +import { composeStory } from '@storybook/react'; |
| 4 | +import type { RenderResult } from '@testing-library/react'; |
| 5 | +import * as stories from '../stories/svg.stories'; |
| 6 | +import { wait, waitForVideoToLoad } from './test-utils'; |
| 7 | + |
| 8 | +const allStoryNames = Object.keys(stories).filter(key => key !== 'default'); |
| 9 | + |
| 10 | +const storyTests: Record<string, (result: RenderResult) => void | Promise<void>> = { |
| 11 | + 'WorksWithSVGs': ({ container }) => { |
| 12 | + const svg = container.querySelector('svg'); |
| 13 | + expect(svg).not.toBeNull(); |
| 14 | + |
| 15 | + const texts = svg?.querySelectorAll('text'); |
| 16 | + expect(texts?.length).toBe(1); |
| 17 | + expect(texts?.[0].textContent).toBe('test'); |
| 18 | + expect(texts?.[0].getAttribute('fill')).toBe('red'); |
| 19 | + }, |
| 20 | + 'CanMoveContentAroundWithinSVGs': async ({ container, getByText }) => { |
| 21 | + const svg = container.querySelector('svg'); |
| 22 | + expect(svg).not.toBeNull(); |
| 23 | + |
| 24 | + const nestedSvgs = svg?.querySelectorAll('svg'); |
| 25 | + expect(nestedSvgs?.length).toBe(2); |
| 26 | + |
| 27 | + const firstSvg = nestedSvgs?.[0]; |
| 28 | + const secondSvg = nestedSvgs?.[1]; |
| 29 | + |
| 30 | + expect(firstSvg?.querySelector('text')).toBeNull(); |
| 31 | + expect(secondSvg?.querySelector('text')).not.toBeNull(); |
| 32 | + expect(secondSvg?.querySelector('text')?.textContent).toBe('test'); |
| 33 | + |
| 34 | + const button = getByText('Click to move the OutPortal within the SVG'); |
| 35 | + button.click(); |
| 36 | + await wait(10); |
| 37 | + |
| 38 | + expect(firstSvg?.querySelector('text')).not.toBeNull(); |
| 39 | + expect(firstSvg?.querySelector('text')?.textContent).toBe('test'); |
| 40 | + expect(secondSvg?.querySelector('text')).toBeNull(); |
| 41 | + }, |
| 42 | + 'PersistDOMWhileMovingWithinSVGs': async ({ container }) => { |
| 43 | + const video = container.querySelector('video') as HTMLVideoElement; |
| 44 | + expect(video).not.toBeNull(); |
| 45 | + expect(video.src).toContain('giphy.mp4'); |
| 46 | + |
| 47 | + await waitForVideoToLoad(video); |
| 48 | + expect(video.paused).toBe(true); |
| 49 | + await video.play(); |
| 50 | + await wait(200); |
| 51 | + |
| 52 | + expect(video.paused).toBe(false); |
| 53 | + const playbackPosition = video.currentTime; |
| 54 | + expect(playbackPosition).toBeGreaterThan(0); |
| 55 | + |
| 56 | + const button = container.querySelector('button') as HTMLButtonElement; |
| 57 | + expect(button).not.toBeNull(); |
| 58 | + button.click(); |
| 59 | + await wait(50); |
| 60 | + |
| 61 | + const videoAfterMove = container.querySelector('video') as HTMLVideoElement; |
| 62 | + expect(videoAfterMove).toBe(video); |
| 63 | + expect(videoAfterMove.paused).toBe(false); |
| 64 | + expect(videoAfterMove.currentTime).toBeGreaterThanOrEqual(playbackPosition); |
| 65 | + expect(videoAfterMove.currentTime).toBeGreaterThan(playbackPosition); |
| 66 | + }, |
| 67 | + 'CanPassAttributesOptionToCreateSvgPortalNode': async ({ container, getByText }) => { |
| 68 | + const svg = container.querySelector('svg'); |
| 69 | + expect(svg).not.toBeNull(); |
| 70 | + |
| 71 | + const nestedSvg = svg?.querySelector('svg'); |
| 72 | + expect(nestedSvg).not.toBeNull(); |
| 73 | + |
| 74 | + const portalWrapper = nestedSvg?.querySelector('g'); |
| 75 | + expect(portalWrapper).not.toBeNull(); |
| 76 | + expect(portalWrapper?.getAttribute('stroke')).toBeNull(); |
| 77 | + |
| 78 | + const button = getByText('Click to pass attributes option to the intermediary svg'); |
| 79 | + button.click(); |
| 80 | + await wait(10); |
| 81 | + |
| 82 | + const portalWrapperAfter = nestedSvg?.querySelector('g'); |
| 83 | + expect(portalWrapperAfter?.getAttribute('stroke')).toBe('blue'); |
| 84 | + }, |
| 85 | +}; |
| 86 | + |
| 87 | +test('all stories have tests', () => { |
| 88 | + const testedStories = Object.keys(storyTests); |
| 89 | + const untestedStories = allStoryNames.filter(name => !testedStories.includes(name)); |
| 90 | + |
| 91 | + if (untestedStories.length > 0) { |
| 92 | + throw new Error( |
| 93 | + `The following stories do not have tests:\n${untestedStories.map(s => ` - ${s}`).join('\n')}` |
| 94 | + ); |
| 95 | + } |
| 96 | +}); |
| 97 | + |
| 98 | +Object.entries(storyTests).forEach(([storyName, testFn]) => { |
| 99 | + test(storyName, async () => { |
| 100 | + const Story = composeStory( |
| 101 | + (stories as any)[storyName], |
| 102 | + stories.default |
| 103 | + ); |
| 104 | + const result = render(<Story />); |
| 105 | + await testFn(result); |
| 106 | + }); |
| 107 | +}); |
0 commit comments