Skip to content

Commit

Permalink
Merge pull request #2242 from HSLdevcom/enable-unit-tests
Browse files Browse the repository at this point in the history
Enable unit tests
  • Loading branch information
vesameskanen authored Apr 16, 2018
2 parents d2d03ed + 7f530ec commit 1ee0228
Show file tree
Hide file tree
Showing 7 changed files with 128 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": ["env"]
}
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ jobs:
env: FLOW=1
node_js: "8"
script: test/flow.sh
- stage: test
env: UNIT=1
node_js: "8"
script:
- yarn install
- yarn run test-unit

- stage: visual
env: VISUAL=chrome
Expand Down
14 changes: 14 additions & 0 deletions docs/Tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,17 @@ If things change, you need to update the images

- run: `BS_USERNAME=user BS_ACCESS_KEY=key npm run test-visual-update`
- then verify that changed test images are OK and commit the changes

# Unit tests

Unit tests can be run locally. This currently uses the ```mocha``` test runner. The pattern being watched is ```'test/unit/**/*.test.js'```.

Using yarn
```
yarn run test-unit
```

Using the continuous watch mode
```
yarn run test-unit -- --watch
```
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"test-browserstack": "./test/flow/script/run-ui-tests.sh browserstack",
"test-smoke": "./test/flow/script/run-ui-tests.sh smoke",
"test-saucelabs": "./test/flow/script/run-ui-tests.sh saucelabs",
"test-unit": "mocha --require babel-core/register 'test/unit/**/*.test.js'",
"test-visual": "gemini --reporter flat test test/visual/",
"test-visual-update": "gemini update test/visual/",
"es-lint": "eslint .",
Expand Down Expand Up @@ -130,6 +131,7 @@
"babel-plugin-transform-runtime": "6.23.0",
"babel-runtime": "6.26.0",
"brotli-webpack-plugin": "0.5.0",
"chai": "^4.1.2",
"css-loader": "0.28.9",
"csswring": "5.1.1",
"eslint": "4.13.1",
Expand All @@ -150,6 +152,7 @@
"html-reporter": "1.2.0",
"iltorb": "2.0.3",
"imports-loader": "0.7.1",
"mocha": "^5.0.5",
"nightwatch": "0.9.19",
"node-sass": "4.7.2",
"nodemon": "1.12.7",
Expand Down
1 change: 1 addition & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
const path = require('path');

require('babel-core/register')({
babelrc: false,
presets: [['env', { targets: { node: 'current' } }], 'stage-2', 'react'],
plugins: [
'dynamic-import-node',
Expand Down
98 changes: 98 additions & 0 deletions test/unit/legUtils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';
import * as utils from '../../app/util/legUtils';

describe('legUtils', () => {
describe('getLegMode', () => {
it('should return undefined for a null leg', () => {
const leg = null;
const mode = utils.getLegMode(leg);
expect(mode).to.equal(undefined);
});

it('should return undefined for a null mode', () => {
const leg = {
mode: null,
};
const mode = utils.getLegMode(leg);
expect(mode).to.equal(undefined);
});

it('should return undefined for an unknown mode', () => {
const leg = {
mode: 'invalid',
};
const mode = utils.getLegMode(leg);
expect(mode).to.equal(undefined);
});

it('should be case-insensitive', () => {
const leg = {
mode: 'citybike',
};
const mode = utils.getLegMode(leg);
expect(mode).to.equal(utils.LegMode.CityBike);
});

it('should return the mode for a string literal mode', () => {
const literalMode = 'WALK';
const mode = utils.getLegMode(literalMode);
expect(mode).to.equal(utils.LegMode.Walk);
});

it('should return the mode for a string object mode', () => {
// eslint-disable-next-line no-new-wrappers
const objectMode = new String('WALK');
const mode = utils.getLegMode(objectMode);
expect(mode).to.equal(utils.LegMode.Walk);
});
});

describe('getTotalWalkingDistance', () => {
it('should return 0 if there are no legs available', () => {
const itinerary = {
legs: [],
};
const distance = utils.getTotalWalkingDistance(itinerary);
expect(distance).to.equal(0);
});

it('should include only walking legs', () => {
const itinerary = {
legs: [
{
distance: 2,
mode: utils.LegMode.Walk,
},
{
distance: 3,
mode: utils.LegMode.Bicycle,
},
],
};
const distance = utils.getTotalWalkingDistance(itinerary);
expect(distance).to.equal(2);
});

it('should include all walking legs', () => {
const itinerary = {
legs: [
{
distance: 2,
mode: utils.LegMode.Walk,
},
{
distance: 3,
mode: utils.LegMode.Bicycle,
},
{
distance: 1,
mode: utils.LegMode.Walk,
},
],
};
const distance = utils.getTotalWalkingDistance(itinerary);
expect(distance).to.equal(3);
});
});
});
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const FaviconsWebpackPlugin = require('favicons-webpack-plugin');
const fs = require('fs');

require('babel-core/register')({
babelrc: false,
presets: [['env', { targets: { node: 'current' } }], 'stage-2', 'react'],
plugins: [
'dynamic-import-node',
Expand All @@ -40,6 +41,7 @@ function getRulesConfig(env) {
loader: 'babel',
include: [path.resolve(__dirname, 'app/')],
options: {
babelrc: false,
presets: [['env', { modules: false }], 'react', 'stage-2'],
plugins: [
'transform-import-commonjs',
Expand All @@ -66,6 +68,7 @@ function getRulesConfig(env) {
loader: 'babel',
include: [path.resolve(__dirname, 'app/')],
options: {
babelrc: false,
// loose is needed by older Androids < 4.3 and IE10
presets: [
[
Expand Down

0 comments on commit 1ee0228

Please sign in to comment.