Skip to content

Commit abc6dca

Browse files
authored
feat(gatsby-plugin-image): add check for node.gatsbyImage in the getImage helper (#35507)
* add check for node.gatsbyImage in the getImage helper * add unit tests
1 parent e51c3a3 commit abc6dca

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

packages/gatsby-plugin-image/src/components/__tests__/hooks.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,19 @@ const node: Node = {
2525
},
2626
}
2727

28-
const dataParent = {
28+
const imageDataParent = {
2929
...node,
3030
gatsbyImageData: imageData,
3131
}
3232

33+
const imageParent = {
34+
...node,
35+
gatsbyImage: imageData,
36+
}
37+
3338
const fileNode = {
3439
...node,
35-
childImageSharp: dataParent,
40+
childImageSharp: imageDataParent,
3641
}
3742

3843
const getImageDataArgs: IGetImageDataArgs = {
@@ -147,13 +152,21 @@ describe(`The image helper functions`, () => {
147152
it(`returns the same data if passed gatsbyImageData`, () => {
148153
expect(getImage(imageData)).toEqual(imageData)
149154
})
155+
it(`returns the same data if passed gatsbyImage`, () => {
156+
expect(getImage(imageData)).toEqual(imageData)
157+
})
150158

151159
it(`gets an image from a FileNode`, () => {
152160
expect(getImage(fileNode)?.images.fallback?.src).toEqual(`imagesrc.jpg`)
153161
})
154162

155-
it(`gets an image from an IGatsbyImageDataParent`, () => {
156-
expect(getImage(dataParent)?.images.fallback?.src).toEqual(`imagesrc.jpg`)
163+
it(`gets an image from an IGatsbyImageDataParent/IGatsbyImageParent`, () => {
164+
expect(getImage(imageDataParent)?.images.fallback?.src).toEqual(
165+
`imagesrc.jpg`
166+
)
167+
expect(getImage(imageParent)?.images.fallback?.src).toEqual(
168+
`imagesrc.jpg`
169+
)
157170
})
158171
it(`returns undefined from an invalid object`, () => {
159172
expect(getImage(node)).toBeUndefined()
@@ -177,8 +190,9 @@ describe(`The image helper functions`, () => {
177190
expect(getSrc(fileNode)).toEqual(`imagesrc.jpg`)
178191
})
179192

180-
it(`gets src from an IGatsbyImageDataParent`, () => {
181-
expect(getSrc(dataParent)).toEqual(`imagesrc.jpg`)
193+
it(`gets src from an IGatsbyImageDataParent/IGatsbyImageParent`, () => {
194+
expect(getSrc(imageDataParent)).toEqual(`imagesrc.jpg`)
195+
expect(getSrc(imageParent)).toEqual(`imagesrc.jpg`)
182196
})
183197

184198
it(`returns undefined from an invalid object`, () => {
@@ -202,8 +216,9 @@ describe(`The image helper functions`, () => {
202216
expect(getSrcSet(fileNode)).toEqual(`imagesrcset.jpg 1x`)
203217
})
204218

205-
it(`gets srcSet from an IGatsbyImageDataParent`, () => {
206-
expect(getSrcSet(dataParent)).toEqual(`imagesrcset.jpg 1x`)
219+
it(`gets srcSet from an IGatsbyImageDataParent/IGatsbyImageParent`, () => {
220+
expect(getSrcSet(imageDataParent)).toEqual(`imagesrcset.jpg 1x`)
221+
expect(getSrcSet(imageParent)).toEqual(`imagesrcset.jpg 1x`)
207222
})
208223

209224
it(`returns undefined from an invalid object`, () => {

packages/gatsby-plugin-image/src/components/hooks.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ export function hasImageLoaded(cacheKey: string): boolean {
4545
export type IGatsbyImageDataParent<T = never> = T & {
4646
gatsbyImageData: IGatsbyImageData
4747
}
48+
export type IGatsbyImageParent<T = never> = T & {
49+
gatsbyImage: IGatsbyImageData
50+
}
4851
export type FileNode = Node & {
4952
childImageSharp?: IGatsbyImageDataParent<Node>
5053
}
@@ -61,14 +64,29 @@ const isGatsbyImageDataParent = <T>(
6164
node: IGatsbyImageDataParent<T> | any
6265
): node is IGatsbyImageDataParent<T> => Boolean(node?.gatsbyImageData)
6366

64-
export type ImageDataLike = FileNode | IGatsbyImageDataParent | IGatsbyImageData
67+
const isGatsbyImageParent = <T>(
68+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
69+
node: IGatsbyImageParent<T> | any
70+
): node is IGatsbyImageParent<T> => Boolean(node?.gatsbyImage)
71+
72+
export type ImageDataLike =
73+
| FileNode
74+
| IGatsbyImageDataParent
75+
| IGatsbyImageParent
76+
| IGatsbyImageData
77+
6578
export const getImage = (node: ImageDataLike): IGatsbyImageData | undefined => {
6679
if (isGatsbyImageData(node)) {
6780
return node
6881
}
82+
// gatsbyImageData GraphQL field
6983
if (isGatsbyImageDataParent(node)) {
7084
return node.gatsbyImageData
7185
}
86+
// gatsbyImage GraphQL field for Gatsby's Image CDN service
87+
if (isGatsbyImageParent(node)) {
88+
return node.gatsbyImage
89+
}
7290
return node?.childImageSharp?.gatsbyImageData
7391
}
7492

0 commit comments

Comments
 (0)