Skip to content

Commit

Permalink
chore(ffe-datepicker-react): Migrate tests to jest
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristofer Selbekk committed Feb 21, 2018
1 parent 258dabf commit 128f9de
Show file tree
Hide file tree
Showing 10 changed files with 608 additions and 652 deletions.
10 changes: 10 additions & 0 deletions packages/ffe-datepicker-react/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"overrides": [
{
"files": [ "src/**/*.spec.js"],
"env": {
"jest": true
}
}
]
}
29 changes: 13 additions & 16 deletions packages/ffe-datepicker-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,22 @@
"main": "lib/ffe-datepicker-react.js",
"license": "UNLICENSED",
"scripts": {
"build": "babel -d lib/. --ignore=*.test.js src/.",
"build": "babel -d lib/. --ignore=*.spec.js src/.",
"lint": "eslint src/.",
"test": "npm run test:spec && npm run test:nsp",
"test:spec": "mocha --require src/util/test-setup.js --compilers js:babel-core/register 'src/**/*.test.js'",
"test:spec": "jest",
"test:nsp": "nsp check",
"test:watch": "npm test -- -w"
"test:watch": "jest --watch"
},
"devDependencies": {
"chai": "^4.1.2",
"chai-enzyme": "^1.0.0-beta.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"eslint": "^4.13.1",
"jsdom": "^11.5.1",
"mocha": "^4.1.0",
"jest": "^22.4.0",
"nsp": "^3.1.0",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-test-renderer": "^16.0.0",
"sinon": "^4.1.3",
"sinon-chai": "^2.14.0"
"react": "^16.2.0",
"react-dom": "^16.2.0"
},
"files": [
"lib",
"*.js"
],
"peerDependencies": {
"ffe-datepicker": "^4.2.5",
"ffe-form": "2.x - 8.x",
Expand All @@ -41,5 +31,12 @@
"ffe-icons-react": "^4.0.5",
"prop-types": "^15.6.0",
"uuid": "^3.1.0"
},
"files": [
"lib",
"*.js"
],
"jest": {
"setupTestFrameworkScriptFile": "./test-setup.js"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
/* eslint-env mocha */

import { shallow } from 'enzyme';
import { assert } from 'chai';
import React from 'react';
import ActiveDate from './ActiveDate';
import simpleDate from '../datelogic/simpledate';
Expand All @@ -19,11 +16,11 @@ describe('<ActiveDate />', () => {

it('should render an active date', () => {
const wrapper = shallow(activeDate);
assert.equal(wrapper.find('td.ffe-calendar__day').length, 1);
expect(wrapper.find('td.ffe-calendar__day').exists()).toBe(true);
});

it('should have role gridcell', () => {
const wrapper = shallow(activeDate);
assert.equal(wrapper.find('[role="gridcell"]').length, 1);
expect(wrapper.find('[role="gridcell"]').exists()).toBe(true);
});
});
154 changes: 154 additions & 0 deletions packages/ffe-datepicker-react/src/datelogic/simpledate.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
import simpleDate from './simpledate';
import ErrorTypes from './error-types';

describe('simpleDate fromString', () => {
describe('triggers', () => {
it('onSuccess callback', () => {
const onSuccess = jest.fn();
const date = simpleDate.fromString('01.01.2016', onSuccess);

expect(onSuccess).toHaveBeenCalledTimes(1);
expect(onSuccess.mock.calls[0][0]).toBe(date);
});

it('onError callback with INVALID_DATE_FORMAT', () => {
const onError = jest.fn();
simpleDate.fromString('---', () => {}, onError);
expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toBe(
ErrorTypes.INVALID_DATE_FORMAT,
);
});

it('onError callback with INVALID_DATE', () => {
const onError = jest.fn();
simpleDate.fromString('29.02.2015', () => {}, onError);

expect(onError).toHaveBeenCalledTimes(1);
expect(onError.mock.calls[0][0]).toBe(ErrorTypes.INVALID_DATE);
});
});

describe('formats', () => {
it('single digit dates as double digit values', () => {
expect(simpleDate.fromString('01.01.2016').format()).toBe(
'01.01.2016',
);
});

it('double digit dates as double digit values', () => {
expect(simpleDate.fromString('10.10.2016').format()).toBe(
'10.10.2016',
);
});
});

describe('does not accept', () => {
it('date format prepended anything other than digits', () =>
expect(simpleDate.fromString('----10102016')).toBeNull());

it('date format appended anything after valid date', () =>
expect(simpleDate.fromString('10102016----')).toBeNull());

it('date without year', () =>
expect(simpleDate.fromString('10.10.')).toBeNull());

it('date without day', () =>
expect(simpleDate.fromString('.10.2016')).toBeNull());

it('date without month', () =>
expect(simpleDate.fromString('10..2016')).toBeNull());

it('date with 1 digit as year', () =>
expect(simpleDate.fromString('10.10.6')).toBeNull());

it('date with 3 digits as year', () =>
expect(simpleDate.fromString('10.10.206')).toBeNull());

it('date with more than 4 digits as year', () =>
expect(simpleDate.fromString('10.10.201673829')).toBeNull());

it('date higher than 31', () =>
expect(simpleDate.fromString('32.03.2015')).toBeNull());

it('february 29 in none-leap years', () =>
expect(simpleDate.fromString('29.02.2015')).toBeNull());

it('month higher than 12', () =>
expect(simpleDate.fromString('25.13.2015')).toBeNull());
});

describe('accepts', () => {
it('single digit day', () => {
expect(simpleDate.fromString('5.04.2015').format()).toBe(
'05.04.2015',
);
});

it('single digit month', () => {
expect(simpleDate.fromString('25.1.2015').format()).toBe(
'25.01.2015',
);
});

it('double digit year', () => {
expect(simpleDate.fromString('10.10.17').format()).toBe(
'10.10.2017',
);
});

it('no separators for 4 digit years', () => {
expect(simpleDate.fromString('10102016').format()).toBe(
'10.10.2016',
);
});

it('no separators for 2 digit years', () => {
expect(simpleDate.fromString('101016').format()).toBe('10.10.2016');
});

it('given separators for 4 digit years', () => {
'. -/'.split('').forEach(separator => {
const date = simpleDate.fromString(
`10${separator}10${separator}2016`,
);
expect(date.format()).toBe(`10.10.2016`);
});
});

it('given separators for 2 digit years', () => {
'. -/'.split('').forEach(separator => {
const date = simpleDate.fromString(
`10${separator}10${separator}16`,
);
expect(date.format()).toBe('10.10.2016');
});
});
});

describe('during last day of month', () => {
describe('given date from month with 30 days', () => {
it('parses', () => {
expect(simpleDate.fromString('01.09.2017')).not.toBeNull();
});

it('formats', () => {
expect(simpleDate.fromString('01.09.2017').format()).toEqual(
'01.09.2017',
);
});
});

describe('given date from month with 28 days', () => {
it('parses', () => {
expect(simpleDate.fromString('05.02.2015')).not.toBeNull();
});

it('formats', () => {
expect(simpleDate.fromString('05.02.2015').format()).toEqual(
'05.02.2015',
);
});
});
});
});

0 comments on commit 128f9de

Please sign in to comment.