Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(typography): Prepend styles to <head> instead of placing them at end, possibly overwriting user styles #201

Merged
merged 5 commits into from
Mar 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}
39 changes: 33 additions & 6 deletions __test__/typography.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,17 @@ describe("typography(options?).createStyles()", () => {
})

describe("typography(options?).injectStyles()", () => {
beforeEach(() => {
delete global.document
})

it("should not fail if document is undefined", () => {
expect(() => {
typography().injectStyles()
}).not.toThrow()
})

it("should set css if typography.js element exists", () => {
it("should set style if typography.js element exists", () => {
const sut = typography()

global.document = jasmine.createSpyObj("document", ["getElementById"])
Expand All @@ -88,8 +92,6 @@ describe("typography(options?).injectStyles()", () => {

expect(styleNode.innerHTML).toEqual(sut.toString())
expect(global.document.getElementById).toHaveBeenCalledWith("typography.js")

delete global.document
})

it("should create a new style node if typography.js element does not exists", () => {
Expand All @@ -105,16 +107,41 @@ describe("typography(options?).injectStyles()", () => {

global.document.getElementById.and.returnValue(null)
global.document.createElement.and.returnValue(styleNode)
global.document.head.appendChild = jasmine.createSpy("appendChild")
global.document.head.prepend = jasmine.createSpy("prepend")

sut.injectStyles()

expect(styleNode.id).toEqual("typography.js")
expect(styleNode.innerHTML).toEqual(sut.toString())
expect(global.document.createElement).toHaveBeenCalledWith("style")
expect(global.document.getElementById).toHaveBeenCalledWith("typography.js")
expect(global.document.head.appendChild).toHaveBeenCalledWith(styleNode)
expect(global.document.head.prepend).toHaveBeenCalledWith(styleNode)
})

delete global.document
describe("prepending", () => {
const setup = (styleNode, head) => {
const sut = typography()

global.document = jasmine.createSpyObj("document", [
"head",
"createElement",
"getElementById",
])

global.document.getElementById.and.returnValue(null)
global.document.createElement.and.returnValue(styleNode)
global.document.head = head

sut.injectStyles(true)
}

it("can add to beginning of head tags", () => {
const styleNode = {}
const head = {
prepend: jasmine.createSpy(),
}
setup(styleNode, head)
expect(head.prepend).toHaveBeenCalledWith(styleNode)
})
})
})
8 changes: 4 additions & 4 deletions packages/typography/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const typography = function(opts: OptionsType) {
"Fira Sans",
"Droid Sans",
"Helvetica Neue",
"sans-serif",
"sans-serif"
],
bodyFontFamily: ["georgia", "serif"],
headerColor: "inherit",
Expand All @@ -34,7 +34,7 @@ const typography = function(opts: OptionsType) {
bodyWeight: "normal",
boldWeight: "bold",
includeNormalize: true,
blockMarginBottom: 1,
blockMarginBottom: 1
}

const options = objectAssign({}, defaults, opts)
Expand Down Expand Up @@ -75,10 +75,10 @@ const typography = function(opts: OptionsType) {
const node = document.createElement("style")
node.id = "typography.js"
node.innerHTML = this.toString()
document.head.appendChild(node)
document.head.prepend(node)
}
}
},
}
}
}

Expand Down