From 1d88d36d88b0df453356b8f846ce06540e14a66a Mon Sep 17 00:00:00 2001 From: Danny Wilson Date: Mon, 15 Jan 2018 14:35:08 -0400 Subject: [PATCH] gatsby-plugin-sharp: Remove warning for resolutions when requested width and image width are equal --- .../src/__tests__/index.js | 38 ++++++++++++++++++- packages/gatsby-plugin-sharp/src/index.js | 2 +- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/packages/gatsby-plugin-sharp/src/__tests__/index.js b/packages/gatsby-plugin-sharp/src/__tests__/index.js index e0f7582a4ea31..01a5ffa8f17e2 100644 --- a/packages/gatsby-plugin-sharp/src/__tests__/index.js +++ b/packages/gatsby-plugin-sharp/src/__tests__/index.js @@ -1,6 +1,6 @@ const path = require(`path`) -const { base64, responsiveSizes } = require(`../`) +const { base64, responsiveSizes, resolutions } = require(`../`) describe(`gatsby-plugin-sharp`, () => { const args = { @@ -50,6 +50,42 @@ describe(`gatsby-plugin-sharp`, () => { }) }) + describe(`resolutions`, () => { + console.warn = jest.fn() + + beforeEach(() => { + console.warn.mockClear() + }) + + afterAll(() => { + console.warn.mockClear() + }) + + it(`does not warn when the requested width is equal to the image width`, async () => { + const args = { width: 1 } + + const result = await resolutions({ + file, + args, + }) + + expect(result.width).toEqual(1) + expect(console.warn).toHaveBeenCalledTimes(0) + }) + + it(`warns when the requested width is greater than the image width`, async () => { + const args = { width: 2 } + + const result = await resolutions({ + file, + args, + }) + + expect(result.width).toEqual(1) + expect(console.warn).toHaveBeenCalledTimes(1) + }) + }) + describe(`base64`, () => { it(`converts image to base64`, async () => { const result = await base64({ diff --git a/packages/gatsby-plugin-sharp/src/index.js b/packages/gatsby-plugin-sharp/src/index.js index 81d7f928f98e8..0a784aa6f2345 100644 --- a/packages/gatsby-plugin-sharp/src/index.js +++ b/packages/gatsby-plugin-sharp/src/index.js @@ -475,7 +475,7 @@ async function resolutions({ file, args = {} }) { sizes.push(options.width * 3) const dimensions = imageSize(file.absolutePath) - const filteredSizes = sizes.filter(size => size < dimensions.width) + const filteredSizes = sizes.filter(size => size <= dimensions.width) // If there's no sizes after filtering (e.g. image is smaller than what's // requested, add back the original so there's at least something)