|
| 1 | +import { expect, testStep, dtl } from '@epic-web/workshop-utils/test' |
| 2 | +const { screen, waitFor, fireEvent } = dtl |
| 3 | + |
| 4 | +import './index.tsx' |
| 5 | + |
| 6 | +function onLoad(img: HTMLElement, cb?: () => void) { |
| 7 | + return new Promise<void>((resolve) => { |
| 8 | + img.addEventListener( |
| 9 | + 'load', |
| 10 | + () => { |
| 11 | + cb?.() |
| 12 | + resolve() |
| 13 | + }, |
| 14 | + { once: true }, |
| 15 | + ) |
| 16 | + }) |
| 17 | +} |
| 18 | + |
| 19 | +await testStep('Initial ship details are loaded', async () => { |
| 20 | + await screen.findByRole('heading', { name: /Dreadnought/i }) |
| 21 | + await waitFor( |
| 22 | + () => expect(screen.queryAllByText('loading')).toHaveLength(0), |
| 23 | + { timeout: 5000 }, |
| 24 | + ) |
| 25 | + await onLoad(screen.getByAltText('Dreadnought')) |
| 26 | + await new Promise((resolve) => setTimeout(resolve, 250)) |
| 27 | +}) |
| 28 | + |
| 29 | +const img = await testStep('Initial image is loaded', async () => { |
| 30 | + const img = await screen.findByAltText('Dreadnought') |
| 31 | + expect(img, '🚨 Initial image should be present').toBeInTheDocument() |
| 32 | + return img |
| 33 | +}) |
| 34 | + |
| 35 | +const initialSrc = img.getAttribute('src') |
| 36 | + |
| 37 | +let loadTime = 0, |
| 38 | + srcChangeTime = 0 |
| 39 | + |
| 40 | +const loadAndSrcChange = Promise.all([ |
| 41 | + onLoad(img).then(() => { |
| 42 | + loadTime = Date.now() |
| 43 | + }), |
| 44 | + waitFor( |
| 45 | + () => { |
| 46 | + expect(img.getAttribute('src')).not.toBe(initialSrc) |
| 47 | + srcChangeTime = Date.now() |
| 48 | + }, |
| 49 | + { timeout: 4000 }, |
| 50 | + ), |
| 51 | +]) |
| 52 | + |
| 53 | +await testStep('Click on a different ship', async () => { |
| 54 | + fireEvent.click(screen.getByRole('button', { name: 'Interceptor' })) |
| 55 | +}) |
| 56 | + |
| 57 | +await testStep('New ship details are loaded', async () => { |
| 58 | + await screen.findByRole( |
| 59 | + 'heading', |
| 60 | + { name: /Interceptor/i }, |
| 61 | + { timeout: 4000 }, |
| 62 | + ) |
| 63 | +}) |
| 64 | + |
| 65 | +const newImg = await testStep('New image element is present', async () => { |
| 66 | + const img = await screen.findByAltText('Interceptor') |
| 67 | + expect(img, '🚨 New image element should be present').toBeInTheDocument() |
| 68 | + return img |
| 69 | +}) |
| 70 | + |
| 71 | +await testStep('Verify image src has changed', async () => { |
| 72 | + await waitFor(() => { |
| 73 | + expect( |
| 74 | + newImg.getAttribute('src'), |
| 75 | + '🚨 Image src should change after load event', |
| 76 | + ).not.toBe(initialSrc) |
| 77 | + expect( |
| 78 | + newImg.getAttribute('src'), |
| 79 | + '🚨 New image src should contain "interceptor"', |
| 80 | + ).toMatch(/interceptor/) |
| 81 | + }) |
| 82 | +}) |
| 83 | + |
| 84 | +// put this last so it doesn't affect the other steps |
| 85 | +await testStep('img was loaded before the src changed.', async () => { |
| 86 | + await loadAndSrcChange |
| 87 | + console.log(loadTime - srcChangeTime) |
| 88 | + expect( |
| 89 | + loadTime - srcChangeTime, |
| 90 | + '🚨 the img should be loaded before the src changes. Make sure to suspend until the img is loaded.', |
| 91 | + ).toBeLessThan(4) |
| 92 | +}) |
0 commit comments