forked from gatsbyjs/gatsby
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a bunch of tests for various plugins (gatsbyjs#1581)
* 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
1 parent
c4fc8fd
commit 8667ccb
Showing
20 changed files
with
693 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`) | ||
}) | ||
}) |
34 changes: 34 additions & 0 deletions
34
packages/gatsby-plugin-coffeescript/src/__tests__/__snapshots__/gatsby-node.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
68
packages/gatsby-plugin-coffeescript/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
21 changes: 21 additions & 0 deletions
21
packages/gatsby-plugin-sharp/src/__tests__/__snapshots__/index.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
} | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/gatsby-plugin-typescript/src/__tests__/__snapshots__/gatsby-node.js.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
76
packages/gatsby-plugin-typescript/src/__tests__/gatsby-node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.