Skip to content

Commit df0fe31

Browse files
ascorbicgatsbybot
and
gatsbybot
authored
tests(gatsby-recipes): Use mock fs and fetch (#26821)
* Make recipe fs tests use mock fs and fetch * Windows fix Co-authored-by: gatsbybot <[email protected]>
1 parent ef86336 commit df0fe31

File tree

7 files changed

+294
-290
lines changed

7 files changed

+294
-290
lines changed

packages/gatsby-recipes/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"@rollup/plugin-commonjs": "^14.0.0",
9999
"@rollup/plugin-json": "^4.1.0",
100100
"@rollup/plugin-node-resolve": "^8.4.0",
101+
"fetch-mock-jest": "^1.3.0",
101102
"ink": "next",
102103
"ink-select-input": "^4.0.0",
103104
"ink-spinner": "^4.0.0-0",

packages/gatsby-recipes/src/providers/fs/__snapshots__/directory.test.js.snap

Lines changed: 0 additions & 37 deletions
This file was deleted.

packages/gatsby-recipes/src/providers/fs/__snapshots__/file.test.js.snap

Lines changed: 0 additions & 225 deletions
This file was deleted.

packages/gatsby-recipes/src/providers/fs/directory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const directoryExists = fullPath => {
1818
const create = async ({ root }, { id, path: directoryPath }) => {
1919
const fullPath = makePath(root, directoryPath)
2020
await fs.ensureDir(fullPath)
21-
return await read({ root }, directoryPath)
21+
return read({ root }, directoryPath)
2222
}
2323

2424
const update = async (context, resource) => {
@@ -29,7 +29,7 @@ const update = async (context, resource) => {
2929
// Also Directory needs a key.
3030
const fullPath = makePath(context.root, resource.id)
3131
await fs.ensureDir(fullPath)
32-
return await read(context, resource.id)
32+
return read(context, resource.id)
3333
}
3434

3535
const read = async (context, id) => {
Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,64 @@
1-
const directory = require(`./directory`)
2-
const resourceTestHelper = require(`../resource-test-helper`)
1+
import directory from "./directory"
2+
import resourceSchema from "../resource-schema"
3+
import Joi from "@hapi/joi"
4+
import fs from "fs-extra"
5+
import path from "path"
6+
jest.mock(`fs-extra`)
37

4-
const root = __dirname
8+
const root = `fakeDir`
59

610
describe(`directory resource`, () => {
711
test(`e2e directory resource test`, async () => {
8-
await resourceTestHelper({
9-
resourceModule: directory,
10-
resourceName: `Directory`,
11-
context: { root },
12-
initialObject: { path: `directory` },
13-
partialUpdate: { path: `directory1` },
12+
const context = { root }
13+
const initialObject = { path: `directory` }
14+
const partialUpdate = { path: `directory1` }
15+
16+
const fullPath = path.join(root, initialObject.path)
17+
18+
const createPlan = await directory.plan(context, initialObject)
19+
expect(createPlan).toBeTruthy()
20+
21+
expect(createPlan).toMatchInlineSnapshot(`
22+
Object {
23+
"describe": "Create directory \\"directory\\"",
24+
}
25+
`)
26+
27+
// Test creating the resource
28+
const createResponse = await directory.create(context, initialObject)
29+
const validateResult = Joi.validate(createResponse, {
30+
...directory.schema,
31+
...resourceSchema,
1432
})
33+
expect(validateResult.error).toBeNull()
34+
expect(fs.ensureDir).toHaveBeenCalledWith(fullPath)
35+
36+
expect(createResponse).toMatchInlineSnapshot(`
37+
Object {
38+
"_message": "Created directory \\"directory\\"",
39+
"id": "directory",
40+
"path": "directory",
41+
}
42+
`)
43+
44+
// Test reading the resource
45+
const readResponse = await directory.read(context, createResponse.id)
46+
expect(readResponse).toEqual(createResponse)
47+
48+
// Test updating the resource
49+
const updatedResource = { ...readResponse, ...partialUpdate }
50+
const updatePlan = await directory.plan(context, updatedResource)
51+
expect(updatePlan).toMatchInlineSnapshot(`
52+
Object {
53+
"describe": "Create directory \\"directory1\\"",
54+
}
55+
`)
56+
57+
fs.ensureDir.mockReset()
58+
const updateResponse = await directory.update(context, updatedResource)
59+
expect(fs.ensureDir).toHaveBeenCalledWith(fullPath)
60+
61+
await directory.destroy(context, updateResponse)
62+
expect(fs.rmdir).toHaveBeenCalledWith(fullPath)
1563
})
1664
})

0 commit comments

Comments
 (0)