Skip to content

Commit

Permalink
add a bunch of tests for various plugins (gatsbyjs#1581)
Browse files Browse the repository at this point in the history
* test: add test scaffolding for plugins

* test: add a simple test for navigateTo

* test: add a bunch of tests

* style: run prettier on tests

* test: fix broken absolute url

* test: fix equality check

* test: (hopefully) fix issue with windows
  • Loading branch information
DSchau authored and KyleAMathews committed Jul 26, 2017
1 parent c4fc8fd commit 8667ccb
Show file tree
Hide file tree
Showing 20 changed files with 693 additions and 3 deletions.
55 changes: 55 additions & 0 deletions packages/gatsby-link/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react"

const getInstance = (props, pathPrefix = ``) => {
Object.assign(global.window, {
__PREFIX_PATHS__: pathPrefix ? true : false,
__PATH_PREFIX__: pathPrefix,
})

const Link = require(`../`).default
return new Link(props)
}

const getNavigateTo = () => {
Object.assign(global.window, {
___navigateTo: jest.fn(),
})

return require(`../`).navigateTo
}

describe(`<Link />`, () => {
describe(`path prefixing`, () => {
it(`does not include path prefix by default`, () => {
const to = `/path`
const instance = getInstance({
to,
})

expect(instance.state.to).toEqual(to)
})

/*
* Running _both_ of these tests causes the globals to be cached or something
*/
it.skip(`will use __PATH_PREFIX__ if __PREFIX_PATHS__ defined`, () => {
const to = `/path`
const pathPrefix = `/blog`

const instance = getInstance(
{
to,
},
pathPrefix
)

expect(instance.state.to).toEqual(`${pathPrefix}${to}`)
})
})

it(`navigateTo is called with correct args`, () => {
getNavigateTo()(`/some-path`)

expect(global.window.___navigateTo).toHaveBeenCalledWith(`/some-path`)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-plugin-coffeescript contains coffee script extensions 1`] = `
Array [
".coffee",
".cjsx",
]
`;

exports[`gatsby-plugin-coffeescript pre processing transforms .cjsx files 1`] = `
"(function() {
var React;
React = require('react');
module.exports = class extends React.Component({
render: function() {
return React.createElement(\\"div\\", null, React.createElement(\\"h1\\", null, \\"Hello World\\"));
}
}) {};
}).call(this);
"
`;

exports[`gatsby-plugin-coffeescript pre processing transforms .coffee files 1`] = `
"(function() {
if (typeof elvis !== \\"undefined\\" && elvis !== null) {
alert(\\"I knew it!\\");
}
}).call(this);
"
`;
68 changes: 68 additions & 0 deletions packages/gatsby-plugin-coffeescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
const {
resolvableExtensions,
modifyWebpackConfig,
preprocessSource,
} = require(`../gatsby-node`)

describe(`gatsby-plugin-coffeescript`, () => {
it(`contains coffee script extensions`, () => {
expect(resolvableExtensions()).toMatchSnapshot()
})

it(`modifies webpack config with cofeescript extensions`, () => {
const spy = jest.fn()
const config = {
loader(...args) {
spy(...args)
},
}

modifyWebpackConfig({ config })

expect(spy).toHaveBeenCalledTimes(resolvableExtensions().length)
})

describe(`pre processing`, () => {
it(`returns null if non-coffeescript file`, () => {
expect(
preprocessSource({
filename: `test.js`,
contents: `alert('hello');`,
})
).toBe(null)
})

it(`transforms .coffee files`, () => {
expect(
preprocessSource(
{
filename: `test.coffee`,
contents: `alert "I knew it!" if elvis?`,
},
{}
)
).toMatchSnapshot()
})

it(`transforms .cjsx files`, () => {
expect(
preprocessSource(
{
filename: `test.cjsx`,
contents: `
React = require('react');
module.exports = class extends React.Component {
render: ->
<div>
<h1>Hello World</h1>
</div>
}
`,
},
{}
)
).toMatchSnapshot()
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-plugin-sharp base64 converts image to base64 1`] = `
Object {
"aspectRatio": 1.1111111111111112,
"height": 18,
"src": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsSAAALEgHS3X78AAACsUlEQVQ4y02U2U9aQRTGvVjBigupNZJWkH2Vy6ZgSxUQ1Ia6tSZdfagPXdI16f//0N93c4Zcko8598yZ76wzC5VKZXMh9Mvlcrv5fL4geTqdelrr9fra3t7ehuROpxPosElks1kfLLuzjUYjulAoFIZOgfwQpHESgfix05dKpRxooI8amexW0O1qDZ339XddLBZT8gxJURs6iD6IvFwuJzjYAYfok3bwEWeCyIiwiIMoe3H0F9q8AaccjEOYCUUVA5sYZkQGQQ+bKmtSe86O7y2lD0dfwYnwSgLKcxl0u92gRuPx2MN4V2SgC/ZNrmIftUwiVvceuts5IWQfwR3Fn3vu9/uLEDbZnxhhR/UGL2q1WtxqGbF1DL46whs8/LZIO46Q9LYgeY7uGOKBIkQeIQ+JLF2tVhct5Sz6GRw/FKXY7/m4s1rOMF5vtVoPIGxaVEeKjIPBag06YH9jMpl46E7Aa3jewvNN+f9RdxSuYYDxDoeeWd0OlLYah9yz9A+xyaFrg0sL5sIR/jKiK9uYytgaoEha6M6JYCZZOqLtI5dVT9dUa+xnEf5EuDGyS4x3aM6yDio9vlXHkVKzWrYVvW4YnKtyBt6AV3D9VQ3fIXy30I/clcM4pRStKUOTR1aKCk6j1hTfJuUefFKXLxG+iBjD+XXzfT9mnT2xoW6bPKTD66FpiKEfE9S/8Bzegg8y4BEIZmswGGiwC2Bkae4rfdCGJLh2zWbTzeExeO8IVdAZ2OZeppznTCazhO6piq8mWIN8zR2/+QVAXie6lI3PtXscatwMDy91d4/RbYYGvGejknaPg4sSuQLUxKSyleKlBtlCX8VbjqsVQd6WDnkJ47LSphFxI1nBYZzoZL4Wer4G6lIm/MBClMYweGDPzs48FyV4Yg48exA2lBFrLPRCJf4D6Rm392Z6xqEAAAAASUVORK5CYII=",
"width": 20,
}
`;

