From 5c99dd53e3a8cbd361751ed428ed05f83b3791c4 Mon Sep 17 00:00:00 2001 From: NathanYi Date: Mon, 18 Dec 2023 15:27:33 -0600 Subject: [PATCH] added mocktwo for test with mdx syntax --- src/__tests__/md-inject.test.ts | 45 ++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/__tests__/md-inject.test.ts b/src/__tests__/md-inject.test.ts index d748be1..d8efa8e 100644 --- a/src/__tests__/md-inject.test.ts +++ b/src/__tests__/md-inject.test.ts @@ -320,7 +320,7 @@ The output of some arbitrary command }) it('writes to the markdown document (command) with mdx syntax', async () => { - mock({ + mocktwo({ config: { type: 'command', value: 'some arbitrary command', @@ -332,7 +332,7 @@ The output of some arbitrary command const outFile = ` {/* CODEBLOCK_START {"type":"command","value":"some arbitrary command"} */} -{/* prettier-ignore */} + ~~~~~~~~~~bash $ some arbitrary command @@ -340,7 +340,7 @@ The output of some arbitrary command ~~~~~~~~~~ {/* CODEBLOCK_END */}` - expect(fs.writeFile).toHaveBeenCalledWith('foo.md', outFile) + expect(fs.writeFile).toHaveBeenCalledWith('foo.mdx', outFile) }) it('does not write to the markdown document (command) because of bad syntax', async () => { @@ -1103,3 +1103,42 @@ ${includePrettierIgnore ? '\n' : ''}${blockContents} }) } } + +const mocktwo = ({ + name = '', + mockFileName = 'foo.mdx', + config, + includePrettierIgnore = true, + blockContents = '', + mockResponse = '', +}: { + name?: string + mockFileName?: string + config: any + includePrettierIgnore?: boolean + blockContents?: string + mockResponse?: string +}) => { + glob.mockResolvedValue([mockFileName]) + + fs.readFile.mockImplementation(async (fileName) => { + if (fileName === mockFileName) { + return ` +{/* CODEBLOCK_START${name} ${JSON.stringify(config)} */} +${includePrettierIgnore ? '\n' : ''}${blockContents} +{/* CODEBLOCK_END${name} */}` + } + + if (config.type !== 'command' && fileName.includes(config.value)) { + return mockResponse + } + throw new Error('Unexpected file name passed') + }) + + if (config.type === 'command') { + exec.mockImplementation((...args) => { + const cb = args.pop() + cb(null, mockResponse) + }) + } +}