|
| 1 | +import { nextTestSetup } from 'e2e-utils' |
| 2 | +import { retry } from 'next-test-utils' |
| 3 | + |
| 4 | +describe('next-image-events', () => { |
| 5 | + const { next } = nextTestSetup({ |
| 6 | + files: __dirname, |
| 7 | + }) |
| 8 | + |
| 9 | + it('should not call onLoad multiple times', async () => { |
| 10 | + const imageRequests = [] |
| 11 | + const browser = await next.browser('/fulfilled', { |
| 12 | + beforePageLoad(page) { |
| 13 | + page.on('request', (request) => { |
| 14 | + if (request.resourceType() === 'image') { |
| 15 | + imageRequests.push(request.url()) |
| 16 | + } |
| 17 | + }) |
| 18 | + }, |
| 19 | + }) |
| 20 | + |
| 21 | + let logsIdx = 0 |
| 22 | + await retry(async () => { |
| 23 | + const logs = await browser.log() |
| 24 | + expect( |
| 25 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 26 | + ).toEqual([ |
| 27 | + { |
| 28 | + source: 'error', |
| 29 | + message: 'hydrated image load', |
| 30 | + }, |
| 31 | + ]) |
| 32 | + logsIdx = logs.length |
| 33 | + }) |
| 34 | + expect(imageRequests).toEqual([expect.stringContaining('test')]) |
| 35 | + imageRequests.length = 0 |
| 36 | + |
| 37 | + await browser.locator(':text("Show Client image")').click() |
| 38 | + |
| 39 | + await retry(async () => { |
| 40 | + const logs = await browser.log() |
| 41 | + expect( |
| 42 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 43 | + ).toEqual([ |
| 44 | + { |
| 45 | + source: 'error', |
| 46 | + message: 'client rendered image load', |
| 47 | + }, |
| 48 | + ]) |
| 49 | + logsIdx = logs.length |
| 50 | + }) |
| 51 | + expect(imageRequests).toEqual([expect.stringContaining('test')]) |
| 52 | + imageRequests.length = 0 |
| 53 | + |
| 54 | + await browser.locator(':text("rerender Page")').click() |
| 55 | + |
| 56 | + const logs = await browser.log() |
| 57 | + expect( |
| 58 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 59 | + ).toEqual([]) |
| 60 | + expect(imageRequests).toEqual([]) |
| 61 | + imageRequests.length = 0 |
| 62 | + }) |
| 63 | + |
| 64 | + it('should not infinitely retry on error', async () => { |
| 65 | + const nextImageRequests: string[] = [] |
| 66 | + let logsIdx = 0 |
| 67 | + const browser = await next.browser('/rejected', { |
| 68 | + beforePageLoad(page) { |
| 69 | + // We're manually aborting here to simulate an error before React hydrates. |
| 70 | + // A real request might settle too late to test this behavior reliably. |
| 71 | + // Especially in dev, requests (even if warm) may take longer than hydration. |
| 72 | + page.route( |
| 73 | + /\/will-never-exist\.png|\/still-doesnt-exist\.png/, |
| 74 | + (route) => { |
| 75 | + nextImageRequests.push(route.request().url()) |
| 76 | + return route.abort() |
| 77 | + } |
| 78 | + ) |
| 79 | + }, |
| 80 | + }) |
| 81 | + |
| 82 | + await retry(async () => { |
| 83 | + const logs = await browser.log() |
| 84 | + expect( |
| 85 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 86 | + ).toEqual([ |
| 87 | + { |
| 88 | + source: 'error', |
| 89 | + message: 'Failed to load resource: net::ERR_FAILED', |
| 90 | + }, |
| 91 | + // Next.js retries once to trigger onError on SSRed, settled images. |
| 92 | + // If this test fails, we'll either hydrated faster than the request settled, |
| 93 | + // or dropped the retrying behavior of next/image |
| 94 | + { |
| 95 | + source: 'error', |
| 96 | + message: 'Failed to load resource: net::ERR_FAILED', |
| 97 | + }, |
| 98 | + { |
| 99 | + source: 'error', |
| 100 | + message: 'hydrated image error', |
| 101 | + }, |
| 102 | + ]) |
| 103 | + logsIdx = logs.length |
| 104 | + }) |
| 105 | + expect(nextImageRequests).toEqual([ |
| 106 | + expect.stringContaining('will-never-exist'), |
| 107 | + // Next.js retries once to trigger onError on SSRed, settled images. |
| 108 | + // If this test fails, we'll either hydrated faster than the request settled, |
| 109 | + // or dropped the retrying behavior of next/image |
| 110 | + expect.stringContaining('will-never-exist'), |
| 111 | + ]) |
| 112 | + nextImageRequests.length = 0 |
| 113 | + |
| 114 | + await browser.locator(':text("Show Client image")').click() |
| 115 | + |
| 116 | + await retry(async () => { |
| 117 | + const logs = await browser.log() |
| 118 | + expect( |
| 119 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 120 | + ).toEqual([ |
| 121 | + { |
| 122 | + source: 'error', |
| 123 | + message: 'Failed to load resource: net::ERR_FAILED', |
| 124 | + }, |
| 125 | + { |
| 126 | + source: 'error', |
| 127 | + message: 'client rendered image error', |
| 128 | + }, |
| 129 | + ]) |
| 130 | + logsIdx = logs.length |
| 131 | + }) |
| 132 | + expect(nextImageRequests).toEqual([ |
| 133 | + expect.stringContaining('still-doesnt-exist'), |
| 134 | + ]) |
| 135 | + nextImageRequests.length = 0 |
| 136 | + |
| 137 | + await browser.locator(':text("rerender Page")').click() |
| 138 | + |
| 139 | + const logs = await browser.log() |
| 140 | + expect( |
| 141 | + logs.slice(logsIdx).filter(({ source }) => source === 'error') |
| 142 | + ).toEqual([]) |
| 143 | + expect(nextImageRequests).toEqual([]) |
| 144 | + nextImageRequests.length = 0 |
| 145 | + logsIdx = logs.length |
| 146 | + }) |
| 147 | +}) |
0 commit comments