Skip to content

Commit

Permalink
added mocktwo for test with mdx syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanYi authored and NathanYi committed Dec 18, 2023
1 parent def27b1 commit 5c99dd5
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/__tests__/md-inject.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -332,15 +332,15 @@ The output of some arbitrary command

const outFile = `
{/* CODEBLOCK_START {"type":"command","value":"some arbitrary command"} */}
{/* prettier-ignore */}
<!-- prettier-ignore -->
~~~~~~~~~~bash
$ some arbitrary command
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 () => {
Expand Down Expand Up @@ -1103,3 +1103,42 @@ ${includePrettierIgnore ? '<!-- prettier-ignore -->\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 ? '<!-- prettier-ignore -->\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)
})
}
}

0 comments on commit 5c99dd5

Please sign in to comment.