Skip to content

Commit

Permalink
ES6-ify tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkp committed Oct 29, 2017
1 parent 6dab321 commit 9a06585
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 50 deletions.
13 changes: 3 additions & 10 deletions test/Calendar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@ import 'core-js/es6/set';

import moment from 'moment';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import Calendar from '../src/Calendar';
import asserter from './assertions/Asserter';
import chai from 'chai';

const expect = chai.expect;

describe('Calendar', () => {
const onSelect = function(date) {
return true;
//console.info('onSelect', date);
};
const onSelect = () => true;

it('displays the correct year', () => {
const calendar = (
Expand Down Expand Up @@ -53,10 +48,8 @@ describe('Calendar', () => {
.assertMonth('May');
});

it('should trigger the callback with selected date when clicking a day', function(
done
) {
const callback = function(selectedDate) {
it('should trigger the callback with selected date when clicking a day', done => {
const callback = selectedDate => {
expect(moment(selectedDate).format('DD/MM/YYYY')).to.equal('08/04/2015');
done();
};
Expand Down
63 changes: 23 additions & 40 deletions test/assertions/Asserter.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,66 @@
import moment from 'moment';
import React from 'react';
import ReactDOM from 'react-dom';
import ReactTestUtils from 'react-dom/test-utils';
import { findDOMNode } from 'react-dom';
import {
Simulate,
scryRenderedDOMComponentsWithClass,
findRenderedDOMComponentWithClass,
renderIntoDocument,
} from 'react-dom/test-utils';

import Calendar from '../../src/Calendar';
import chai from 'chai';

const expect = chai.expect;

export default jsx => {
const calendar = ReactTestUtils.renderIntoDocument(jsx);
const calendar = renderIntoDocument(jsx);

return {
assertYear(expectedYear) {
const year = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'year'
);
expect(ReactDOM.findDOMNode(year).textContent).to.equal(expectedYear);
const year = findRenderedDOMComponentWithClass(calendar, 'year');
expect(findDOMNode(year).textContent).to.equal(expectedYear);
return this;
},

assertMonth(expectedMonth) {
const month = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'month'
);
expect(ReactDOM.findDOMNode(month).textContent).to.equal(expectedMonth);
const month = findRenderedDOMComponentWithClass(calendar, 'month');
expect(findDOMNode(month).textContent).to.equal(expectedMonth);
return this;
},

previousMonth() {
const previous = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'previous'
);
ReactTestUtils.Simulate.click(previous.firstChild);
const previous = findRenderedDOMComponentWithClass(calendar, 'previous');
Simulate.click(previous.firstChild);
return this;
},

nextMonth() {
const next = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'next'
);
ReactTestUtils.Simulate.click(next.firstChild);
const next = findRenderedDOMComponentWithClass(calendar, 'next');
Simulate.click(next.firstChild);
return this;
},

assertSelectedDay(expectedDay) {
const selected = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'selected'
);
const value = ReactDOM.findDOMNode(selected).textContent;
const selected = findRenderedDOMComponentWithClass(calendar, 'selected');
const value = findDOMNode(selected).textContent;
expect(+value).to.equal(expectedDay);
return this;
},

assertToday() {
const today = ReactTestUtils.findRenderedDOMComponentWithClass(
calendar,
'today'
);
const value = ReactDOM.findDOMNode(today).textContent;
const today = findRenderedDOMComponentWithClass(calendar, 'today');
const value = findDOMNode(today).textContent;
expect(value).to.equal(moment().format('D'));
return this;
},

clickDay(date) {
const days = ReactTestUtils.scryRenderedDOMComponentsWithClass(
calendar,
'Day'
);
const days = scryRenderedDOMComponentsWithClass(calendar, 'Day');
const found = days.filter(day => {
var value = ReactDOM.findDOMNode(day).dataset.day;
const value = findDOMNode(day).dataset.day;
return +value === date;
});
ReactTestUtils.Simulate.click(found[0].firstChild);
Simulate.click(found[0].firstChild);
return this;
},
};
Expand Down

0 comments on commit 9a06585

Please sign in to comment.