var ReactTestUtils = React.addons.TestUtils;
var expect = chai.expect;
var ReactableTestUtils = {
resetTestEnvironment: function() {
ReactDOM.unmountComponentAtNode($('div#test-node')[0]);
$('div#test-node').remove();
},
// Expect the row specified to have the specified class
expectRowClass: function(rowIndex, className) {
var row = $($('#table tbody.reactable-data tr')[rowIndex]);
expect(row).to.have.class(className);
},
// Expect the columns of a the data row specified to have the values in the array as their text values
expectRowText: function(rowIndex, textArray) {
var row = $($('#table tbody.reactable-data tr')[rowIndex]).find('td');
expect(row.length).to.equal(textArray.length);
for (var i = 0; i < row.length; i++) {
expect($(row[i])).to.have.text(textArray[i]);
}
},
testNode: function() {
var testNode = $('
').attr('id', 'test-node');
$('body').append(testNode);
testNode.empty();
return testNode[0];
}
};
describe('Reactable', function() {
describe("with null children", function(){
before(function () {
ReactDOM.render(
{null}
{null}
{null}
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
});
describe('directly passing a data array', function() {
before(function() {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position']);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
it('handles null values', function() {
ReactableTestUtils.expectRowText(3, ['Leonor Hyatt', '', '']);
});
});
describe('adding
s to the
', function() {
before(function() {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
});
describe('adding
s to the
s', function() {
context('with only one
', function() {
before(function() {
ReactDOM.render(
Griffin SmithLee SalminenIan Zhang,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql(['Name']);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['Ian Zhang']);
});
});
context('with multiple
s', function () {
context('with plain text', function() {
before(function() {
ReactDOM.render(
Griffin Smith18Lee Salminen23Developer28,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
});
});
context('with React.DOM nodes inside', function() {
before(function() {
ReactDOM.render(
Griffin Smith18Lee Salminen23Developer28,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
});
context('with null
s', function(){
before(function() {
ReactDOM.render(
Griffin Smith
{null}
Lee Salminen23Developer28,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
});
context('with null
s', function(){
before(function() {
ReactDOM.render(
Griffin Smith18
{null}
Developer28,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['', '28', 'Developer']);
});
});
context("when one of the
s is null", function(){
before(function () {
ReactDOM.render(
Test
{null}
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
});
});
describe('Adding a
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('#table')).to.exist;
});
it('renders the regular data rows', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
it('renders the tfoot', function() {
expect($('#tfoot')).to.exist;
});
it('renders the children of the tfoot', function() {
expect($('#tfoot-stuff')).to.exist;
});
context('when sorting', function() {
before(function() {
ReactTestUtils.Simulate.click($('th')[0]);
});
it('leaves the tfoot alone', function() {
expect($('table :last-child')).to.match('tfoot');
});
});
context('when filtering', function() {
before(function() {
var $filter = $('.reactable-filter-input');
$filter.val('griffin');
ReactTestUtils.Simulate.keyUp($filter[0]);
});
it('adds the filterClassName to the filterer', function() {
expect($('.reactable-filter-input').hasClass('new-class')).to.eq(true)
});
it('leaves the tfoot alone', function() {
expect($('table :last-child')).to.match('tfoot');
});
});
});
describe('passing through HTML props', function() {
describe('adding
s with className to the
', function() {
before(function() {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the column headers in the table', function() {
var headers = [];
$('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql([ 'Name', 'Age', 'Position' ]);
});
it('renders the first row with the correct class name', function() {
ReactableTestUtils.expectRowClass(0, 'rowClass1');
});
it('renders the second row with the correct class name', function() {
ReactableTestUtils.expectRowClass(1, 'rowClass2');
});
it('renders the third row with the correct class name', function() {
ReactableTestUtils.expectRowClass(2, 'rowClass3');
});
});
describe('adding
s with classNames to the
', function() {
before(function () {
ReactDOM.render(
Griffin Smith18Lee Salminen23Developer28,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the first column with the correct class name', function() {
expect($('td.name-1')).to.have.text('Griffin Smith');
});
it('renders the second column with the correct class name', function() {
expect($('td.name-2')).to.have.text('Lee Salminen');
});
it('renders the third column with the correct class name', function() {
expect($('td.position')).to.have.text('Developer');
});
});
});
describe('adding
with style to the
', function() {
before(function () {
var tdStyle = {width:"100px"};
ReactDOM.render(
Griffin Smith18,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the first column with the width', function() {
expect($('td.name-1')).to.have.attr('style').match(/width/);
});
});
describe('specifying an array of columns', function() {
describe('as strings', function() {
before(function() {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('omits columns not in the list', function() {
var columns = $('tr.reactable-column-header th');
expect(columns.length).to.equal(2);
expect($(columns[0])).to.have.text('Name');
expect($(columns[1])).to.have.text('Age');
});
it('adds class name for each column base on its label', function() {
var columns = $('tr.reactable-column-header th');
expect($(columns[0])).to.have.class('reactable-th-name');
expect($(columns[1])).to.have.class('reactable-th-age');
});
});
describe('as objects', function() {
before(function() {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('omits columns not in the list', function() {
var columns = $('tr.reactable-column-header th');
expect(columns.length).to.equal(2);
});
it('allows changing the labels of the columns', function() {
var columns = $('tr.reactable-column-header th');
expect($(columns[0])).to.have.text('Name');
expect($(columns[1])).to.have.text('Age');
});
it('adds class name for each column base on its key', function() {
var columns = $('tr.reactable-column-header th');
expect($(columns[0])).to.have.class('reactable-th-name');
expect($(columns[1])).to.have.class('reactable-th-age');
});
});
});
describe('specifying columns using a ', function() {
describe('and an element for the column title', function() {
before(function() {
ReactDOM.render(
Griffin Smith'), Age: '18'},
{ Age: '28', Position: Reactable.unsafe('Developer')},
{ Age: '23', Name: Reactable.unsafe('Lee Salminen')},
]}>
name,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders only the columns in the Thead', function() {
expect($('#table tbody tr:first td')).to.exist;
expect($('#table thead tr:first th')).to.exist;
});
it('renders the contents of the Th', function() {
expect($('#table>thead>tr>th>strong')).to.exist;
});
it('passes through the properties of the Th', function() {
expect($('#table>thead>tr>th')).to.have.id('my-name')
});
});
describe('and a string for the column title', function() {
before(function() {
ReactDOM.render(
Griffin Smith'), Age: '18'},
{ Age: '28', Position: Reactable.unsafe('Developer')},
{ Age: '23', Name: Reactable.unsafe('Lee Salminen')},
]}>
name
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders only the columns in the Thead', function() {
expect($('#table tbody tr:first td')).to.exist;
expect($('#table thead tr:first th')).to.exist;
});
it('renders the contents of the Th', function() {
expect($('#table>thead>tr>th')).to.exist;
});
it('passes through the properties of the Th', function() {
expect($('#table>thead>tr>th')).to.have.id('my-name')
});
})
});
describe('table headers', function() {
describe("with hideTableHeader prop on
", function() {
before(function () {
ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('renders the table', function() {
expect($('table#table.table')).to.exist;
});
it('renders the first row with the correct data', function() {
ReactableTestUtils.expectRowText(0, ['Griffin Smith', '18', '']);
});
it('renders the second row with the correct data', function() {
ReactableTestUtils.expectRowText(1, ['Lee Salminen', '23', '']);
});
it('renders the third row with the correct data', function() {
ReactableTestUtils.expectRowText(2, ['', '28', 'Developer']);
});
it('does not show a ', function() {
expect($('#table thead')).not.to.exist;
});
});
});
describe('unsafe() strings', function() {
context('in the
').attr('id', 'test-node-2');
ReactDOM.render(
,
this.testNode1[0]
);
ReactDOM.render(
,
this.testNode2[0]
);
});
after(function() {
$(this.parentTestNode).empty().remove();
});
it('renders the column headers in the first table', function() {
var headers = [];
this.testNode1.find('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql(['Name', 'Age', 'Position']);
});
it('renders the column headers in the second table', function() {
var headers = [];
this.testNode2.find('thead th').each(function() {
headers.push($(this).text());
});
expect(headers).to.eql(['Moniker', 'Elderliness', 'Title']);
});
});
describe('handleClick callbacks', function(){
before(function() {
this.clicked = false
ReactDOM.render(
Griffin Smith,
ReactableTestUtils.testNode()
);
ReactTestUtils.Simulate.click($('td')[0])
});
after(ReactableTestUtils.resetTestEnvironment);
it('calls the callbacks on click', function() {
expect(this.clicked).to.eq(true);
});
});
describe('onContextMenu callbacks on
elements', function(){
before(function() {
this.rightClicked = false
ReactDOM.render(
Griffin Smith,
ReactableTestUtils.testNode()
);
ReactTestUtils.Simulate.contextMenu($('td')[0])
});
after(ReactableTestUtils.resetTestEnvironment);
it('calls the callbacks on right click', function() {
expect(this.rightClicked).to.eq(true);
});
});
describe('table with no data', () => {
context('when noDataText prop is null', () => {
before(function() {
this.component = ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('does not render the reactable-no-data element', () => {
expect($('.reactable-no-data').length).to.eq(0);
});
});
context('when initialized without
s', () => {
before(function() {
this.component = ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('shows the "no data" message', () => {
var $text = $('.reactable-no-data').text();
expect($text).to.eq('No matching records found.');
});
});
context('when filtered without any matches', () => {
before(function() {
this.component = ReactDOM.render(
New Yorkthis is some textnewNew Mexicolorem ipsumoldColorado
new description that shouldnt match filter
oldAlaskabaconrenewed,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment)
it('shows the "no data" message', () => {
var text = $('.reactable-no-data').text();
expect(text).to.eq('No matching records found.');
});
});
context('when initialized with an empty array for `data` prop', () => {
before(function() {
this.component = ReactDOM.render(
,
ReactableTestUtils.testNode()
);
});
after(ReactableTestUtils.resetTestEnvironment);
it('shows the "no data" message', () => {
var $text = $('.reactable-no-data').text();
expect($text).to.eq('No matching records found.');
});
});
})
describe('receive props with no currentPage', () => {
let parent;
before(function () {
//create a wrapper component so we can update its state and trigger componentWillReceiveProps in the table
var TestParent = React.createFactory(React.createClass({
render() {
return (Griffin Smith);
}
}));
parent = ReactDOM.render(TestParent(), ReactableTestUtils.testNode());
});
after(ReactableTestUtils.resetTestEnvironment);
it('keeps the same currentPage and does not set it to undefined', function() {
const preUpdateCurrentPage = parent.refs.table.state.currentPage;
parent.setState({testState: "this state update will trigger componentWillReceiveProps in the Reactable.Table"});
const postUpdateCurrentPage = parent.refs.table.state.currentPage;
expect(postUpdateCurrentPage).to.not.eq(undefined);
expect(postUpdateCurrentPage).to.eq(preUpdateCurrentPage);
});
});
});