exports[`gatsby-plugin-sharp responsiveSizes includes responsive image properties, e.g. sizes, srcset, etc. 1`] = `
Object {
"aspectRatio": 1.1041666666666667,
"base64": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAASCAYAAABb0P4QAAAACXBIWXMAAAsSAAALEgHS3X78AAACsUlEQVQ4y02U2U9aQRTGvVjBigupNZJWkH2Vy6ZgSxUQ1Ia6tSZdfagPXdI16f//0N93c4Zcko8598yZ76wzC5VKZXMh9Mvlcrv5fL4geTqdelrr9fra3t7ehuROpxPosElks1kfLLuzjUYjulAoFIZOgfwQpHESgfix05dKpRxooI8amexW0O1qDZ339XddLBZT8gxJURs6iD6IvFwuJzjYAYfok3bwEWeCyIiwiIMoe3H0F9q8AaccjEOYCUUVA5sYZkQGQQ+bKmtSe86O7y2lD0dfwYnwSgLKcxl0u92gRuPx2MN4V2SgC/ZNrmIftUwiVvceuts5IWQfwR3Fn3vu9/uLEDbZnxhhR/UGL2q1WtxqGbF1DL46whs8/LZIO46Q9LYgeY7uGOKBIkQeIQ+JLF2tVhct5Sz6GRw/FKXY7/m4s1rOMF5vtVoPIGxaVEeKjIPBag06YH9jMpl46E7Aa3jewvNN+f9RdxSuYYDxDoeeWd0OlLYah9yz9A+xyaFrg0sL5sIR/jKiK9uYytgaoEha6M6JYCZZOqLtI5dVT9dUa+xnEf5EuDGyS4x3aM6yDio9vlXHkVKzWrYVvW4YnKtyBt6AV3D9VQ3fIXy30I/clcM4pRStKUOTR1aKCk6j1hTfJuUefFKXLxG+iBjD+XXzfT9mnT2xoW6bPKTD66FpiKEfE9S/8Bzegg8y4BEIZmswGGiwC2Bkae4rfdCGJLh2zWbTzeExeO8IVdAZ2OZeppznTCazhO6piq8mWIN8zR2/+QVAXie6lI3PtXscatwMDy91d4/RbYYGvGejknaPg4sSuQLUxKSyleKlBtlCX8VbjqsVQd6WDnkJ47LSphFxI1nBYZzoZL4Wer4G6lIm/MBClMYweGDPzs48FyV4Yg48exA2lBFrLPRCJf4D6Rm392Z6xqEAAAAASUVORK5CYII=",
"originalImage": "/static/1234-bc51b.png",
"sizes": "(max-width: 800px) 100vw, 800px",
"src": "/static/1234-bc51b.png",
"srcSet": "/static/1234-bc51b.png 106w",
}
`;
57 changes: 57 additions & 0 deletions packages/gatsby-plugin-sharp/src/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const path = require(`path`)

