Skip to content
This repository has been archived by the owner on May 3, 2021. It is now read-only.

Commit

Permalink
Write tests for utilities and implement basic functionality of data t… (
Browse files Browse the repository at this point in the history
#1)

* Write tests for utilities and implement basic functionality of data table

* Add build status badge to readme
  • Loading branch information
thechinedu committed Aug 8, 2016
1 parent dca9f17 commit 2504591
Show file tree
Hide file tree
Showing 20 changed files with 783 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": ["es2015", "stage-2", "react"]
}
40 changes: 40 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module.exports = {
"env": {
"browser": true,
"commonjs": true,
"es6": true
},
"extends": ["eslint:recommended", "plugin:react/recommended"],
"parserOptions": {
"ecmaFeatures": {
"experimentalObjectRestSpread": true,
"jsx": true
},
"sourceType": "module"
},
"plugins": [
"react"
],
"rules": {
"indent": [
"error",
2
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
},
"globals": {
"describe": 0,
"it": 0
}
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
dist
coverage
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
src
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# Mui Data Table

Data table implementation for [react material-ui](http://www.material-ui.com/#/)

[![CircleCI](https://circleci.com/gh/andela-cdaniel/mui-data-table/tree/master.svg?style=shield)](https://circleci.com/gh/andela-cdaniel/mui-data-table/tree/master)
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./dist/index');
61 changes: 61 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
{
"name": "mui-data-table",
"version": "0.1.0",
"description": "Data table for react material-ui",
"repository": {
"type": "git",
"url": "https://github.com/andela-cdaniel"
},
"author": "Daniel Chinedu",
"license": "MIT",
"bugs": {
"url": "https://github.com/andela-cdaniel"
},
"homepage": "https://github.com/andela-cdaniel",
"keywords": [
"react",
"react material-ui data table",
"react material-ui",
"react data table",
"material ui",
"material-ui data table"
],
"scripts": {
"prepublish": "babel src --ignore __tests__ --out-dir ./dist",
"lint": "eslint ./src",
"lintfix": "eslint ./src --fix",
"test": "mocha --require babel-register src/__tests__/**/*.js",
"test:coverage": "istanbul cover _mocha -- --require babel-register src/__tests__/**/*.js"
},
"devDependencies": {
"babel-cli": "^6.6.4",
"babel-core": "^6.7.4",
"babel-eslint": "^6.0.2",
"babel-plugin-transform-es2015-modules-umd": "^6.6.5",
"babel-polyfill": "^6.7.4",
"babel-preset-es2015": "^6.6.0",
"babel-preset-react": "^6.5.0",
"babel-preset-stage-2": "^6.5.0",
"babel-register": "^6.11.6",
"chai": "^3.5.0",
"enzyme": "^2.2.0",
"eslint": "^2.7.0",
"eslint-plugin-babel": "^3.1.0",
"eslint-plugin-react": "^4.2.3",
"istanbul": "1.0.0-alpha.2",
"jsdom": "^8.1.0",
"mocha": "^2.4.5",
"nodemon": "^1.9.1",
"react": "^15.0.0",
"react-addons-test-utils": "^15.0.0",
"react-dom": "^15.3.0",
"sinon": "^1.17.3"
},
"peerDependencies": {
"react": "~0.14.8 || ^15.0.0",
"material-ui": "^0.15.1"
},
"dependencies": {
"babel-runtime": "^6.6.1"
}
}
31 changes: 31 additions & 0 deletions src/__tests__/utils/handleCustomRender.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import chai from 'chai';
import { hasCustomRender, callCustomRender } from '../../utils/handleCustomRender';
import { objCopy } from './utilities';

const expect = chai.expect;

const data = [{ property: 'client_name', title: 'Client', renderAs: function (data) {
return data.message;
} }];

describe('The render utility functions', function () {
describe('#hasCustomRender', function () {
it('should return true if the property in the object array contains a renderAs property', function () {
expect(hasCustomRender('client_name', data)).to.be.true;
});

it('should return false if the property does not contain an html property', function () {
const clone = objCopy(data);
delete clone[0].renderAs;

expect(hasCustomRender('client_name', clone)).to.be.false;
});
});

describe('#callCustomRender', function () {
it('should call the callback function and return it\'s result', function () {

expect(callCustomRender('client_name', data, {message: 'hello world'})).to.equal('hello world');
});
});
});
29 changes: 29 additions & 0 deletions src/__tests__/utils/handleHtmlProp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import chai from 'chai';
import { extractHtml, hasHtml } from '../../utils/handleHtmlProp';
import { objCopy } from './utilities';

const expect = chai.expect;

const data = [{ property: 'client_name', title: 'Client', html: '<span></span>' }];

describe('The html utility functions', function () {
describe('#hasHtml', function () {
it('should return true if the property in the object array contains an html property', function () {
expect(hasHtml('client_name', data)).to.be.true;
});

it('should return false if the property does not contain an html property', function () {
const clone = objCopy(data);
delete clone[0].html;

expect(hasHtml('client_name', clone)).to.be.false;
});
});

describe('#extractHtml', function () {
it('should extract and return the value of the html property', function () {

expect(extractHtml('client_name', data)).to.equal('<span></span>');
});
});
});
19 changes: 19 additions & 0 deletions src/__tests__/utils/injectProp.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import chai from 'chai';
import injectProp from '../../utils/injectProp';

const expect = chai.expect;
const data = [
{name: 'ade', location: 'Lagos', age: 21, mood: 'happy', property: 'id'},
{name: 'tunde', location: 'Edo', age: 25, mood: 'angry'},
{name: 'bayo', location: 'Ibadan', age: 28, mood: 'scared'}
];

describe('The injectProp function', function () {
it('should return a new array containing custom property attribute if it is not set in the config', function () {
expect(injectProp(data)).to.eql([
{name: 'ade', location: 'Lagos', age: 21, mood: 'happy', property: 'id'},
{name: 'tunde', location: 'Edo', age: 25, mood: 'angry', property: 'MuiDataTableProp-1'},
{name: 'bayo', location: 'Ibadan', age: 28, mood: 'scared', property: 'MuiDataTableProp-2'}
]);
});
});
90 changes: 90 additions & 0 deletions src/__tests__/utils/paginate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import chai from 'chai';
import Paginate from '../../utils/paginate';

import { lastElem } from './utilities';

const expect = chai.expect;
const data = [
{name: 'ade', location: 'Lagos', age: 21, mood: 'happy'},
{name: 'tunde', location: 'Edo', age: 25, mood: 'angry'},
{name: 'bayo', location: 'Ibadan', age: 28, mood: 'scared'},
{name: 'ade', location: 'Lagos', age: 21, mood: 'happy'},
{name: 'tunde', location: 'Edo', age: 25, mood: 'angry'},
{name: 'bayo', location: 'Ibadan', age: 28, mood: 'curious'}
];
const paginateObj = new Paginate(data);

describe('The Paginate class', function () {
it('should raise an error if called without the new keyword', function () {
expect(Paginate).to.throw(TypeError);
});


describe('#showingCalc', function () {
it('should return a string after execution', function () {
expect(typeof paginateObj.showingCalc([1,2,3,4,5])).to.eql('string');
});

it('should return 1 up to first argument of array if the length of the array isn\'t greater than 1', function () {
expect(paginateObj.showingCalc([1])).to.eql('1 - 1');
});

it('should add one to the result of adding all items, with the exception of the\
last item in the array to get the start value', function () {
expect(paginateObj.showingCalc([1,2,3]).split('-')[0].trim()).to.eql('4');
});

it('should add all items in the array to get the end value', function () {
expect(paginateObj.showingCalc([1,2,3]).split('-')[1].trim()).to.eql('6');
});

it('should return the correct page range based on array argument', function () {
expect(paginateObj.showingCalc([1,2,3])).to.eql('4 - 6');
expect(paginateObj.showingCalc([5,5,4])).to.eql('11 - 14');
expect(paginateObj.showingCalc([3,2,2])).to.eql('6 - 7');
});
});

describe('#perPage', function () {
it('should return a new instance of Paginate', function () {
expect(paginateObj.perPage(2) instanceof Paginate).to.be.true;
});

it('should append a pagination info object to every item in the paginated array', function () {
const val = paginateObj.perPage(3).arr;

expect(val[0]).to.have.lengthOf(4);
expect(val[1]).to.have.lengthOf(4);
expect(lastElem(val[0])).have.ownProperty('paginationInfo');
});

it('should paginate with a default of 5 if no argument is passed in', function () {
const val = paginateObj.perPage().arr[0];

expect(val.slice(0, val.length - 1)).to.have.lengthOf(5);
});

});

describe('#page', function () {
it('should return the item if it finds the item at a given index', function () {
const val = paginateObj.perPage(3).page(1);

expect(val.slice(0, val.length - 1)).to.eql([ data[0], data[1], data[2] ]);
});

it('should return the last item if a number higher than the total amount of pages is passed in', function () {
const val = paginateObj.perPage(3).page(5);

expect(val.slice(0, val.length - 1)).to.eql([ data[3], data[4], data[5] ]);
});

it('should return an empty array if the original array is empty', function () {
const temp = [];
const val = new Paginate(temp).perPage(5).page(1);

expect(val).to.eql([]);
});
});

});
37 changes: 37 additions & 0 deletions src/__tests__/utils/search.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import chai from 'chai';
import search from '../../utils/search';

const expect = chai.expect;
const data = [
{name: 'ade', location: 'Lagos', age: 21, mood: 'happy'},
{name: 'tunde', location: 'Edo', age: 25, mood: 'angry'},
{name: 'bayo', location: 'Ibadan', age: 28, mood: 'scared'}
];

describe('The search function', function () {
it('should return the original array if the word being searched for is empty', function () {
expect(search('name', '', data)).to.eql(data);
});

it('should return an empty array if the word isn\'t found in the list', function () {
expect(search('name', 'damilare', data)).to.eql([]);
});

it('should return the approriate result that match the word if found in the array', function () {
expect(search('name', 'ade', data)).to.eql([data[0]]);
});

it('should return results when there is a partial match', function () {
expect(search('name', 'a', data)).to.eql([data[0], data[2]]);
});

it('should be able to search using multiple keys', function () {
expect(search('location|name|mood', 'angry', data)).to.eql([data[1]]);
expect(search('location|name|mood', 'Lagos', data)).to.eql([data[0]]);
expect(search('location|name|mood|age', '28', data)).to.eql([data[2]]);
});

it('should perform case insensitive search(es)', function () {
expect(search('name', 'TUNDE', data)).to.eql([data[1]]);
});
});
13 changes: 13 additions & 0 deletions src/__tests__/utils/utilities.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export const objCopy = (arr) => {
const res = [];

arr.forEach(function (item) {
if (typeof item === 'object') res.push(Object.assign({}, item, {}));
});

return res;
};

export const lastElem = (arr) => {
return arr[arr.length - 1];
};
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import MuiDataTable from './lib/mui-data-table';

export { MuiDataTable };
Loading

0 comments on commit 2504591

Please sign in to comment.