Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/constrained.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
describe(`constrained`, () => {
beforeEach(() => {
cy.visit(`/constrained`).waitForRouteChange()
})

it(`renders a spacer svg`, () => {
cy.getTestElement(`image-constrained`)
.find(`img[role=presentation]`)
.should(`have.attr`, `src`)
.and(`match`, /svg\+xml/)
})

it(`includes sizes attribute with default width`, () => {
cy.getTestElement(`image-constrained`)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.and(`equal`, `(min-width: 4015px) 4015px, 100vw`)
})

it(`includes sizes attribute with specified width`, () => {
cy.getTestElement(`image-constrained-limit`)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.and(`equal`, `(min-width: 500px) 500px, 100vw`)
})
})
6 changes: 6 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/fixed.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ describe(`fixed`, () => {
.should(`exist`)
})

it(`includes sizes attribute`, () => {
cy.getTestElement(fixedTestId)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.should(`equal`, `500px`)
})
})
7 changes: 7 additions & 0 deletions e2e-tests/gatsby-static-image/cypress/integration/fluid.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,11 @@ describe(`fluid`, () => {
.should(`have.attr`, `style`)
.and(`match`, /padding/)
})

it(`includes sizes attribute`, () => {
cy.getTestElement(fluidTestId)
.find(`[data-main-image]`)
.should(`have.attr`, `sizes`)
.should(`equal`, `100vw`)
})
})
16 changes: 7 additions & 9 deletions e2e-tests/gatsby-static-image/src/pages/constrained.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ import Layout from "../components/layout"

const FluidPage = () => (
<Layout>
<div data-testid="image-fluid">
<div data-testid="image-constrained">
<StaticImage src="../images/citrus-fruits.jpg" alt="Citrus fruits" />
</div>
<div data-testid="image-fluid-png">
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon" />
</div>
<div data-testid="image-fluid-relative">
<StaticImage src="../../content/relative.jpg" alt="Citrus fruits" />
</div>
<div data-testid="invalid-image">
<StaticImage src="./does-not-exist.jpg" />
<div data-testid="image-constrained-limit">
<StaticImage
src="../images/citrus-fruits.jpg"
maxWidth={500}
alt="Citrus fruits"
/>
</div>
</Layout>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react"
import { GatsbyImage, ISharpGatsbyImageData } from "../gatsby-image.browser"
import { GatsbyImage, IGatsbyImageData } from "../gatsby-image.browser"
import { render, waitFor } from "@testing-library/react"
import * as hooks from "../hooks"

Expand All @@ -14,7 +14,7 @@ jest.mock(`../../../macros/terser.macro`, () => (strs): string => strs.join(``))

describe(`GatsbyImage browser`, () => {
let beforeHydrationContent: HTMLDivElement
let image: ISharpGatsbyImageData
let image: IGatsbyImageData

beforeEach(() => {
console.warn = jest.fn()
Expand All @@ -27,9 +27,9 @@ describe(`GatsbyImage browser`, () => {
width: 100,
height: 100,
layout: `fluid`,
images: { fallback: { src: `some-src-fallback.jpg` } },
images: { fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` } },
placeholder: { sources: [] },
sizes: `192x192`,

backgroundColor: `red`,
}

Expand Down Expand Up @@ -168,7 +168,7 @@ describe(`GatsbyImage browser`, () => {
expect(onStartLoadSpy).toBeCalledWith({ wasCached: false })
expect(onLoadSpy).toBeCalled()
expect(hooks.storeImageloaded).toBeCalledWith(
`{"fallback":{"src":"some-src-fallback.jpg"}}`
`{"fallback":{"src":"some-src-fallback.jpg","sizes":"192x192"}}`
)
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ describe(`GatsbyImage server`, () => {
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -186,22 +185,22 @@ describe(`GatsbyImage server`, () => {
data-main-image=""
decoding="async"
loading="lazy"
sizes="192x192"
style="opacity: 0;"
/>
`)
})

it(`has a valid src value when fallback is provided in images`, () => {
const images = { fallback: { src: `some-src-fallback.jpg` } }
const images = {
fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` },
}

const image: IGatsbyImageData = {
width: 100,
height: 100,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down Expand Up @@ -233,6 +232,7 @@ icon64px.png 64w,
icon-retina.png 2x,
icon-ultra.png 3x,
icon.svg`,
sizes: `192x192`,
},
}

Expand All @@ -242,7 +242,6 @@ icon.svg`,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down Expand Up @@ -278,7 +277,6 @@ icon.svg`,
layout: `constrained`,
images,
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -294,7 +292,6 @@ icon.svg`,
data-main-image=""
decoding="async"
loading="lazy"
sizes="192x192"
style="opacity: 0;"
/>
`)
Expand All @@ -317,9 +314,11 @@ icon.svg`,
width: 100,
height: 100,
layout: `constrained`,
images: { sources },
images: {
sources,
fallback: { src: `some-src-fallback.jpg`, sizes: `192x192` },
},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -339,6 +338,7 @@ icon.svg`,
alt="A fake image for testing purpose"
data-gatsby-image-ssr=""
data-main-image=""
data-src="some-src-fallback.jpg"
decoding="async"
loading="lazy"
sizes="192x192"
Expand All @@ -357,7 +357,6 @@ icon.svg`,
layout: `fluid`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -383,7 +382,6 @@ icon.svg`,
layout: `fixed`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand All @@ -409,7 +407,6 @@ icon.svg`,
layout: `constrained`,
images: {},
placeholder: { sources: [] },
sizes: `192x192`,
backgroundColor: `red`,
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ export interface IGatsbyImageData {
layout: Layout
height?: number
backgroundColor?: string
sizes?: string
images: Pick<MainImageProps, "sources" | "fallback">
placeholder?: Pick<PlaceholderProps, "sources" | "fallback">
width?: number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
layout,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
layout,
layout,
fallback,

images,
placeholder,
sizes,
backgroundColor: placeholderBackgroundColor,
} = image

Expand All @@ -65,7 +64,7 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI
}
if (images.fallback) {
cleanedImages.fallback = {
src: images.fallback.src,
...images.fallback,
Comment on lines 65 to +67
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (images.fallback) {
cleanedImages.fallback = {
src: images.fallback.src,
...images.fallback,
if (fallback) {
cleanedImages.fallback = {
...fallback,

srcSet: images.fallback.srcSet
? removeNewLines(images.fallback.srcSet)
: undefined,
Expand Down Expand Up @@ -106,7 +105,6 @@ export const GatsbyImage: FunctionComponent<GatsbyImageProps> = function GatsbyI

<MainImage
data-gatsby-image-ssr=""
sizes={sizes}
className={imgClassName}
style={imgStyle}
{...(props as Omit<MainImageProps, "images" | "fallback">)}
Expand Down