-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(gatsby-plugin-typography): sort typography style tag first to avo…
- Loading branch information
1 parent
881ca39
commit 5caf95e
Showing
3 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
File renamed without changes.
103 changes: 103 additions & 0 deletions
103
packages/gatsby-plugin-typography/src/__tests__/gatsby-ssr.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
jest.mock( | ||
`../.cache/typography`, | ||
() => { | ||
return {} | ||
}, | ||
{ virtual: true } | ||
) | ||
|
||
import { onPreRenderHTML, onRenderBody } from "../gatsby-ssr" | ||
|
||
const clone = arr => arr.reduce((merged, item) => merged.concat(item), []) | ||
|
||
describe(`onRenderBody`, () => { | ||
const setup = (options = {}, env = `build-html`) => { | ||
process.env.BUILD_STAGE = env | ||
const api = { | ||
setHeadComponents: jest.fn(), | ||
} | ||
onRenderBody(api, options) | ||
return api | ||
} | ||
|
||
afterAll(() => { | ||
delete process.env.BUILD_STAGE | ||
}) | ||
|
||
it(`invokes setHeadComponents with array of typography components`, () => { | ||
const api = setup() | ||
|
||
expect(api.setHeadComponents).toHaveBeenCalledWith([ | ||
expect.objectContaining({ key: `TypographyStyle` }), | ||
expect.objectContaining({ key: `GoogleFont` }), | ||
]) | ||
}) | ||
|
||
it(`only invokes setHeadComponents if BUILD_STAGE is build-html`, () => { | ||
const api = setup({}, `develop`) | ||
|
||
expect(api.setHeadComponents).not.toHaveBeenCalled() | ||
}) | ||
|
||
it(`does not add google font if omitGoogleFont is passed`, () => { | ||
const api = setup({ | ||
omitGoogleFont: true, | ||
}) | ||
|
||
expect(api.setHeadComponents).toHaveBeenCalledWith([ | ||
expect.objectContaining({ key: `TypographyStyle` }), | ||
]) | ||
}) | ||
}) | ||
|
||
describe(`onPreRenderHTML`, () => { | ||
const setup = (components = []) => { | ||
const api = { | ||
getHeadComponents: jest.fn(() => components), | ||
replaceHeadComponents: jest.fn(), | ||
} | ||
onPreRenderHTML(api) | ||
return api | ||
} | ||
|
||
it(`reorders typography-js first`, () => { | ||
const spies = setup([ | ||
{ | ||
key: `link-1234`, | ||
}, | ||
{ | ||
key: `link-preload`, | ||
}, | ||
{ | ||
key: `TypographyStyle`, | ||
}, | ||
]) | ||
|
||
expect(spies.replaceHeadComponents).toHaveBeenCalledTimes(1) | ||
expect(spies.replaceHeadComponents).toHaveBeenCalledWith( | ||
expect.arrayContaining([ | ||
{ | ||
key: `TypographyStyle`, | ||
}, | ||
]) | ||
) | ||
}) | ||
|
||
it(`leaves non-typography head components as-is`, () => { | ||
const components = [ | ||
{ | ||
key: `link-1234`, | ||
}, | ||
{ | ||
key: `link-preload`, | ||
}, | ||
{ | ||
key: `_____01234_____`, | ||
}, | ||
] | ||
|
||
const spies = setup(clone(components)) | ||
|
||
expect(spies.replaceHeadComponents).toHaveBeenCalledWith(components) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
I'm having an issue with this logic when using this plugin with bootstrap and
gatsby-plugin-sass
. Bootstrap rewrites the font rules fromtypography.js
(_reboot.scss
from bootstrap). I tried checking if it was possible to re-order the styles, but it's non-trivial, it seems. Is there a workaround for that?