|
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`) |
3 | 7 |
|
4 |
| -const root = __dirname |
| 8 | +const root = `fakeDir` |
5 | 9 |
|
6 | 10 | describe(`directory resource`, () => {
|
7 | 11 | 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, |
14 | 32 | })
|
| 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) |
15 | 63 | })
|
16 | 64 | })
|
0 commit comments