Skip to content

Commit 4618aeb

Browse files
sarahetterascorbic
andauthored
chore: Remote patterns tests (#1434)
* feat: add image for remotePatterns example * test: add test for image config * test: add cypress test for remotePattern in default project * chore: fix import * test: add test for broken image * test: trying non netlify image * chore: removing image because everything else isnt loading now? * chore: add back image and fix test brackets * chore: stringify and scroll * chore: missed git add on a file * chore: just first img * chore: remove log and extra param * chore: test for height and width as next adds these * chore: add px Co-authored-by: Matt Kane <[email protected]>
1 parent ad5d8e4 commit 4618aeb

File tree

4 files changed

+54
-3
lines changed

4 files changed

+54
-3
lines changed

cypress/integration/default/images.spec.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,27 @@ describe('next/images', () => {
1717
cy.visit('/image')
1818
cy.findByRole('img', { name: /shiba inu dog looks through a window/i })
1919
})
20-
})
20+
21+
it('should show image allow-listed with remotePatterns', () => {
22+
cy.visit('/image')
23+
cy.findByRole('img',{ name: /tawny frogmouth/i }).should('be.visible').and(($img) => {
24+
// "naturalWidth" and "naturalHeight" are set when the image loads
25+
expect(
26+
$img[0].naturalWidth,
27+
'image has natural width'
28+
).to.be.greaterThan(0)
29+
})
30+
})
31+
32+
it('should show a broken image if it is not on domains or remotePatterns allowlist', () => {
33+
cy.visit('/image')
34+
cy.findByRole('img',{ name: /jellybeans/i }).should('be.visible').and(($img) => {
35+
expect(
36+
$img[0].style.height
37+
).to.equal('0px')
38+
expect(
39+
$img[0].style.width
40+
).to.equal('0px')
41+
})
42+
})
43+
})

demos/default/next.config.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,13 @@ module.exports = {
7777
sassOptions: {
7878
includePaths: [path.join(__dirname, 'styles-sass-test')],
7979
},
80+
experimental: {
81+
images: {
82+
remotePatterns: [
83+
{
84+
hostname: '*.imgur.com',
85+
},
86+
],
87+
},
88+
},
8089
}

demos/default/pages/image.js

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ const Images = () => (
1414
</p>
1515
<p>
1616
<Image src={logo} alt="netlify logomark" />
17-
1817
<Image
1918
src="https://raw.githubusercontent.com/netlify/netlify-plugin-nextjs/main/next-on-netlify.png"
2019
alt="Picture of the author"
2120
width={500}
2221
height={500}
2322
/>
23+
<Image src="https://i.imgur.com/bxSRS3Jb.png" alt="Tawny Frogmouth" width={160} height={160} />
24+
</p>
25+
26+
<p>
27+
The following image should be broken as the domain is not added to domains or remotePatterns in next.config.js:
2428
</p>
29+
<Image
30+
src="https://upload.wikimedia.org/wikipedia/commons/thumb/b/b6/SIPI_Jelly_Beans_4.1.07.tiff/lossy-page1-256px-SIPI_Jelly_Beans_4.1.07.tiff.jpg"
31+
alt="Jellybeans"
32+
width={146}
33+
height={32}
34+
/>
2535
</div>
2636
)
2737

test/index.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const { downloadFile } = require('../plugin/src/templates/handlerUtils')
1616

1717
const plugin = require('../plugin/src')
1818

19-
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME } = require('../plugin/src/constants')
19+
const { HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, IMAGE_FUNCTION_NAME } = require('../plugin/src/constants')
2020
const { join } = require('pathe')
2121
const {
2222
matchMiddleware,
@@ -527,6 +527,15 @@ describe('onBuild()', () => {
527527
// The function is supposed to return undefined, but we want to check if it throws
528528
expect(await plugin.onBuild(defaultArgs)).toBeUndefined()
529529
})
530+
531+
test('generates imageconfig file with entries for domains and remotePatterns', async () => {
532+
await moveNextDist()
533+
await plugin.onBuild(defaultArgs)
534+
const imageConfigPath = path.join(constants.INTERNAL_FUNCTIONS_SRC, IMAGE_FUNCTION_NAME, 'imageconfig.json')
535+
const imageConfigJson = await readJson(imageConfigPath)
536+
expect(imageConfigJson.domains.length).toBe(1)
537+
expect(imageConfigJson.remotePatterns.length).toBe(1)
538+
})
530539
})
531540

532541
describe('onPostBuild', () => {

0 commit comments

Comments
 (0)