Skip to content

Commit d24268f

Browse files
axe312gerwardpeet
andauthored
fix(gatsby-source-contentful): fix base64 aspect ratio (#33533)
Co-authored-by: Ward Peeters <[email protected]>
1 parent c7efdb9 commit d24268f

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed
Loading
Loading

packages/gatsby-source-contentful/src/__tests__/gatsby-plugin-image.js

+74-20
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
// @ts-check
22
import fs from "fs-extra"
3-
import { fetchRemoteFile } from "gatsby-core-utils"
3+
import _ from "lodash"
4+
import nock from "nock"
5+
import path from "path"
46
import { generateImageSource, getBase64Image } from "../gatsby-plugin-image"
7+
import * as coreUtils from "gatsby-core-utils"
58

6-
jest.mock(`gatsby-core-utils`)
7-
jest.mock(`fs-extra`)
9+
nock.disableNetConnect()
10+
11+
const FIXTURES = path.resolve(__dirname, `..`, `__fixtures__`)
12+
13+
const createMockCache = () => {
14+
const actualCacheMap = new Map()
15+
return {
16+
get: jest.fn(key => Promise.resolve(_.cloneDeep(actualCacheMap.get(key)))),
17+
set: jest.fn((key, value) => actualCacheMap.set(key, value)),
18+
directory: __dirname,
19+
actualMap: actualCacheMap,
20+
}
21+
}
22+
23+
const cache = createMockCache()
24+
25+
const fetchRemoteFileSpy = jest.spyOn(coreUtils, `fetchRemoteFile`)
826

927
describe(`contentful extend node type`, () => {
1028
describe(`generateImageSource`, () => {
@@ -65,12 +83,7 @@ describe(`contentful extend node type`, () => {
6583
})
6684

6785
describe(`getBase64Image`, () => {
68-
beforeEach(() => {
69-
// @ts-ignore
70-
fetchRemoteFile.mockClear()
71-
// @ts-ignore
72-
fs.readFile.mockResolvedValue(Buffer.from(`test`))
73-
})
86+
afterEach(() => nock.cleanAll())
7487

7588
const imageProps = {
7689
aspectRatio: 4.8698224852071,
@@ -97,19 +110,60 @@ describe(`contentful extend node type`, () => {
97110
},
98111
}
99112
test(`keeps image format`, async () => {
100-
const result = await getBase64Image(imageProps)
101-
102-
expect(fetchRemoteFile).toHaveBeenCalled()
103-
expect(result).toMatchInlineSnapshot(`"data:image/png;base64,dGVzdA=="`)
113+
nock(`https://images.ctfassets.net:443`)
114+
.get(
115+
`/k8iqpp6u0ior/3ljGfnpegOnBTFGhV07iC1/94257340bda15ad4ca8462da3a8afa07/347966-contentful-logo-wordmark-dark__1_-4cd185-original-1582664935__1_.png`
116+
)
117+
.query({ w: `20`, h: `4`, q: `50` })
118+
.reply(
119+
200,
120+
fs.readFileSync(`${FIXTURES}/contentful-base64.png`, null),
121+
[
122+
`Content-Type`,
123+
`image/png`,
124+
`Content-Length`,
125+
`355`,
126+
`Access-Control-Allow-Origin`,
127+
`*`,
128+
]
129+
)
130+
const result = await getBase64Image(imageProps, cache)
131+
expect(fetchRemoteFileSpy.mock.calls[0][0].url).toBe(
132+
`https://images.ctfassets.net/k8iqpp6u0ior/3ljGfnpegOnBTFGhV07iC1/94257340bda15ad4ca8462da3a8afa07/347966-contentful-logo-wordmark-dark__1_-4cd185-original-1582664935__1_.png?w=20&h=4&q=50`
133+
)
134+
expect(result).toMatch(
135+
`data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABMAAAAECAMAAABbXfTdAAAAh1BMVEUAAABEl785ruI7tOc7tOcqMDkqMDkqMDkqMDnby3FEmME7tOc7tOcqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDn3vl/cVmDtXGjtXGgqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDkqMDnbVWDqWWftXGjtXGilAiI/AAAALXRSTlMAUt/ZNiQhOC/bFh4dhm6FnaR+qZiKmZqJsdwWHRyIdqclqIA9nIdWslLf2jb4BEMwAAAACXBIWXMAAAsSAAALEgHS3X78AAAAB3RJTUUH5QgNCSUh4wxIuwAAADZJREFUCNdjZASCPwxAwPobSLAxMv5gY+FghIC/LFDGW2EWQTDjgwBI+K04iORn1ARSVxhQAAC2HQpRHmHx6QAAAABJRU5ErkJggg==`
136+
)
137+
expect(nock.pendingMocks).toHaveLength(0)
104138
})
105139
test(`uses given image format`, async () => {
106-
const result = await getBase64Image({
107-
...imageProps,
108-
options: { ...imageProps.options, toFormat: `jpg` },
109-
})
110-
111-
expect(fetchRemoteFile).toHaveBeenCalled()
112-
expect(result).toMatchInlineSnapshot(`"data:image/jpg;base64,dGVzdA=="`)
140+
nock(`https://images.ctfassets.net:443`)
141+
.get(
142+
`/k8iqpp6u0ior/3ljGfnpegOnBTFGhV07iC1/94257340bda15ad4ca8462da3a8afa07/347966-contentful-logo-wordmark-dark__1_-4cd185-original-1582664935__1_.png`
143+
)
144+
.query({ w: `20`, h: `4`, q: `50`, fm: `jpg` })
145+
.reply(
146+
200,
147+
fs.readFileSync(`${FIXTURES}/contentful-base64.jpg`, null),
148+
[
149+
`Content-Type`,
150+
`image/jpeg`,
151+
`Content-Length`,
152+
`356`,
153+
`Access-Control-Allow-Origin`,
154+
`*`,
155+
]
156+
)
157+
const result = await getBase64Image(
158+
{
159+
...imageProps,
160+
options: { ...imageProps.options, toFormat: `jpg` },
161+
},
162+
cache
163+
)
164+
expect(result).toMatch(
165+
`data:image/jpg;base64,/9j/4AAQSkZJRgABAQIAHAAcAAD/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkzODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P/2wBDARESEhgVGC8aGi9jQjhCY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P/wAARCAAEABMDASIAAhEBAxEB/8QAFwABAAMAAAAAAAAAAAAAAAAAAAIDBv/EACIQAAAFAwQDAAAAAAAAAAAAAAABAgMRBBIhBRMUMUFRof/EABUBAQEAAAAAAAAAAAAAAAAAAAIB/8QAFxEBAQEBAAAAAAAAAAAAAAAAAQACMf/aAAwDAQACEQMRAD8A1dI1zG3S3HGLFmmWFW3dZP2YuPS0ndNXWQrxu4LMgAmeS2BpCg5o6XFmrm1qeihLsEXwAAKN/9k=`
166+
)
113167
})
114168
})
115169
})

packages/gatsby-source-contentful/src/gatsby-plugin-image.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export const getBase64Image = (imageProps, cache) => {
3535
...imageProps.options,
3636
toFormat,
3737
width: 20,
38-
height: Math.floor(20 * aspectRatio),
38+
height: Math.floor(20 / aspectRatio),
3939
}
4040

4141
const requestUrl = createUrl(imageProps.baseUrl, imageOptions)

0 commit comments

Comments
 (0)