Skip to content

Commit 4dc9ffe

Browse files
committed
Readme added + tests
1 parent e04ab74 commit 4dc9ffe

File tree

5 files changed

+130
-13
lines changed

5 files changed

+130
-13
lines changed

README.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
# react-fast-highlight
2+
###### A fast react component wrapper for highlight.js
3+
4+
[![Build Status](https://travis-ci.org/chili-labs/react-fast-highlight.svg?branch=master)](https://travis-ci.org/chili-labs/react-fast-highlight)
5+
6+
## Usage
7+
8+
`npm install --save react-fast-highlight`
9+
10+
```js
11+
import React from 'react';
12+
import Highlight from 'react-fast-highlight';
13+
14+
class App extends React.Component {
15+
16+
render() {
17+
const code = 'let t = 0.5 * 5 * 4;';
18+
19+
return (
20+
{/*
21+
`languages` is an optional property to set the languages that highlight.js should pick from.
22+
By default it is empty, which means highlight.js will pick from all available languages.
23+
If only one language is provided, this language will be used without doing checks beforehand.
24+
(Be aware that automatic language detection is not as fast as when specifing a language.)
25+
26+
`worker` is used to take advantage of the possibility to do the highlighting work in a
27+
web-worker. As different module-bundlers use different ways to load web-workers, it is up
28+
to you to load the webworker and supply it to the highlight component. (see example)
29+
Currently every instance of the highlight component needs its own web-worker.
30+
31+
`className` sets additional class names on the generated html markup.
32+
*/}
33+
<Highlight
34+
languages={['js']}
35+
className="my-class"
36+
>
37+
{code}
38+
</Highlight>
39+
);
40+
}
41+
```
42+
43+
### Webworker
44+
45+
#### Webpack
46+
47+
To make web-workers working with webpack you additionally need to install `worker-loader`.
48+
49+
`npm install --save worker-loader react-fast-highlight`
50+
51+
```js
52+
import React from 'react';
53+
import Highlight from 'react-fast-highlight';
54+
import Worker from 'worker!react-fast-highlight/lib/worker';
55+
56+
class App extends React.Component {
57+
58+
render() {
59+
const code = 'let t = 0.5 * 5 * 4;';
60+
61+
return (
62+
<Highlight
63+
languages={['js']}
64+
worker={new Worker}
65+
>
66+
{code}
67+
</Highlight>
68+
);
69+
}
70+
```
71+
72+
73+
## License
74+
75+
react-fast-highlight is licensed under the MIT license.

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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",
9+
"build": "cross-env NODE_ENV=production babel src --out-dir lib --ignore __tests__",
1010
"flow": "flow",
1111
"lint": "eslint src",
1212
"prepublish": "npm run check && npm run clean && npm run build",
@@ -51,7 +51,8 @@
5151
"jsdom": "^7.2.2",
5252
"mocha": "^2.3.4",
5353
"react": "^0.14.0",
54-
"react-addons-test-utils": "^0.14.6",
54+
"react-addons-test-utils": "^0.14.0",
55+
"react-dom": "^0.14.0",
5556
"rimraf": "^2.5.0",
5657
"sinon": "^1.17.2",
5758
"sinon-chai": "^2.8.0"

src/components/Highlight.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export default class Highlight extends Component<void, Props, State> {
4040
get initialCode(): ?string {
4141
const type = typeof this.props.children;
4242
if (type !== 'string') {
43-
throw new Error(`Children of <Highlight> must be a string. ${type} supplied`);
43+
throw new Error(`Children of <Highlight> must be a string. '${type}' supplied`);
4444
}
4545

4646
return this.props.children;

src/components/__tests__/Highlight.spec.js

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,62 @@
22
/* eslint-disable no-unused-expressions */
33

44
import React from 'react';
5+
import ReactDOM from 'react-dom';
56
import TestUtils from 'react-addons-test-utils';
67
import Highlight from '../Highlight';
78
import { expect } from 'chai';
89
import hjs from 'highlight.js';
910
import sinon from 'sinon';
1011

11-
describe('Button', () => {
12-
it('has correct label', () => {
13-
const highlightAuto = sinon.spy(hjs, 'highlightAuto');
14-
TestUtils.renderIntoDocument(
15-
<Highlight>test</Highlight>
16-
);
12+
describe('Highlight', () => {
13+
describe('no languages', () => {
14+
it('calls correct highlightCall', () => {
15+
const highlightAuto = sinon.spy(hjs, 'highlightAuto');
16+
TestUtils.renderIntoDocument(
17+
<Highlight>test</Highlight>
18+
);
19+
20+
expect(highlightAuto).to.have.been.calledOnce;
21+
expect(highlightAuto).to.have.been.calledWith('test');
22+
23+
highlightAuto.restore();
24+
});
25+
});
1726

18-
expect(highlightAuto).to.have.been.calledOnce;
19-
expect(highlightAuto).to.have.been.calledWith('test');
27+
describe('one language', () => {
28+
it('calls correct highlightCall', () => {
29+
const highlight = sinon.spy(hjs, 'highlight');
30+
TestUtils.renderIntoDocument(
31+
<Highlight languages={['js']}>test</Highlight>
32+
);
33+
34+
expect(highlight).to.have.been.calledOnce;
35+
expect(highlight).to.have.been.calledWith('js', 'test');
36+
37+
highlight.restore();
38+
});
39+
});
40+
41+
describe('multiple languages', () => {
42+
it('calls correct highlightCall', () => {
43+
const highlightAuto = sinon.spy(hjs, 'highlightAuto');
44+
TestUtils.renderIntoDocument(
45+
<Highlight languages={['js', 'html']}>test</Highlight>
46+
);
47+
48+
expect(highlightAuto).to.have.been.calledOnce;
49+
expect(highlightAuto).to.have.been.calledWith('test', ['js', 'html']);
50+
51+
highlightAuto.restore();
52+
});
53+
});
54+
55+
it('className is passed through', () => {
56+
const component = TestUtils.renderIntoDocument(
57+
<Highlight className="foobla">test</Highlight>
58+
);
59+
const domElement = ReactDOM.findDOMNode(component);
2060

21-
highlightAuto.restore();
61+
expect(domElement.children[0].classList.contains('foobla')).to.equal.true;
2262
});
2363
});

src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export Highlight from './components/Highlight';
1+
import Highlight from './components/Highlight';
2+
export default Highlight;

0 commit comments

Comments
 (0)