diff --git a/packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap
index 0936ecb7472dd..5f87b56af2fe4 100644
--- a/packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap
+++ b/packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap
@@ -176,7 +176,7 @@ exports[` should render multiple fixed image variants 1`] = `
aria-hidden="true"
class="placeholder"
itemprop="item-prop-for-the-image"
- src="other_string_of_base64"
+ src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
title="Title for the image"
/>
@@ -204,15 +204,15 @@ exports[` should render multiple fixed image variants 1`] = `
height="100"
itemprop="item-prop-for-the-image"
loading="lazy"
- src="test_image_2.jpg"
- srcset="some other srcSet"
+ src="test_image.jpg"
+ srcset="some srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
width="100"
/>
@@ -278,14 +278,14 @@ exports[` should render multiple fluid image variants 1`] = `
itemprop="item-prop-for-the-image"
loading="lazy"
sizes="(max-width: 600px) 100vw, 600px"
- src="test_image_2.jpg"
- srcset="some other srcSet"
+ src="test_image.jpg"
+ srcset="some srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
/>
diff --git a/packages/gatsby-image/src/__tests__/index.js b/packages/gatsby-image/src/__tests__/index.js
index c92973fa700fd..34c6e1c13595b 100644
--- a/packages/gatsby-image/src/__tests__/index.js
+++ b/packages/gatsby-image/src/__tests__/index.js
@@ -33,8 +33,8 @@ const fixedImagesShapeMock = [
base64: `string_of_base64`,
},
{
- width: 100,
- height: 100,
+ width: 300,
+ height: 300,
src: `test_image_2.jpg`,
srcSet: `some other srcSet`,
srcSetWebp: `some other srcSetWebp`,
@@ -45,7 +45,7 @@ const fixedImagesShapeMock = [
const fluidImagesShapeMock = [
{
- aspectRatio: 1.5,
+ aspectRatio: 2,
src: `test_image.jpg`,
srcSet: `some srcSet`,
srcSetWebp: `some srcSetWebp`,
@@ -53,7 +53,7 @@ const fluidImagesShapeMock = [
base64: `string_of_base64`,
},
{
- aspectRatio: 2,
+ aspectRatio: 3,
src: `test_image_2.jpg`,
srcSet: `some other srcSet`,
srcSetWebp: `some other srcSetWebp`,
@@ -121,6 +121,7 @@ const setupImages = (
describe(``, () => {
const OLD_MATCH_MEDIA = window.matchMedia
beforeEach(() => {
+ // None of the media conditions above match.
window.matchMedia = jest.fn(media =>
media === `only screen and (min-width: 1024px)`
? {
@@ -280,6 +281,48 @@ describe(``, () => {
)
})
+ it(`should select the image with no media query as mocked image of fluid variants provided.`, () => {
+ const { container } = render(
+
+ )
+ const aspectPreserver = container.querySelector(`div div div`)
+ expect(aspectPreserver.getAttribute(`style`)).toEqual(
+ expect.stringMatching(/padding-bottom: 50%/)
+ )
+ })
+
+ it(`should select the image with no media query as mocked image of fixed variants provided.`, () => {
+ const { container } = render(
+
+ )
+ const aspectPreserver = container.querySelector(`div div`)
+ expect(aspectPreserver.getAttribute(`style`)).toEqual(
+ expect.stringMatching(/width: 100px; height: 100px;/)
+ )
+ })
+
it(`should call onLoad and onError image events`, () => {
const onLoadMock = jest.fn()
const onErrorMock = jest.fn()
diff --git a/packages/gatsby-image/src/index.js b/packages/gatsby-image/src/index.js
index 29e45495a1532..d575531bf7f36 100644
--- a/packages/gatsby-image/src/index.js
+++ b/packages/gatsby-image/src/index.js
@@ -91,6 +91,14 @@ const getCurrentSrcData = currentData => {
if (foundMedia !== -1) {
return currentData[foundMedia]
}
+
+ // No media matches, select first element without a media condition
+ const noMedia = currentData.findIndex(
+ image => typeof image.media === `undefined`
+ )
+ if (noMedia !== -1) {
+ return currentData[noMedia]
+ }
}
// Else return the first image.
return currentData[0]