|
| 1 | +jest.mock(`fs-extra`, () => { |
| 2 | + return { |
| 3 | + ensureDir: jest.fn(() => true), |
| 4 | + writeFile: jest.fn((_f, _b, cb) => cb()), |
| 5 | + stat: jest.fn(() => { |
| 6 | + return { |
| 7 | + isDirectory: jest.fn(), |
| 8 | + } |
| 9 | + }), |
| 10 | + } |
| 11 | +}) |
| 12 | +jest.mock(`../create-file-node`, () => { |
| 13 | + return { |
| 14 | + createFileNode: jest.fn(() => { |
| 15 | + return { internal: {} } |
| 16 | + }), |
| 17 | + } |
| 18 | +}) |
| 19 | + |
| 20 | +const { ensureDir, writeFile } = require(`fs-extra`) |
| 21 | +const { createFileNode } = require(`../create-file-node`) |
| 22 | +const createFileNodeFromBuffer = require(`../create-file-node-from-buffer`) |
| 23 | + |
| 24 | +const createMockBuffer = content => { |
| 25 | + const buffer = Buffer.alloc(content.length) |
| 26 | + buffer.write(content) |
| 27 | + return buffer |
| 28 | +} |
| 29 | + |
| 30 | +const createMockCache = () => { |
| 31 | + return { |
| 32 | + get: jest.fn(), |
| 33 | + set: jest.fn(), |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +const bufferEq = (b1, b2) => Buffer.compare(b1, b2) === 0 |
| 38 | + |
| 39 | +describe(`create-file-node-from-buffer`, () => { |
| 40 | + const defaultArgs = { |
| 41 | + store: { |
| 42 | + getState: jest.fn(() => { |
| 43 | + return { |
| 44 | + program: { |
| 45 | + directory: `__whatever__`, |
| 46 | + }, |
| 47 | + } |
| 48 | + }), |
| 49 | + }, |
| 50 | + createNode: jest.fn(), |
| 51 | + createNodeId: jest.fn(), |
| 52 | + } |
| 53 | + |
| 54 | + describe(`functionality`, () => { |
| 55 | + afterEach(() => jest.clearAllMocks()) |
| 56 | + |
| 57 | + const setup = ({ |
| 58 | + hash, |
| 59 | + buffer = createMockBuffer(`some binary content`), |
| 60 | + cache = createMockCache(), |
| 61 | + } = {}) => |
| 62 | + createFileNodeFromBuffer({ |
| 63 | + ...defaultArgs, |
| 64 | + buffer, |
| 65 | + hash, |
| 66 | + cache, |
| 67 | + }) |
| 68 | + |
| 69 | + it(`rejects when the buffer can't be read`, () => { |
| 70 | + expect(setup({ buffer: null })).rejects.toEqual( |
| 71 | + expect.stringContaining(`bad buffer`) |
| 72 | + ) |
| 73 | + }) |
| 74 | + |
| 75 | + it(`caches the buffer's content locally`, async () => { |
| 76 | + expect.assertions(2) |
| 77 | + |
| 78 | + let output |
| 79 | + writeFile.mockImplementationOnce((_f, buf, cb) => { |
| 80 | + output = buf |
| 81 | + cb() |
| 82 | + }) |
| 83 | + |
| 84 | + const buffer = createMockBuffer(`buffer-content`) |
| 85 | + await setup({ buffer }) |
| 86 | + |
| 87 | + expect(ensureDir).toBeCalledTimes(2) |
| 88 | + expect(bufferEq(buffer, output)).toBe(true) |
| 89 | + }) |
| 90 | + |
| 91 | + it(`uses cached file Promise for buffer with a matching hash`, async () => { |
| 92 | + expect.assertions(3) |
| 93 | + |
| 94 | + const cache = createMockCache() |
| 95 | + |
| 96 | + await setup({ cache, hash: `same-hash` }) |
| 97 | + await setup({ cache, hash: `same-hash` }) |
| 98 | + |
| 99 | + expect(cache.get).toBeCalledTimes(1) |
| 100 | + expect(cache.set).toBeCalledTimes(1) |
| 101 | + expect(writeFile).toBeCalledTimes(1) |
| 102 | + }) |
| 103 | + |
| 104 | + it(`uses cached file from previous run with a matching hash`, async () => { |
| 105 | + expect.assertions(3) |
| 106 | + |
| 107 | + const cache = createMockCache() |
| 108 | + cache.get.mockImplementationOnce(() => `cached-file-path`) |
| 109 | + |
| 110 | + await setup({ cache, hash: `cached-hash` }) |
| 111 | + |
| 112 | + expect(cache.get).toBeCalledWith(expect.stringContaining(`cached-hash`)) |
| 113 | + expect(cache.set).not.toBeCalled() |
| 114 | + expect(createFileNode).toBeCalledWith( |
| 115 | + expect.stringContaining(`cached-file-path`), |
| 116 | + expect.any(Function), |
| 117 | + expect.any(Object) |
| 118 | + ) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + describe(`validation`, () => { |
| 123 | + it(`throws on invalid inputs: createNode`, () => { |
| 124 | + expect(() => { |
| 125 | + createFileNodeFromBuffer({ |
| 126 | + ...defaultArgs, |
| 127 | + createNode: undefined, |
| 128 | + }) |
| 129 | + }).toThrowErrorMatchingInlineSnapshot( |
| 130 | + `"createNode must be a function, was undefined"` |
| 131 | + ) |
| 132 | + }) |
| 133 | + |
| 134 | + it(`throws on invalid inputs: createNodeId`, () => { |
| 135 | + expect(() => { |
| 136 | + createFileNodeFromBuffer({ |
| 137 | + ...defaultArgs, |
| 138 | + createNodeId: undefined, |
| 139 | + }) |
| 140 | + }).toThrowErrorMatchingInlineSnapshot( |
| 141 | + `"createNodeId must be a function, was undefined"` |
| 142 | + ) |
| 143 | + }) |
| 144 | + |
| 145 | + it(`throws on invalid inputs: cache`, () => { |
| 146 | + expect(() => { |
| 147 | + createFileNodeFromBuffer({ |
| 148 | + ...defaultArgs, |
| 149 | + cache: undefined, |
| 150 | + }) |
| 151 | + }).toThrowErrorMatchingInlineSnapshot( |
| 152 | + `"cache must be the Gatsby cache, was undefined"` |
| 153 | + ) |
| 154 | + }) |
| 155 | + |
| 156 | + it(`throws on invalid inputs: store`, () => { |
| 157 | + expect(() => { |
| 158 | + createFileNodeFromBuffer({ |
| 159 | + ...defaultArgs, |
| 160 | + store: undefined, |
| 161 | + }) |
| 162 | + }).toThrowErrorMatchingInlineSnapshot( |
| 163 | + `"store must be the redux store, was undefined"` |
| 164 | + ) |
| 165 | + }) |
| 166 | + }) |
| 167 | +}) |
0 commit comments