diff --git a/package.json b/package.json index 7f0e1734..5fc56634 100644 --- a/package.json +++ b/package.json @@ -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 (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", diff --git a/test/DocSearch-test.js b/test/DocSearch-test.js new file mode 100644 index 00000000..fc0c96f2 --- /dev/null +++ b/test/DocSearch-test.js @@ -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 = '
'; + }); + + 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'); + }); + }); +}); diff --git a/test/fixtures/formatHits.json b/test/fixtures/formatHits.json new file mode 100644 index 00000000..d5d8a5f5 --- /dev/null +++ b/test/fixtures/formatHits.json @@ -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": "Search with Ruby API" + } + }, + "_highlightResult": { + "hierarchy": { + "lvl0": { + "value": "Ruby" + }, + "lvl1": { + "value": "API" + }, + "lvl2": { + "value": "Search" + }, + "lvl3": null, + "lvl4": null, + "lvl5": null + }, + "content": { + "value": "Search 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": "Indexing in Wordpress with PHP" + } + }, + "_highlightResult": { + "hierarchy": { + "lvl0": { + "value": "PHP" + }, + "lvl1": { + "value": "Wordpress" + }, + "lvl2": { + "value": "Indexing" + }, + "lvl3": null, + "lvl4": null, + "lvl5": null + }, + "content": { + "value": "Indexing 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 geosearch with the API in Go" + } + }, + "_highlightResult": { + "hierarchy": { + "lvl0": { + "value": "Go" + }, + "lvl1": { + "value": "API" + }, + "lvl2": { + "value": "Geosearch" + }, + "lvl3": null, + "lvl4": null, + "lvl5": null + }, + "content": { + "value": "Doing geosearch 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 geosearch with the API in Ruby" + } + }, + "_highlightResult": { + "hierarchy": { + "lvl0": { + "value": "Ruby" + }, + "lvl1": { + "value": "API" + }, + "lvl2": { + "value": "Geosearch" + }, + "lvl3": null, + "lvl4": null, + "lvl5": null + }, + "content": { + "value": "Doing geosearch 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 Magento with PHP" + } + }, + "_highlightResult": { + "hierarchy": { + "lvl0": { + "value": "PHP" + }, + "lvl1": { + "value": "Magento" + }, + "lvl2": null, + "lvl3": null, + "lvl4": null, + "lvl5": null + }, + "content": { + "value": "Searching in Magento with PHP" + } + } + } +] diff --git a/test/helpers.js b/test/helpers.js new file mode 100644 index 00000000..191d31cd --- /dev/null +++ b/test/helpers.js @@ -0,0 +1,4 @@ +global.ddescribe = describe.only; +global.xdescribe = describe.skip; +global.iit = it.only; +global.xit = it.skip; diff --git a/test/utils-test.js b/test/utils-test.js index e136b775..ddd95b42 100644 --- a/test/utils-test.js +++ b/test/utils-test.js @@ -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();