Skip to content

Commit 9b01c33

Browse files
committed
Switch to ava and use PureComponent
1 parent 8cc8b27 commit 9b01c33

File tree

14 files changed

+112
-111
lines changed

14 files changed

+112
-111
lines changed

.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"extends": "airbnb",
33
"parser": "babel-eslint",
44
"rules": {
5-
"no-underscore-dangle": 0
5+
"no-underscore-dangle": 0,
6+
"react/jsx-filename-extension": 0
67
},
78
"globals": {
89
"HighlightResult": false

.flowconfig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ flow/
1111
munge_underscores=true
1212
esproposal.class_static_fields=enable
1313
esproposal.class_instance_fields=enable
14-
unsafe.enable_getters_and_setters=true
1514
suppress_comment=\\(.\\|\n\\)*\\$FlowIssue

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@ node_js:
44
- "4"
55
- "5"
66
- "6"
7+
script: npm run check

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
[![Build Status](https://travis-ci.org/chili-labs/react-fast-highlight.svg?branch=master)](https://travis-ci.org/chili-labs/react-fast-highlight)
55

6+
## Requirements
7+
8+
Version 1.x works with React 0.14 and <=15.2
9+
10+
Version 2.x works with React >=15.3
11+
612
## Usage
713

814
`npm install --save react-fast-highlight`

package.json

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
"scripts": {
77
"check": "npm run lint && npm run flow && npm test",
88
"clean": "rimraf lib",
9-
"build": "cross-env NODE_ENV=production babel src --out-dir lib --ignore __tests__",
9+
"build": "babel src --out-dir lib",
1010
"flow": "flow",
1111
"lint": "eslint src",
1212
"precommit": "npm run check",
1313
"prepublish": "npm run clean && npm run build",
1414
"preversion": "npm run check",
15-
"test": "cross-env NODE_ENV=test mocha tests/setup.js $(find src -path '*__tests__/*.spec.js') --compilers js:babel-register"
15+
"test": "ava"
1616
},
1717
"repository": {
1818
"type": "git",
19-
"url": "git+https://github.com/chili-labs/react-fast-highlight.git"
19+
"url": "https://github.com/chili-labs/react-fast-highlight.git"
2020
},
2121
"files": [
2222
"lib"
@@ -35,19 +35,17 @@
3535
},
3636
"homepage": "https://github.com/chili-labs/react-fast-highlight#readme",
3737
"peerDependencies": {
38-
"react": "^0.14.0 || ^15.0.0"
38+
"react": "^15.3.0"
3939
},
4040
"devDependencies": {
41+
"ava": "^0.15.2",
4142
"babel-cli": "^6.4.5",
42-
"babel-eslint": "^6.0.0-beta",
43+
"babel-eslint": "^6.0.0",
4344
"babel-preset-es2015": "^6.3.13",
4445
"babel-preset-es2015-loose": "^7.0.0",
4546
"babel-preset-react": "^6.3.13",
4647
"babel-preset-stage-1": "^6.3.13",
47-
"babel-register": "^6.4.3",
48-
"chai": "^3.4.1",
49-
"cross-env": "^2.0.0",
50-
"dirty-chai": "^1.2.2",
48+
"babel-register": "^6.11.6",
5149
"enzyme": "^2.2.0",
5250
"eslint": "^3.0.0",
5351
"eslint-config-airbnb": "^10.0.0",
@@ -57,18 +55,21 @@
5755
"flow-bin": "^0.30.0",
5856
"husky": "^0.11.1",
5957
"jsdom": "^9.1.0",
60-
"mocha": "^3.0.0",
61-
"mocha-jsdom": "^1.1.0",
62-
"react": "^15.0.0",
63-
"react-addons-test-utils": "^15.0.0",
64-
"react-dom": "^15.0.0",
58+
"react": "^15.3.0",
59+
"react-addons-test-utils": "^15.3.0",
60+
"react-dom": "^15.3.0",
6561
"rimraf": "^2.5.0",
66-
"sinon": "^1.17.2",
67-
"sinon-chai": "^2.8.0"
62+
"testdouble": "^1.6.0"
6863
},
6964
"dependencies": {
7065
"classnames": "^2.2.3",
71-
"highlight.js": "^9.0.0",
72-
"react-addons-shallow-compare": "^15.0.0"
66+
"highlight.js": "^9.0.0"
67+
},
68+
"ava": {
69+
"require": [
70+
"babel-register",
71+
"./test/helpers/setup-browser-env.js"
72+
],
73+
"babel": "inherit"
7374
}
7475
}

src/__tests__/index.spec.js

Lines changed: 0 additions & 11 deletions
This file was deleted.

src/components/Highlight.js

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
/* @flow */
2-
import React, { Component, Element, PropTypes } from 'react';
3-
import shallowCompare from 'react-addons-shallow-compare';
2+
import React, { PureComponent, PropTypes } from 'react';
43
import hljs from 'highlight.js';
54
import cx from 'classnames';
65

@@ -16,7 +15,7 @@ type State = {
1615
language: ?string,
1716
};
1817

19-
export default class Highlight extends Component {
18+
export default class Highlight extends PureComponent {
2019

2120
state: State = {
2221
highlightedCode: null,
@@ -27,17 +26,13 @@ export default class Highlight extends Component {
2726
this._highlightCode();
2827
}
2928

30-
shouldComponentUpdate(nextProps: Props, nextState: State): boolean {
31-
return shallowCompare(this, nextProps, nextState);
32-
}
33-
3429
componentDidUpdate() {
3530
this._highlightCode();
3631
}
3732

3833
props: Props;
3934

40-
get initialCode(): string {
35+
getInitialCode() {
4136
const type = typeof this.props.children;
4237
if (type !== 'string') {
4338
throw new Error(`Children of <Highlight> must be a string. '${type}' supplied`);
@@ -46,40 +41,40 @@ export default class Highlight extends Component {
4641
return this.props.children;
4742
}
4843

49-
get highlightCallback(): (resolve: Function) => HighlightResult {
44+
getHighlightCallback() {
5045
let callback;
5146

5247
if (this.props.languages && this.props.languages.length === 1) {
5348
const language:string = this.props.languages[0];
54-
callback = (resolve) =>
55-
resolve(hljs.highlight(language, this.initialCode));
49+
callback = (resolve: (x: *) => void) =>
50+
resolve(hljs.highlight(language, this.getInitialCode()));
5651
} else {
57-
callback = (resolve) =>
58-
resolve(hljs.highlightAuto(this.initialCode, this.props.languages));
52+
callback = (resolve: (x: *) => void) =>
53+
resolve(hljs.highlightAuto(this.getInitialCode(), this.props.languages));
5954
}
6055

6156
return callback;
6257
}
6358

64-
_highlightCode(): void {
59+
_highlightCode() {
6560
const worker = this.props.worker;
6661
if (worker) {
6762
worker.onmessage = event => this.setState({
6863
highlightedCode: event.data.value,
6964
language: event.data.language,
7065
});
71-
worker.postMessage({ code: this.initialCode, languages: this.props.languages });
66+
worker.postMessage({ code: this.getInitialCode(), languages: this.props.languages });
7267
} else {
73-
const promise = new Promise(this.highlightCallback);
68+
const promise = new Promise(this.getHighlightCallback());
7469

7570
promise.then(
7671
result => this.setState({ highlightedCode: result.value, language: result.language })
7772
);
7873
}
7974
}
8075

81-
render(): ?Element {
82-
const code: ?string = this.state.highlightedCode;
76+
render() {
77+
const code = this.state.highlightedCode;
8378
const classes = cx(this.props.className, 'hljs', this.state.language);
8479

8580
if (code) {
@@ -90,7 +85,7 @@ export default class Highlight extends Component {
9085
);
9186
}
9287

93-
return <pre><code className={classes}>{this.initialCode}</code></pre>;
88+
return <pre><code className={classes}>{this.getInitialCode()}</code></pre>;
9489
}
9590
}
9691

src/components/__tests__/Highlight.spec.js

Lines changed: 0 additions & 54 deletions
This file was deleted.

src/worker.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
/* global postMessage, onmessage:true */
22
/* @flow */
3-
43
import hjs from 'highlight.js';
54

5+
declare function postMessage(result: Object): void;
6+
67
onmessage = event => {
78
const { code, languages } = event.data;
89
let result;

test/.eslintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rules": {
3+
"import/no-extraneous-dependencies": [2, {"devDependencies": true}]
4+
}
5+
}

test/components/Highlight.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import React from 'react';
2+
import td from 'testdouble';
3+
import test from 'ava';
4+
import { mount, shallow } from 'enzyme';
5+
6+
const hjs = td.replace('highlight.js');
7+
const Highlight = require('../../src/components/Highlight').default;
8+
9+
test.cb('no language - calls correct highlightCall', t => {
10+
td
11+
.when(hjs.highlightAuto('test', undefined))
12+
.thenReturn({ value: 'othertest', language: 'xml' });
13+
const wrapper = mount(<Highlight>test</Highlight>);
14+
15+
setTimeout(() => {
16+
t.is(wrapper.state('language'), 'xml');
17+
t.is(wrapper.state('highlightedCode'), 'othertest');
18+
t.end();
19+
}, 1);
20+
});
21+
22+
test.cb('one language - calls correct highlightCall', t => {
23+
td
24+
.when(hjs.highlight('js', 'test'))
25+
.thenReturn({ value: 'othertest', language: 'js' });
26+
const wrapper = mount(<Highlight languages={['js']}>test</Highlight>);
27+
28+
setTimeout(() => {
29+
t.is(wrapper.state('language'), 'js');
30+
t.is(wrapper.state('highlightedCode'), 'othertest');
31+
t.end();
32+
}, 1);
33+
});
34+
35+
test.cb('multiple languages - calls correct highlightCall', t => {
36+
td
37+
.when(hjs.highlightAuto('test', ['js', 'html']))
38+
.thenReturn({ value: 'othertest', language: 'js' });
39+
const wrapper = mount(<Highlight languages={['js', 'html']}>test</Highlight>);
40+
41+
setTimeout(() => {
42+
t.is(wrapper.state('language'), 'js');
43+
t.is(wrapper.state('highlightedCode'), 'othertest');
44+
t.end();
45+
}, 1);
46+
});
47+
48+
test('className is passed through', t => {
49+
const wrapper = shallow(<Highlight className="foobar">test</Highlight>);
50+
51+
t.true(wrapper.childAt(0).hasClass('foobar'));
52+
});

test/helpers/setup-browser-env.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
global.document = require('jsdom').jsdom('<body></body>');
2+
global.window = document.defaultView;
3+
global.navigator = window.navigator;

test/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import React from 'react';
2+
import { shallow } from 'enzyme';
3+
import test from 'ava';
4+
import Highlight from '../src/index';
5+
6+
test('exports default component', t => {
7+
t.notThrows(() => shallow(<Highlight>test</Highlight>));
8+
});

tests/setup.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)