Skip to content

Commit 87dfb1a

Browse files
committed
test: add test coverage
1 parent b316864 commit 87dfb1a

File tree

2 files changed

+55
-5
lines changed

2 files changed

+55
-5
lines changed

test/helpers/utils.spec.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import Chance from 'chance'
2+
import { getCustomImageResponseHeaders } from '../../plugin/src/helpers/utils'
3+
4+
const chance = new Chance()
5+
6+
describe('getCustomImageResponseHeaders', () => {
7+
it('returns null when no custom image response headers are found', () => {
8+
const mockHeaders = [{
9+
for: '/test',
10+
values: {
11+
'X-Foo': chance.string()
12+
}
13+
}]
14+
15+
expect(getCustomImageResponseHeaders(mockHeaders)).toBe(null)
16+
})
17+
18+
it('returns header values when custom image response headers are found', () => {
19+
const mockFooValue = chance.string()
20+
21+
const mockHeaders = [{
22+
for: '/_next/image/',
23+
values: {
24+
'X-Foo': mockFooValue
25+
}
26+
}]
27+
28+
const result = getCustomImageResponseHeaders(mockHeaders)
29+
expect(result).toStrictEqual({
30+
'X-Foo': mockFooValue,
31+
})
32+
})
33+
})

test/index.js

+22-5
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ const {
3333
} = require('../plugin/src/helpers/config')
3434
const { dirname } = require('path')
3535
const { getProblematicUserRewrites } = require('../plugin/src/helpers/verification')
36-
const { onPostBuild } = require('../plugin/lib')
37-
const { basePath } = require('../demos/next-i18next/next.config')
3836

3937
const chance = new Chance()
4038
const FIXTURES_DIR = `${__dirname}/fixtures`
@@ -455,7 +453,7 @@ describe('onBuild()', () => {
455453
'.next/static/chunks/webpack-middleware*.js',
456454
'!.next/server/**/*.js.nft.json',
457455
".next/static/css/1152424140993be6.css",
458-
".next/static/css/84099ae0bbc955fa.css",
456+
".next/static/css/84099ae0bbc955fa.css",
459457
'!../../node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
460458
`!node_modules/next/dist/server/lib/squoosh/**/*.wasm`,
461459
`!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm`,
@@ -530,13 +528,32 @@ describe('onBuild()', () => {
530528
expect(await plugin.onBuild(defaultArgs)).toBeUndefined()
531529
})
532530

533-
test('generates imageconfig file with entries for domains and remotePatterns', async () => {
531+
test('generates imageconfig file with entries for domains, remotePatterns, and custom response headers', async () => {
534532
await moveNextDist()
535-
await plugin.onBuild(defaultArgs)
533+
const mockHeaderValue = chance.string()
534+
535+
const updatedArgs = {
536+
...defaultArgs,
537+
netlifyConfig: {
538+
...defaultArgs.netlifyConfig,
539+
headers: [{
540+
for: '/_next/image/',
541+
values: {
542+
'X-Foo': mockHeaderValue
543+
}
544+
}]
545+
}
546+
}
547+
await plugin.onBuild(updatedArgs)
548+
536549
const imageConfigPath = path.join(constants.INTERNAL_FUNCTIONS_SRC, IMAGE_FUNCTION_NAME, 'imageconfig.json')
537550
const imageConfigJson = await readJson(imageConfigPath)
551+
538552
expect(imageConfigJson.domains.length).toBe(1)
539553
expect(imageConfigJson.remotePatterns.length).toBe(1)
554+
expect(imageConfigJson.responseHeaders).toStrictEqual({
555+
'X-Foo': mockHeaderValue
556+
})
540557
})
541558
})
542559

0 commit comments

Comments
 (0)