|
| 1 | +jest.mock(`fs`, () => { |
| 2 | + return { |
| 3 | + existsSync: jest.fn(), |
| 4 | + readdirSync: jest.fn(), |
| 5 | + readFileSync: jest.fn(), |
| 6 | + }; |
| 7 | +}); |
| 8 | + |
| 9 | +const fs = require(`fs`); |
| 10 | +const Remark = require(`remark`); |
| 11 | +const plugin = require(`../index`); |
| 12 | + |
| 13 | +const remark = new Remark(); |
| 14 | + |
| 15 | +const getNodeContent = node => node.children[0].children[0]; |
| 16 | + |
| 17 | +describe('gatsby-remark-embedded-codesandbox', () => { |
| 18 | + beforeEach(() => { |
| 19 | + fs.existsSync.mockReset(); |
| 20 | + fs.readdirSync.mockReset(); |
| 21 | + fs.readFileSync.mockReset(); |
| 22 | + |
| 23 | + fs.existsSync.mockReturnValue(true); |
| 24 | + fs.readdirSync.mockReturnValue(['package.json', 'index.html', 'index.js']); |
| 25 | + fs.readFileSync |
| 26 | + .mockReturnValueOnce('{ "name": "example" }') |
| 27 | + .mockReturnValueOnce('<html><body></body></html>') |
| 28 | + .mockReturnValueOnce('const foo = "bar";'); |
| 29 | + }); |
| 30 | + |
| 31 | + it(`generates an embedded sandbox for the specified example folder`, async () => { |
| 32 | + const markdownAST = remark.parse(`[](embedded-codesandbox://example)`); |
| 33 | + const transformed = plugin({ markdownAST }, { directory: `examples` }); |
| 34 | + expect(getNodeContent(transformed)).toMatchSnapshot(); |
| 35 | + }); |
| 36 | + |
| 37 | + it(`generates an embedded sandbox for a nested example folder`, async () => { |
| 38 | + const markdownAST = remark.parse( |
| 39 | + `[](embedded-codesandbox://path/to/folder)` |
| 40 | + ); |
| 41 | + const transformed = plugin({ markdownAST }, { directory: `examples` }); |
| 42 | + expect(fs.readdirSync).toHaveBeenCalledWith('examples/path/to/folder'); |
| 43 | + expect(getNodeContent(transformed)).toMatchSnapshot(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('generates an embedded sandbox using a custom protocol', async () => { |
| 47 | + const markdownAST = remark.parse(`[](custom-embedded-protocol://example)`); |
| 48 | + const transformed = plugin( |
| 49 | + { markdownAST }, |
| 50 | + { directory: 'examples', protocol: 'custom-embedded-protocol://' } |
| 51 | + ); |
| 52 | + expect(getNodeContent(transformed)).toMatchSnapshot(); |
| 53 | + }); |
| 54 | + |
| 55 | + it('generates an embedded sandbox using custom embedding options', async () => { |
| 56 | + const markdownAST = remark.parse(`[](embedded-codesandbox://example)`); |
| 57 | + const transformed = plugin( |
| 58 | + { markdownAST }, |
| 59 | + { |
| 60 | + directory: 'examples', |
| 61 | + embedOptions: { |
| 62 | + view: 'split', |
| 63 | + hidenavigation: 0, |
| 64 | + }, |
| 65 | + } |
| 66 | + ); |
| 67 | + expect(getNodeContent(transformed).value).toEqual( |
| 68 | + expect.stringContaining('query=hidenavigation%3D0%26view%3Dsplit') |
| 69 | + ); |
| 70 | + }); |
| 71 | + |
| 72 | + it('generates an embedded sandbox using a custom getIframe function', async () => { |
| 73 | + const markdownAST = remark.parse(`[](embedded-codesandbox://example)`); |
| 74 | + const transformed = plugin( |
| 75 | + { markdownAST }, |
| 76 | + { |
| 77 | + directory: 'examples', |
| 78 | + getIframe: url => `<iframe src="${url}" />`, |
| 79 | + } |
| 80 | + ); |
| 81 | + expect(getNodeContent(transformed)).toMatchSnapshot(); |
| 82 | + }); |
| 83 | +}); |
0 commit comments