Skip to content

Commit

Permalink
Merge pull request #204 from algolia/test/port-old-tests
Browse files Browse the repository at this point in the history
test(lib/InstantSearch): test using mocha, expect
  • Loading branch information
Vincent Voyer committed Oct 12, 2015
2 parents 5eb3200 + 695c2ce commit 027662c
Show file tree
Hide file tree
Showing 11 changed files with 195 additions and 286 deletions.
9 changes: 5 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
language: node_js
node_js:
- stable
before_install: npm prune
node_js: 4
install: npm install
script: npm test
script:
- npm test
- ./scripts/validate-commit-msgs.sh
before_cache: npm prune
branches:
only:
- master
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ npm run dev
## Test

```sh
npm test # test and lint
npm run test:watch # developer mode, test only
npm test # test and lint single run using phantomjs
npm run test:watch # test with auto reload using real browser
```

## Available widgets
Expand Down
6 changes: 3 additions & 3 deletions karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,18 @@ module.exports = function(config) {
basePath: '',

preprocessors: {
'components/**/*-test.js': ['webpack', 'sourcemap']
'@(components|lib)/**/*-test.js': ['webpack', 'sourcemap']
},

files: [
'components/**/*-test.js'
'@(components|lib)/**/*-test.js'
],

webpack: {
devtool: 'inline-source-map',
module: {
loaders: [{
test: /\.js$/, exclude: /node_modules/, loader: 'babel'
test: /\.js$/, exclude: /node_modules/, loader: 'babel?plugins=rewire'
}]
}
},
Expand Down
179 changes: 179 additions & 0 deletions lib/__tests__/InstantSearch-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
/* eslint-env mocha */
import EventEmitter from 'events';

import expect from 'expect';
import range from 'lodash/utility/range';
import sinon from 'sinon';

import InstantSearch from '../InstantSearch';

describe('InstantSearch lifecycle', () => {
var algoliasearch;
var algoliasearchHelper;
var client;
var helper;
var appId;
var apiKey;
var indexName;
var searchParameters;
var search;

beforeEach(() => {
client = {algolia: 'client'};
helper = new EventEmitter();

helper.search = sinon.spy();
helper.state = 'state';

algoliasearch = sinon.stub().returns(client);
algoliasearchHelper = sinon.stub().returns(helper);

appId = 'appId';
apiKey = 'apiKey';
indexName = 'lifeycle';

searchParameters = {some: 'configuration', values: [-2, -1], another: {config: 'parameter'}};

InstantSearch.__Rewire__('algoliasearch', algoliasearch);
InstantSearch.__Rewire__('algoliasearchHelper', algoliasearchHelper);

search = new InstantSearch({
appId: appId,
apiKey: apiKey,
indexName: indexName,
searchParameters: searchParameters
});
});

afterEach(() => {
InstantSearch.__ResetDependency__('algoliasearch');
InstantSearch.__ResetDependency__('algoliasearchHelper');
});

it('calls algoliasearch(appId, apiKey)', () => {
expect(algoliasearch.calledOnce).toBe(true, 'algoliasearch called once');
expect(algoliasearch.args[0])
.toEqual([appId, apiKey]);
});

it('does not call algoliasearchHelper', () => {
expect(algoliasearchHelper.notCalled).toBe(true, 'algoliasearchHelper not yet called');
});

context('when adding a widget', () => {
var widget;

beforeEach(() => {
widget = {
getConfiguration: sinon.stub().returns({some: 'modified', another: {different: 'parameter'}}),
init: sinon.spy(),
render: sinon.spy()
};
search.addWidget(widget);
});

it('does not call widget.getConfiguration', () => {
expect(widget.getConfiguration.notCalled).toBe(true);
});

context('when we call search.start', () => {
beforeEach(() => {
search.start();
});

it('calls widget.getConfiguration(searchParameters)', () => {
expect(widget.getConfiguration.args[0]).toEqual([searchParameters]);
});

it('calls algoliasearchHelper(client, indexName, searchParameters)', () => {
expect(algoliasearchHelper.calledOnce).toBe(true, 'algoliasearchHelper called once');
expect(algoliasearchHelper.args[0])
.toEqual([
client,
indexName,
{some: 'modified', values: [-2, -1], another: {different: 'parameter', config: 'parameter'}}
]);
});

it('calls helper.search()', () => {
expect(helper.search.calledOnce).toBe(true);
});

it('calls widget.init(helper.state, helper, templatesConfig)', () => {
expect(widget.init.calledOnce).toBe(true, 'widget.init called once');
expect(widget.init.calledAfter(widget.getConfiguration))
.toBe(true, 'widget.init() was called after widget.getConfiguration()');
expect(widget.init.args[0]).toEqual([helper.state, helper, search.templatesConfig]);
});

it('does not call widget.render', () => {
expect(widget.render.notCalled).toBe(true);
});

context('when we have results', () => {
var results;

beforeEach(() => {
results = {some: 'data'};
helper.emit('result', results, helper.state);
});

it('calls widget.render({results, state, helper, templatesConfig})', () => {
expect(widget.render.calledOnce).toBe(true, 'widget.render called once');
expect(widget.render.args[0])
.toEqual([{
results,
state: helper.state,
helper,
templatesConfig: search.templatesConfig
}]);
});
});
});
});

context('when we have 5 widgets, the third one having __initLast = true', () => {
var widgets;

beforeEach(() => {
widgets = range(5);
widgets = widgets.map((widget, widgetIndex) => {
widget = {
getConfiguration: sinon.stub().returns({values: [widgetIndex]})
};

if (widgetIndex === 2) widget.__initLast = true;

return widget;
});
widgets.forEach(search.addWidget, search);
search.start();
});

it('calls widget[x].getConfiguration in the orders the widgets were added', () => {
let order = widgets
.filter((widget, widgetIndex) => widgetIndex !== 2)
.every((widget, widgetIndex, filteredWidgets) => {
if (widgetIndex === 0) {
return widget.getConfiguration.calledOnce &&
widget.getConfiguration.calledBefore(filteredWidgets[1].getConfiguration);
}
let previousWidget = filteredWidgets[widgetIndex - 1];
return widget.getConfiguration.calledOnce &&
widget.getConfiguration.calledAfter(previousWidget.getConfiguration);
});

expect(order).toBe(true);
});

it('calls widget[2].getConfiguration before any other widget, because of __initLast', () => {
let initLastWidget = widgets[2];
let lastWidget = widgets[widgets.length - 1];
expect(initLastWidget.getConfiguration.calledAfter(lastWidget.getConfiguration)).toBe(true);
});

it('recursevly merges searchParameters.values array', () => {
expect(algoliasearchHelper.args[0][2].values).toEqual([-2, -1, 0, 1, 3, 4, 2]);
});
});
});
12 changes: 5 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"author": "Algolia <support@algolia.com>",
"scripts": {
"build": "./scripts/build.sh",
"dev": "./scripts/dev.sh",
"dev": "webpack-dev-server --config webpack.example.config.js --hot --inline --no-info",
"doctoc": "doctoc --maxlevel 3 README.md",
"gh-pages": "./scripts/gh-pages.sh",
"lint": "./scripts/lint.sh",
"lint": "eslint . --quiet --no-color",
"release": "./scripts/release.sh",
"test": "karma start --single-run --browsers PhantomJS && npm run lint",
"test": "karma start --single-run --browsers PhantomJS --reporters progress && npm run lint",
"test:watch": "karma start",
"watch": "webpack --watch"
},
Expand All @@ -21,7 +21,7 @@
"babel-core": "^5.8.24",
"babel-eslint": "^4.1.3",
"babel-loader": "^5.3.2",
"babel-tape-runner": "^1.2.0",
"babel-plugin-rewire": "^0.1.22",
"conventional-changelog": "^0.5.0",
"doctoc": "^0.15.0",
"eslint": "^1.6.0",
Expand All @@ -46,14 +46,12 @@
"nodemon": "^1.7.1",
"phantomjs": "^1.9.18",
"pretty-bytes": "^2.0.1",
"proxyquire": "^1.7.3",
"raw-loader": "^0.5.1",
"react-addons-test-utils": "^0.14.0",
"semver": "^5.0.3",
"sinon": "^1.17.1",
"sinon": "sinonjs/sinon#a97374641599ed2f451e9a22f784060cf7a38b3e",
"style-loader": "^0.12.4",
"tap-spec": "^4.1.0",
"tape": "^4.2.1",
"uglifyjs": "^2.4.10",
"url-loader": "^0.5.6",
"webpack": "^1.12.2",
Expand Down
8 changes: 0 additions & 8 deletions scripts/dev.sh

This file was deleted.

7 changes: 0 additions & 7 deletions scripts/lint.sh

This file was deleted.

8 changes: 0 additions & 8 deletions scripts/test.sh

This file was deleted.

2 changes: 1 addition & 1 deletion scripts/validate-commit-msgs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/env bash
#!/usr/bin/env bash

# Checks the commits msgs in the range of commits travis is testing.
# Based heavily on
Expand Down
Loading

0 comments on commit 027662c

Please sign in to comment.