-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
mdx-loader.test.js
98 lines (92 loc) · 2.24 KB
/
mdx-loader.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const mdxLoader = require(`./mdx-loader`)
const prettier = require(`prettier`)
const c = require(`js-combinatorics`)
function genMDXFile(input) {
const code = {
frontmatter: `---
one: two
three: 4
array: [1,2,3]
---`,
defaultLayout: `export default ({children, ...props}) => (
<div>
{children}
</div>
)`,
namedExports: `export const meta = {author: "chris"}`,
body: `# Some title
a bit of a paragraph
some content`,
}
return {
name:
Object.entries(input)
.filter(([k, v]) => !!v) // eslint-disable-line no-unused-vars
.map(([k, v]) => k) // eslint-disable-line no-unused-vars
.join(`-`) || `body`,
content: [
input.frontmatter ? code.frontmatter : ``,
input.layout ? code.defaultLayout : ``,
input.namedExports ? code.namedExports : ``,
code.body,
].join(`\n\n`),
}
}
// generate a table of all possible combinations of genMDXfile input
const fixtures = c
.baseN([true, false], 3)
.toArray()
.map(([frontmatter, layout, namedExports]) =>
genMDXFile({ frontmatter, layout, namedExports })
)
.map(({ name, content }) => [
name,
{
internal: { type: `File` },
sourceInstanceName: `webpack-test-fixtures`,
absolutePath: `/fake/${name}`,
},
content,
])
describe(`mdx-loader`, () => {
expect.addSnapshotSerializer({
print(val /*, serialize */) {
return prettier.format(val, { parser: `babel` })
},
test() {
return true
},
})
test.each(fixtures)(
`snapshot with %s`,
async (filename, fakeGatsbyNode, content) => {
const loader = mdxLoader.bind({
async() {
return (err, result) => {
expect(err).toBeNull()
expect(result).toMatchSnapshot()
}
},
query: {
getNodes(_type) {
return fixtures.map(([, node]) => node)
},
getNodesByType(_type) {
return fixtures.map(([, node]) => node)
},
pluginOptions: {},
cache: {
get() {
return false
},
set() {
return
},
},
},
resourcePath: fakeGatsbyNode.absolutePath,
})
await loader(content)
}
)
})