diff --git a/.prettierrc b/.prettierrc new file mode 100644 index 00000000..48e90e8d --- /dev/null +++ b/.prettierrc @@ -0,0 +1,7 @@ +{ + "endOfLine": "lf", + "semi": false, + "singleQuote": false, + "tabWidth": 2, + "trailingComma": "es5" +} diff --git a/__test__/typography.test.js b/__test__/typography.test.js index c50995ce..68b95658 100644 --- a/__test__/typography.test.js +++ b/__test__/typography.test.js @@ -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"]) @@ -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", () => { @@ -105,7 +107,7 @@ 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() @@ -113,8 +115,33 @@ describe("typography(options?).injectStyles()", () => { 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) + }) }) }) diff --git a/packages/typography/src/index.js b/packages/typography/src/index.js index e600571e..8ab71e60 100644 --- a/packages/typography/src/index.js +++ b/packages/typography/src/index.js @@ -25,7 +25,7 @@ const typography = function(opts: OptionsType) { "Fira Sans", "Droid Sans", "Helvetica Neue", - "sans-serif", + "sans-serif" ], bodyFontFamily: ["georgia", "serif"], headerColor: "inherit", @@ -34,7 +34,7 @@ const typography = function(opts: OptionsType) { bodyWeight: "normal", boldWeight: "bold", includeNormalize: true, - blockMarginBottom: 1, + blockMarginBottom: 1 } const options = objectAssign({}, defaults, opts) @@ -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) } } - }, + } } }