From 8e73a31d6f5b531061156cdcca4e6c949e067cd0 Mon Sep 17 00:00:00 2001 From: James Williams Date: Thu, 14 Feb 2019 14:08:09 +0000 Subject: [PATCH] Added tests for remark images with query strings. --- .../src/__tests__/__snapshots__/index.js.snap | 38 +++++++++++++++++++ .../src/__tests__/index.js | 37 +++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap index 3bd298f555300..c61b3665e6f1e 100644 --- a/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap +++ b/packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap @@ -124,3 +124,41 @@ exports[`it transforms images in markdown 1`] = ` " `; + +exports[`it transforms HTML img tags with query strings 1`] = ` +" + + + \\"my + + " +`; + +exports[`it transforms images in markdown with query strings 1`] = ` +" + + + \\"image\\" + + " +`; diff --git a/packages/gatsby-remark-images/src/__tests__/index.js b/packages/gatsby-remark-images/src/__tests__/index.js index 8696b1d7d8b64..4314facbe4ab6 100644 --- a/packages/gatsby-remark-images/src/__tests__/index.js +++ b/packages/gatsby-remark-images/src/__tests__/index.js @@ -15,6 +15,7 @@ jest.mock(`gatsby-plugin-sharp`, () => { }) const Remark = require(`remark`) +const queryString = require(`query-string`) const plugin = require(`../`) @@ -53,7 +54,7 @@ const createPluginOptions = (content, imagePaths = `/`) => { return { files: [].concat(imagePaths).map(imagePath => { return { - absolutePath: `${dirName}/${imagePath}`, + absolutePath: queryString.parseUrl(`${dirName}/${imagePath}`).url, } }), markdownNode: createNode(content), @@ -268,3 +269,37 @@ test(`it handles goofy nesting properly`, async () => { expect(node.value).toMatchSnapshot() expect(node.value).not.toMatch(``) }) + +test(`it transforms HTML img tags with query strings`, async () => { + const imagePath = `image/my-image.jpeg?query=string` + + const content = ` + + `.trim() + + const nodes = await plugin(createPluginOptions(content, imagePath)) + + expect(nodes.length).toBe(1) + + const node = nodes.pop() + expect(node.type).toBe(`html`) + expect(node.value).toMatchSnapshot() + expect(node.value).not.toMatch(``) +}) + +test(`it transforms images in markdown with query strings`, async () => { + const imagePath = `images/my-image.jpeg?query=string` + const content = ` + +![image](./${imagePath}) + `.trim() + + const nodes = await plugin(createPluginOptions(content, imagePath)) + + expect(nodes.length).toBe(1) + + const node = nodes.pop() + expect(node.type).toBe(`html`) + expect(node.value).toMatchSnapshot() + expect(node.value).not.toMatch(``) +})