const {
queueImageResizing,
base64,
responsiveSizes,
responsiveResolution,
} = require(`../`)

describe(`gatsby-plugin-sharp`, () => {
const args = {
duotone: false,
grayscale: false,
rotate: false,
}
const absolutePath = path.resolve(`./www/src/argyle.png`)
const file = {
id: `${absolutePath} absPath of file`,
absolutePath,
extension: `png`,
internal: {
contentDigest: `1234`,
},
}

describe(`responsiveSizes`, () => {
it(`includes responsive image properties, e.g. sizes, srcset, etc.`, async () => {
const result = await responsiveSizes({ file })

expect(result).toMatchSnapshot()
})

it(`adds pathPrefix if defined`, async () => {
const pathPrefix = `/blog`
const result = await responsiveSizes({
file,
args: {
pathPrefix,
},
})

expect(result.src.indexOf(pathPrefix)).toBe(0)
expect(result.srcSet.indexOf(pathPrefix)).toBe(0)
})
})

describe(`base64`, () => {
it(`converts image to base64`, async () => {
const result = await base64({
file,
args,
})

expect(result).toMatchSnapshot()
})
})
})
2 changes: 1 addition & 1 deletion packages/gatsby-plugin-sharp/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ async function responsiveSizes({ file, args = {} }) {

return queueImageResizing({
file,
args: arrrgs,
args: arrrgs, // matey
})
})

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`gatsby-plugin-typescript modifies webpack config 1`] = `
Array [
"typescript",
Object {
"loaders": Array [
"babel?{\\"plugins\\":[\\"\\"]}",
"ts-loader?{\\"compilerOptions\\":{\\"target\\":\\"esnext\\",\\"experimentalDecorators\\":true,\\"jsx\\":\\"react\\",\\"module\\":\\"commonjs\\"},\\"transpileOnly\\":true}",
],
"test": /\\\\\\.tsx\\?\\$/,
},
]
`;

exports[`gatsby-plugin-typescript pre-processing transforms .ts files 1`] = `
"const now = moment().format('HH:MM:ss');
"
`;

exports[`gatsby-plugin-typescript pre-processing transforms JSX files 1`] = `
"import * as React from 'react';
export default () => React.createElement(\\"h1\\", null, \\"Hello World\\");
"
`;

exports[`gatsby-plugin-typescript returns correct extensions 1`] = `
Array [
".ts",
".tsx",
]
`;
76 changes: 76 additions & 0 deletions packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
jest.mock(`path`, () => {
return {
resolve: () => ``,
}
})
const {
resolvableExtensions,
modifyWebpackConfig,
preprocessSource,
} = require(`../gatsby-node`)

describe(`gatsby-plugin-typescript`, () => {
it(`returns correct extensions`, () => {
expect(resolvableExtensions()).toMatchSnapshot()
})

it(`modifies webpack config`, () => {
const config = {
loader: jest.fn(),
}

modifyWebpackConfig({ config }, { compilerOptions: {} })

expect(config.loader).toHaveBeenCalledTimes(1)
const lastCall = config.loader.mock.calls.pop()
expect(lastCall).toMatchSnapshot()
})

describe(`pre-processing`, () => {
const opts = { compilerOptions: {} }
it(`leaves non-tsx? files alone`, () => {
expect(
preprocessSource(
{
contents: `alert('hello');`,
filename: `test.js`,
},
opts
)
).toBeNull()
})

it(`transforms .ts files`, () => {
const js = preprocessSource(
{
filename: `index.ts`,
contents: `
declare let moment: any;
const now: string = moment().format('HH:MM:ss');
`,
},
opts
)
expect(js).not.toBeNull()
expect(js).toMatchSnapshot()
})

it(`transforms JSX files`, () => {
const js = preprocessSource(
{
filename: `tags.ts`,
contents: `
import * as React from 'react';
export default () => <h1>Hello World</h1>;
`,
},
opts
)

expect(js).not.toBeNull()
expect(js).toMatchSnapshot()
})
})
})
Loading

0 comments on commit 8667ccb

Please sign in to comment.