Skip to content

Commit 6a97c51

Browse files
coreyleelarsonCorey Larson
authored and
Corey Larson
committed
Ensures Typekit works on server-rendered and non-server-rendered applications.
1 parent 26434df commit 6a97c51

File tree

6 files changed

+57
-27
lines changed

6 files changed

+57
-27
lines changed

.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"presets": ["latest", "react"],
2+
"presets": ["env", "react"],
33
}

.eslintrc

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
{
2-
"parser": "babel-eslint",
3-
"extends": "airbnb",
4-
"env": {
5-
"mocha": true,
6-
"node": true
2+
"extends": ["eslint:recommended", "plugin:react/recommended"],
3+
"parserOptions": {
4+
"ecmaFeatures": {
5+
"experimentalObjectRestSpread": true,
6+
"jsx": true
7+
},
8+
"sourceType": "module"
79
},
8-
"rules": {
9-
"global-require": 0,
10-
"import/no-extraneous-dependencies": 0,
11-
"no-unused-vars": 1,
12-
"react/jsx-filename-extension": 0,
13-
"react/forbid-prop-types": 0
10+
"plugins": [
11+
"react"
12+
],
13+
"env": {
14+
"browser": true,
15+
"mocha": true
1416
}
1517
}

package.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-typekit",
3-
"version": "1.0.3",
3+
"version": "1.0.4",
44
"description": "React component that generates Typekit script",
55
"author": "Corey Larson <[email protected]> (http://github.com/coreyleelarson)",
66
"license": "MIT",
@@ -21,12 +21,12 @@
2121
"clean": "rimraf lib",
2222
"lint": "eslint ./src/**",
2323
"prepublish": "npm run clean && npm run build",
24-
"test": "mocha ./src/**/*.spec.js --compilers js:babel-register"
24+
"test": "mocha --require test/setup.js ./src/**/*.spec.js --compilers js:babel-register"
2525
},
2626
"devDependencies": {
2727
"babel-cli": "^6.18.0",
2828
"babel-eslint": "^7.1.1",
29-
"babel-preset-latest": "^6.16.0",
29+
"babel-preset-env": "^1.2.1",
3030
"babel-preset-react": "^6.16.0",
3131
"babel-register": "^6.18.0",
3232
"chai": "^3.5.0",
@@ -35,7 +35,9 @@
3535
"eslint-config-airbnb": "^13.0.0",
3636
"eslint-plugin-import": "^2.2.0",
3737
"eslint-plugin-jsx-a11y": "^2.2.3",
38-
"eslint-plugin-react": "^6.8.0",
38+
"eslint-plugin-react": "^6.10.0",
39+
"jsdom": "^9.11.0",
40+
"lodash": "^4.17.4",
3941
"mocha": "^3.2.0",
4042
"react": "^15.4.1",
4143
"react-addons-test-utils": "^15.4.1",

src/Typekit.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
/* eslint react/no-danger: 0 */
2-
import React, { PropTypes } from 'react';
1+
import { Component, PropTypes } from 'react';
32
import buildScript from './utilities/buildScript';
43

5-
const Typekit = ({ kitId }) => {
6-
const script = buildScript(kitId);
7-
return kitId ? (<script dangerouslySetInnerHTML={{ __html: script }} />) : false;
8-
};
4+
class Typekit extends Component {
5+
componentDidMount() {
6+
const is_rendered = document.querySelectorAll('.react-typekit').length > 0;
7+
if (!is_rendered) {
8+
const { kitId } = this.props;
9+
const script = document.createElement('script');
10+
script.className = 'react-typekit';
11+
script.async = true;
12+
script.innerHTML = buildScript(kitId);
13+
document.body.appendChild(script);
14+
}
15+
}
16+
17+
render() {
18+
return false;
19+
}
20+
}
921

1022
Typekit.propTypes = {
1123
kitId: PropTypes.string,

src/Typekit.spec.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
1-
/* eslint no-underscore-dangle: 0 */
1+
import { filter } from 'lodash';
22
import React from 'react';
33
import { expect } from 'chai';
4-
import { shallow } from 'enzyme';
4+
import { mount } from 'enzyme';
55
import Typekit from './Typekit';
66
import buildScript from './utilities/buildScript';
77

88
describe('<Typekit />', () => {
99
it('should output typekit script when kitId prop is present', () => {
1010
const kitId = 'abc123';
11-
const wrapper = shallow(<Typekit kitId={kitId} />);
12-
expect(wrapper.find('script').node.props.dangerouslySetInnerHTML.__html).to.equal(buildScript(kitId));
11+
mount(<Typekit kitId={kitId} />);
12+
const matches = filter(document.querySelectorAll('script'), (script) => script.innerHTML === buildScript(kitId));
13+
expect(matches).to.have.length(1);
1314
});
1415

1516
it('should output nothing when kitId prop is not present', () => {
16-
const wrapper = shallow(<Typekit />);
17+
const wrapper = mount(<Typekit />);
1718
expect(wrapper.html()).to.equal(null);
1819
});
1920
});

test/setup.js

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { jsdom } from 'jsdom';
2+
3+
global.document = jsdom('');
4+
global.window = document.defaultView;
5+
Object.keys(document.defaultView).forEach((property) => {
6+
if (typeof global[property] === 'undefined') {
7+
global[property] = document.defaultView[property];
8+
}
9+
});
10+
11+
global.navigator = {
12+
userAgent: 'node.js'
13+
};

0 commit comments

Comments
 (0)