Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(gatsby-image): Add jest-matchmedia-mock to replace DIY mock #26118

Merged
merged 1 commit into from
Jul 30, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
],
"dependencies": {
"dictionary-en": "^3.0.1",
"jest-matchmedia-mock": "^1.0.1",
"retext-emoji": "^7.0.1",
"retext-spell": "^4.0.0",
"retext-syntax-mentions": "^2.1.1",
Expand Down
24 changes: 11 additions & 13 deletions packages/gatsby-image/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react"
import { render, cleanup, fireEvent } from "@testing-library/react"
import MatchMediaMock from "jest-matchmedia-mock"
import Image from "../"

afterAll(cleanup)
Expand Down Expand Up @@ -117,22 +118,13 @@ const setupImages = (
return container
}

let matchMedia
describe(`<Image />`, () => {
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)`
? {
matches: true,
}
: {
matches: false,
}
)
beforeAll(() => {
matchMedia = new MatchMediaMock()
})
afterEach(() => {
window.matchMedia = OLD_MATCH_MEDIA
matchMedia.clear()
})

it(`should render fixed size images`, () => {
Expand Down Expand Up @@ -233,6 +225,9 @@ describe(`<Image />`, () => {
})

it(`should select the correct mocked image of fluid variants provided.`, () => {
const mediaQuery = `only screen and (min-width: 1024px)`
matchMedia.useMediaQuery(mediaQuery)

const tripleFluidImageShapeMock = fluidImagesShapeMock.concat({
aspectRatio: 5,
src: `test_image_4.jpg`,
Expand Down Expand Up @@ -263,6 +258,9 @@ describe(`<Image />`, () => {
})

it(`should select the correct mocked image of fixed variants provided.`, () => {
const mediaQuery = `only screen and (min-width: 1024px)`
matchMedia.useMediaQuery(mediaQuery)

const tripleFixedImageShapeMock = fixedImagesShapeMock.concat({
width: 1024,
height: 768,
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13458,6 +13458,11 @@ jest-matcher-utils@^24.9.0:
jest-get-type "^24.9.0"
pretty-format "^24.9.0"

jest-matchmedia-mock@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/jest-matchmedia-mock/-/jest-matchmedia-mock-1.0.1.tgz#da1c75be82a3b3e46e6c8ae195aa54e4a6032eb4"
integrity sha512-IXk2AnldfsCRmhB4pFY0Eb9mk2NxFqOgOwA4DxKOJ22T5bn/j3DzM5s3cdKxuEowmpuXK1gPXAIlVGIC+kb/fA==

jest-message-util@^22.4.0, jest-message-util@^22.4.3:
version "22.4.3"
resolved "https://registry.yarnpkg.com/jest-message-util/-/jest-message-util-22.4.3.tgz#cf3d38aafe4befddbfc455e57d65d5239e399eb7"
Expand Down