|
| 1 | +import fs from "fs-extra" |
| 2 | +import { tmpdir } from "os" |
| 3 | +import { join } from "path" |
| 4 | +import { |
| 5 | + injectEntries, |
| 6 | + ADAPTER_MARKER_START, |
| 7 | + ADAPTER_MARKER_END, |
| 8 | + NETLIFY_PLUGIN_MARKER_START, |
| 9 | + NETLIFY_PLUGIN_MARKER_END, |
| 10 | + GATSBY_PLUGIN_MARKER_START, |
| 11 | +} from "../route-handler" |
| 12 | + |
| 13 | +function generateLotOfContent(placeholderCharacter: string): string { |
| 14 | + return (placeholderCharacter.repeat(80) + `\n`).repeat(1_000_000) |
| 15 | +} |
| 16 | + |
| 17 | +const newAdapterContent = generateLotOfContent(`a`) |
| 18 | +const previousAdapterContent = |
| 19 | + ADAPTER_MARKER_START + |
| 20 | + `\n` + |
| 21 | + generateLotOfContent(`b`) + |
| 22 | + ADAPTER_MARKER_END + |
| 23 | + `\n` |
| 24 | + |
| 25 | +const gatsbyPluginNetlifyContent = |
| 26 | + GATSBY_PLUGIN_MARKER_START + `\n` + generateLotOfContent(`c`) |
| 27 | + |
| 28 | +const netlifyPluginGatsbyContent = |
| 29 | + NETLIFY_PLUGIN_MARKER_START + |
| 30 | + `\n` + |
| 31 | + generateLotOfContent(`c`) + |
| 32 | + NETLIFY_PLUGIN_MARKER_END + |
| 33 | + `\n` |
| 34 | + |
| 35 | +const customContent1 = |
| 36 | + `# customContent1 start` + |
| 37 | + `\n` + |
| 38 | + generateLotOfContent(`x`) + |
| 39 | + `# customContent1 end` + |
| 40 | + `\n` |
| 41 | +const customContent2 = |
| 42 | + `# customContent2 start` + |
| 43 | + `\n` + |
| 44 | + generateLotOfContent(`y`) + |
| 45 | + `# customContent2 end` + |
| 46 | + `\n` |
| 47 | +const customContent3 = |
| 48 | + `# customContent3 start` + |
| 49 | + `\n` + |
| 50 | + generateLotOfContent(`z`) + |
| 51 | + `# customContent3 end` + |
| 52 | + `\n` |
| 53 | + |
| 54 | +async function getContent(previousContent?: string): Promise<string> { |
| 55 | + const filePath = join( |
| 56 | + await fs.mkdtemp(join(tmpdir(), `inject-entries`)), |
| 57 | + `out.txt` |
| 58 | + ) |
| 59 | + |
| 60 | + if (typeof previousContent !== `undefined`) { |
| 61 | + await fs.writeFile(filePath, previousContent) |
| 62 | + } |
| 63 | + |
| 64 | + await injectEntries(filePath, newAdapterContent) |
| 65 | + |
| 66 | + return fs.readFile(filePath, `utf8`) |
| 67 | +} |
| 68 | + |
| 69 | +jest.setTimeout(60_000) |
| 70 | + |
| 71 | +describe(`route-handler`, () => { |
| 72 | + describe(`injectEntries`, () => { |
| 73 | + it(`no cached file`, async () => { |
| 74 | + const content = await getContent() |
| 75 | + |
| 76 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 77 | + }) |
| 78 | + |
| 79 | + describe(`has cached file`, () => { |
| 80 | + it(`no previous adapter or plugins or custom entries`, async () => { |
| 81 | + const content = await getContent(``) |
| 82 | + |
| 83 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 84 | + }) |
| 85 | + |
| 86 | + it(`has just custom entries`, async () => { |
| 87 | + const content = await getContent(customContent1) |
| 88 | + |
| 89 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 90 | + expect(content.indexOf(customContent1)).not.toBe(-1) |
| 91 | + }) |
| 92 | + |
| 93 | + it(`has just gatsby-plugin-netlify entries`, async () => { |
| 94 | + const content = await getContent(gatsbyPluginNetlifyContent) |
| 95 | + |
| 96 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 97 | + // it removes gatsby-plugin-netlify entries |
| 98 | + expect(content.indexOf(GATSBY_PLUGIN_MARKER_START)).toBe(-1) |
| 99 | + expect(content.indexOf(gatsbyPluginNetlifyContent)).toBe(-1) |
| 100 | + }) |
| 101 | + |
| 102 | + it(`has just netlify-plugin-gatsby entries`, async () => { |
| 103 | + const content = await getContent(netlifyPluginGatsbyContent) |
| 104 | + |
| 105 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 106 | + // it removes netlify-plugin-gatsby entries |
| 107 | + expect(content.indexOf(NETLIFY_PLUGIN_MARKER_START)).toBe(-1) |
| 108 | + expect(content.indexOf(NETLIFY_PLUGIN_MARKER_END)).toBe(-1) |
| 109 | + expect(content.indexOf(netlifyPluginGatsbyContent)).toBe(-1) |
| 110 | + }) |
| 111 | + |
| 112 | + it(`has gatsby-plugin-netlify, nelify-plugin-gatsby, custom content and previous adapter content`, async () => { |
| 113 | + // kitchen-sink |
| 114 | + const previousContent = |
| 115 | + customContent1 + |
| 116 | + previousAdapterContent + |
| 117 | + customContent2 + |
| 118 | + netlifyPluginGatsbyContent + |
| 119 | + customContent3 + |
| 120 | + gatsbyPluginNetlifyContent |
| 121 | + |
| 122 | + const content = await getContent(previousContent) |
| 123 | + |
| 124 | + expect(content.indexOf(newAdapterContent)).not.toBe(-1) |
| 125 | + |
| 126 | + // it preserve any custom entries |
| 127 | + expect(content.indexOf(customContent1)).not.toBe(-1) |
| 128 | + expect(content.indexOf(customContent2)).not.toBe(-1) |
| 129 | + expect(content.indexOf(customContent3)).not.toBe(-1) |
| 130 | + |
| 131 | + // it removes previous gatsby-adapter-netlify entries |
| 132 | + expect(content.indexOf(previousAdapterContent)).toBe(-1) |
| 133 | + |
| 134 | + // it removes gatsby-plugin-netlify entries |
| 135 | + expect(content.indexOf(GATSBY_PLUGIN_MARKER_START)).toBe(-1) |
| 136 | + expect(content.indexOf(gatsbyPluginNetlifyContent)).toBe(-1) |
| 137 | + |
| 138 | + // it removes netlify-plugin-gatsby entries |
| 139 | + expect(content.indexOf(NETLIFY_PLUGIN_MARKER_START)).toBe(-1) |
| 140 | + expect(content.indexOf(NETLIFY_PLUGIN_MARKER_END)).toBe(-1) |
| 141 | + expect(content.indexOf(netlifyPluginGatsbyContent)).toBe(-1) |
| 142 | + }) |
| 143 | + }) |
| 144 | + }) |
| 145 | +}) |
0 commit comments