diff --git a/src/gatsby-ssr.spec.ts b/src/gatsby-ssr.spec.ts index 83c5120..7343387 100644 --- a/src/gatsby-ssr.spec.ts +++ b/src/gatsby-ssr.spec.ts @@ -13,17 +13,17 @@ describe('gatsby-ssr.js', () => { expect(() => { onRenderBody({}) }).toThrow(new Error('gatsby-plugin-no-javascript: Gatsby removed an internal detail that this plugin relied upon, please submit this issue to https://www.github.com/itmayziii/gatsby-plugin-no-javascript.')) }) }) - + describe('onPreRenderHTML', () => { const pathname = '/my-cool-page' let replaceHeadComponentsSpy: Spy let replacePostBodyComponentsSpy: Spy - + beforeEach(() => { replaceHeadComponentsSpy = jasmine.createSpy('replaceHeadComponents') replacePostBodyComponentsSpy = jasmine.createSpy('replacePostBodyComponents') }) - + it('does not remove non react components from the head, (checks for props)', function () { function getHeadComponents () { return [headComponentsData[2], headComponentsData[3]] @@ -31,18 +31,11 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [] } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replaceHeadComponentsSpy).toHaveBeenCalledWith([headComponentsData[2], headComponentsData[3]]) }) - - it('does the pathname match any of the exclusions', () => { - expect(checkPathExclusion('/client/omegablerns', { excludePaths: /\/client/ })).toBeTruthy() - expect(checkPathExclusion('/about/blerns', { excludePaths: /(\/client)|(\/tacos)/ })).toBeFalsy() - expect(checkPathExclusion('/about/tacos', { excludePaths: /(\/client)|(\/about\/tacos)/ })).toBeTruthy() - expect(checkPathExclusion('/blerkstorm', {})).toBeFalsy() - }) - + it('does not remove scripts in the head marked as excluded from plugin options', () => { onRenderBody({ scripts: scriptsData }) function getHeadComponents () { @@ -51,7 +44,7 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [] } - + onPreRenderHTML({ pathname: '/my-cool-page', getHeadComponents, @@ -61,10 +54,10 @@ describe('gatsby-ssr.js', () => { }, { excludeFiles: /webpack-runtime/ }) expect(replaceHeadComponentsSpy).toHaveBeenCalledWith([headComponentsData[13]]) }) - + it('should remove static files like JSON from the head as these files are always added by Gatsby', () => { const pathname = '/my-cool-page' - + onRenderBody({ scripts: [] }) function getHeadComponents () { return [headComponentsData[0], headComponentsData[1], headComponentsData[14]] @@ -72,32 +65,32 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [] } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replaceHeadComponentsSpy).toHaveBeenCalledWith([headComponentsData[0], headComponentsData[1]]) }) - + it('should remove preload scripts from the head that are called out by Gatsby during onRenderBody', () => { const pathname = '/my-cool-page' onRenderBody({ scripts: scriptsData }) - + function getHeadComponents () { return [ headComponentsData[0], headComponentsData[1], headComponentsData[8], headComponentsData[9], headComponentsData[10], headComponentsData[11], headComponentsData[12], headComponentsData[13], headComponentsData[14] ] } - + function getPostBodyComponents () { return [] } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replaceHeadComponentsSpy).toHaveBeenCalledTimes(1) expect(replaceHeadComponentsSpy.calls.argsFor(0)[0].length).toEqual(2) expect(replaceHeadComponentsSpy).toHaveBeenCalledWith([headComponentsData[0], headComponentsData[1]]) }) - + it('does not remove non react components from the body, (checks for props)', function () { const pathname = '/my-cool-page' const fakeBodyComponents = [ @@ -110,19 +103,19 @@ describe('gatsby-ssr.js', () => { key: 'styles-module2.css' } ] - + function getHeadComponents () { return [] } - + function getPostBodyComponents () { return fakeBodyComponents } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith(fakeBodyComponents) }) - + it('does not remove scripts in the body marked as excluded from plugin options', () => { onRenderBody({ scripts: scriptsData }) function getHeadComponents () { @@ -131,7 +124,7 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [postBodyComponentsData[3]] } - + onPreRenderHTML({ pathname: '/my-cool-page', getHeadComponents, @@ -141,10 +134,10 @@ describe('gatsby-ssr.js', () => { }, { excludeFiles: /webpack-runtime/ }) expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith([postBodyComponentsData[3]]) }) - + it('should remove special Gatsby scripts from the body', () => { const pathname = '/my-cool-page' - + onRenderBody({ scripts: [] }) function getHeadComponents () { return [] @@ -152,14 +145,14 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [postBodyComponentsData[0], postBodyComponentsData[1], postBodyComponentsData[2]] } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith([postBodyComponentsData[0]]) }) - + it('should remove preload scripts from the body that are called out by Gatsby during onRenderBody', () => { const pathname = '/my-cool-page' - + onRenderBody({ scripts: scriptsData }) function getHeadComponents () { return [] @@ -167,17 +160,17 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return [postBodyComponentsData[0], postBodyComponentsData[3], postBodyComponentsData[4], postBodyComponentsData[5], postBodyComponentsData[6], postBodyComponentsData[7]] } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) expect(replacePostBodyComponentsSpy).toHaveBeenCalledTimes(1) expect(replacePostBodyComponentsSpy.calls.argsFor(0)[0].length).toEqual(1) expect(replacePostBodyComponentsSpy).toHaveBeenCalledWith([postBodyComponentsData[0]]) }) - + it('should not remove anything during a non production build', () => { const pathname = '/my-cool-page' const oldEnv = process.env.NODE_ENV - + process.env.NODE_ENV = 'development' onRenderBody({}) function getHeadComponents () { @@ -186,13 +179,43 @@ describe('gatsby-ssr.js', () => { function getPostBodyComponents () { return postBodyComponentsData } - + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, {}) - + expect(replaceHeadComponentsSpy).toHaveBeenCalledTimes(0) expect(replacePostBodyComponentsSpy).toHaveBeenCalledTimes(0) - + process.env.NODE_ENV = oldEnv }) + + it('should not remove anything because of page exclusion ', () => { + const pathname = '/my-cool-page' + + function getHeadComponents () { + return headComponentsData + } + function getPostBodyComponents () { + return postBodyComponentsData + } + + onPreRenderHTML({ getHeadComponents, pathname, replaceHeadComponents: replaceHeadComponentsSpy, getPostBodyComponents, replacePostBodyComponents: replacePostBodyComponentsSpy }, + { + excludePaths: /\/my-cool-page/ + } + ) + + expect(replaceHeadComponentsSpy).toHaveBeenCalledTimes(0) + expect(replacePostBodyComponentsSpy).toHaveBeenCalledTimes(0) + }) + }) + + describe('checkPathExclusion', () => { + it('does the pathname match any of the exclusions', () => { + expect(checkPathExclusion('/client/omegablerns', { excludePaths: /\/client/ })).toBeTruthy() + expect(checkPathExclusion('/my-cool-page', { excludePaths: /\/my-cool-page/ })).toBeTruthy() + expect(checkPathExclusion('/about/blerns', { excludePaths: /(\/client)|(\/tacos)/ })).toBeFalsy() + expect(checkPathExclusion('/about/tacos', { excludePaths: /(\/client)|(\/about\/tacos)/ })).toBeTruthy() + expect(checkPathExclusion('/blerkstorm', {})).toBeFalsy() + }) }) })