1
0
Fork 0

Merge pull request #17 from algolia/test/docsearch-test

test(checkArguments): Add tests for checkArguments
This commit is contained in:
Sylvain Utard 2015-12-16 21:25:59 +01:00
commit 35d6382b14
5 changed files with 290 additions and 7 deletions

View file

@ -14,8 +14,9 @@
"release": "./scripts/release",
"release:beta": "./scripts/release-beta",
"lint": "eslint .",
"test": "BABEL_ENV=test mocha --reporter dot",
"test:watch": "BABEL_ENV=test mocha --reporter min --watch"
"test": "BABEL_ENV=test mocha --reporter dot ./test/helpers.js ./test/*-test.js",
"test:watch": "BABEL_ENV=test mocha --reporter min --watch ./test/helpers.js ./test/*-test.js",
"gh-pages": "./scripts/gh-pages"
},
"author": "Algolia <support@algolia.com> (https://github.com/algolia/)",
"license": "MIT",
@ -42,6 +43,7 @@
"mocha-jsdom": "^1.0.0",
"mversion": "^1.10.1",
"nd": "^1.2.0",
"node-fixtures": "0.0.1",
"node-sass": "^3.4.2",
"npm-shrinkwrap": "^200.4.0",
"onchange": "^2.0.0",

107
test/DocSearch-test.js Normal file
View file

@ -0,0 +1,107 @@
/* eslint-env mocha */
import jsdom from 'mocha-jsdom';
import expect from 'expect';
import fixtures from 'node-fixtures';
describe('DocSearch', () => {
let docSearch;
let $;
jsdom({useEach: true});
beforeEach(() => {
// We need a DOM to be ready before importing Zepto
docSearch = require('../index.js');
$ = require('npm-zepto');
// Note: If you edit this HTML while doing TDD with `npm run test:watch`,
// you will have to restart `npm run test:watch` for the new HTML to be
// updated
document.body.innerHTML = '<div><input id="input" name="input-name" /></div>';
});
describe('checkArguments', () => {
it('should throw an error if no apiKey defined', () => {
// Given
let input = {
indexName: 'indexName'
};
// When
expect(() => {
docSearch(input);
}).toThrow(/^Usage:/);
});
it('should throw an error if no indexName defined', () => {
// Given
let input = {
apiKey: 'apiKey'
};
// When
expect(() => {
docSearch(input);
}).toThrow(/^Usage:/);
});
it('should throw an error if no input element matches the selector in the page', () => {
// Given
let input = {
apiKey: 'apiKey',
indexName: 'indexName',
inputSelector: '#unknown-input'
};
// When
expect(() => {
docSearch(input);
}).toThrow(/^Error:/);
});
it('should pass apiKey and indexName as properties of the instance', () => {
// Given
let input = {
apiKey: 'apiKey',
indexName: 'indexName',
inputSelector: '#input'
};
// When
let actual = docSearch(input);
// Then
expect(actual.apiKey).toEqual('apiKey');
expect(actual.indexName).toEqual('indexName');
});
it('should pass the matching input as property of the input', () => {
// Given
let input = {
apiKey: 'apiKey',
indexName: 'indexName',
inputSelector: '#input'
};
// When
let actual = docSearch(input);
// Then
expect($(actual.input).attr('name')).toEqual('input-name');
});
it('should pass options as properties of the input', () => {
// Given
let input = {
apiKey: 'apiKey',
indexName: 'indexName',
inputSelector: '#input',
algoliaOptions: {name: 'foo'},
autocompleteOptions: {name: 'bar'}
};
// When
let actual = docSearch(input);
// Then
expect(actual.algoliaOptions.name).toEqual('foo');
expect(actual.autocompleteOptions.name).toEqual('bar');
});
});
});

175
test/fixtures/formatHits.json vendored Normal file
View file

@ -0,0 +1,175 @@
[
{
"hierarchy": {
"lvl0": "Ruby",
"lvl1": "API",
"lvl2": "Search",
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": "Search with Ruby API",
"_snippetResult": {
"content": {
"value": "<mark>Search</mark> with Ruby API"
}
},
"_highlightResult": {
"hierarchy": {
"lvl0": {
"value": "Ruby"
},
"lvl1": {
"value": "API"
},
"lvl2": {
"value": "<mark>Search</mark>"
},
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": {
"value": "<mark>Search</mark> with Ruby API"
}
}
},
{
"hierarchy": {
"lvl0": "PHP",
"lvl1": "Wordpress",
"lvl2": "Indexing",
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": "Indexing in Wordpress with PHP",
"_snippetResult": {
"content": {
"value": "<mark>Indexing</mark> in Wordpress with PHP"
}
},
"_highlightResult": {
"hierarchy": {
"lvl0": {
"value": "PHP"
},
"lvl1": {
"value": "Wordpress"
},
"lvl2": {
"value": "<mark>Indexing</mark>"
},
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": {
"value": "<mark>Indexing</mark> in Wordpress with PHP"
}
}
},
{
"hierarchy": {
"lvl0": "Go",
"lvl1": "API",
"lvl2": "Geosearch",
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": "Doing geosearch with the API in Go",
"_snippetResult": {
"content": {
"value": "Doing <mark>geosearch</mark> with the API in Go"
}
},
"_highlightResult": {
"hierarchy": {
"lvl0": {
"value": "Go"
},
"lvl1": {
"value": "API"
},
"lvl2": {
"value": "<mark>Geosearch</mark>"
},
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": {
"value": "Doing <mark>geosearch</mark> with the API in Go"
}
}
},
{
"hierarchy": {
"lvl0": "Ruby",
"lvl1": "API",
"lvl2": "Geosearch",
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": "Doing geosearch with the API in Ruby",
"_snippetResult": {
"content": {
"value": "Doing <mark>geosearch</mark> with the API in Ruby"
}
},
"_highlightResult": {
"hierarchy": {
"lvl0": {
"value": "Ruby"
},
"lvl1": {
"value": "API"
},
"lvl2": {
"value": "<mark>Geosearch</mark>"
},
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": {
"value": "Doing <mark>geosearch</mark> with the API in Ruby"
}
}
},
{
"hierarchy": {
"lvl0": "PHP",
"lvl1": "Magento",
"lvl2": null,
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": "Searching in Magento with PHP",
"_snippetResult": {
"content": {
"value": "Searching in <mark>Magento</mark> with PHP"
}
},
"_highlightResult": {
"hierarchy": {
"lvl0": {
"value": "PHP"
},
"lvl1": {
"value": "Magento"
},
"lvl2": null,
"lvl3": null,
"lvl4": null,
"lvl5": null
},
"content": {
"value": "Searching in <mark>Magento</mark> with PHP"
}
}
}
]

4
test/helpers.js Normal file
View file

@ -0,0 +1,4 @@
global.ddescribe = describe.only;
global.xdescribe = describe.skip;
global.iit = it.only;
global.xit = it.skip;

View file

@ -3,11 +3,6 @@
import jsdom from 'mocha-jsdom';
import expect from 'expect';
let ddescribe = describe.only;
let xdescribe = describe.skip;
let iit = it.only;
let xit = it.skip;
describe('utils', () => {
var utils;
jsdom();