Skip to content

Commit

Permalink
Add basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
SamyPesse committed Jun 10, 2016
1 parent ae58b2c commit cbb8a1e
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 22 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# draft-js-prism

[![NPM version](https://badge.fury.io/js/draft-js-prism.svg)](http://badge.fury.io/js/draft-js-prism)
[![Build Status](https://travis-ci.org/SamyPesse/draft-js-prism.svg?branch=master)](https://travis-ci.org/SamyPesse/draft-js-prism)

`draft-js-prism` is a decorator for DraftJS to highlight code blocks using [Prism](https://github.com/PrismJS/prism). It only decorates code blocks with syntax highlighting, if you're interested in providing a correct edition UX for code blocks, take a look at [draft-js-code](https://github.com/SamyPesse/draft-js-code).

Expand Down
47 changes: 47 additions & 0 deletions lib/__tests__/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var Draft = require('draft-js');
var expect = require('expect');

var PrismDecorator = require('../');

describe('PrismDecorator', function() {
var jsBlock = new Draft.ContentBlock({
type: 'code-block',
text: 'var a = "test"'
});

it('should not fail for block without syntax', function() {
var decorator = new PrismDecorator();
var out = decorator.getDecorations(jsBlock);
expect(out.toJS()).toEqual([
null, null, null, null, null, null, null, null,
null, null, null, null, null, null
]);
});

it('should use defaultSyntax option', function() {
var decorator = new PrismDecorator({
defaultSyntax: 'javascript'
});
var out = decorator.getDecorations(jsBlock);
expect(out.toJS()).toEqual([
'-tok0', '-tok0', '-tok0',
null, null, null,
'-tok6', null,
'-tok8', '-tok8', '-tok8', '-tok8', '-tok8', '-tok8' ]);
});

it('should use not process non code-block by default', function() {
var block = new Draft.ContentBlock({
type: 'unstyled',
text: 'var'
});
var decorator = new PrismDecorator({
defaultSyntax: 'javascript'
});
var out = decorator.getDecorations(block);
expect(out.toJS()).toEqual([
null, null, null
]);
});
});

30 changes: 15 additions & 15 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ function PrismDecorator(options) {
}

/**
Return list of decoration IDs per character
@param {ContentBlock}
@return {List<String>}
*/
* Return list of decoration IDs per character
*
* @param {ContentBlock}
* @return {List<String>}
*/
PrismDecorator.prototype.getDecorations = function(block) {
var tokens, token, tokenId, resultId, offset = 0;
var filter = this.options.get('filter');
Expand Down Expand Up @@ -61,21 +61,21 @@ PrismDecorator.prototype.getDecorations = function(block) {
};

/**
Return component to render a decoration
@param {String}
@return {Function}
*/
* Return component to render a decoration
*
* @param {String}
* @return {Function}
*/
PrismDecorator.prototype.getComponentForKey = function(key) {
return this.options.get('render');
};

/**
Return props to render a decoration
@param {String}
@return {Object}
*/
* Return props to render a decoration
*
* @param {String}
* @return {Object}
*/
PrismDecorator.prototype.getPropsForKey = function(key) {
var parts = key.split('-');
var blockKey = parts[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function defaultRender(props) {

var PrismOptions = Immutable.Record({
// Default language to use
defaultSyntax: String('javascript'),
defaultSyntax: null,

// Filter block before highlighting
filter: defaultFilter,
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Code highlighting for DraftJS",
"main": "./lib/index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"test": "./node_modules/.bin/mocha \"./lib/__tests__/*.js\" --bail --reporter=list",
"build": "browserify -t [ babelify --presets [ es2015 react ] ] ./demo/main.js > ./demo/dist.js; cp ./node_modules/prismjs/themes/prism.css ./demo/prism.css; cp ./node_modules/draft-js/dist/Draft.css ./demo/draft.css",
"deploy": "npm run build; gh-pages -d ./demo",
"watch": "watch 'npm run build' ./lib",
Expand All @@ -30,16 +30,18 @@
"prismjs": "^1.5.0"
},
"devDependencies": {
"draft-js": "^0.7.0",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babelify": "^7.2.0",
"browserify": "^13.0.0",
"draft-js": "^0.7.0",
"expect": "^1.20.1",
"gh-pages": "^0.11.0",
"http-server": "^0.9.0",
"watch": "^0.18.0",
"parallelshell": "2.0.0"
"mocha": "^2.5.3",
"parallelshell": "2.0.0",
"react": "^15.1.0",
"react-dom": "^15.1.0",
"watch": "^0.18.0"
}
}

0 comments on commit cbb8a1e

Please sign in to comment.