diff --git a/.babelrc b/.babelrc index cab5d10d..60e33aa0 100644 --- a/.babelrc +++ b/.babelrc @@ -1,3 +1,8 @@ { + "env": { + "test": { + "plugins": ["babel-plugin-rewire"] + } + }, "stage": 2 } diff --git a/package.json b/package.json index 5fc56634..c9eec2ae 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "babel-core": "^5.8.29", "babel-eslint": "^4.1.3", "babel-loader": "^5.3.2", + "babel-plugin-rewire": "^0.1.22", "conventional-changelog": "^0.5.1", "cssnano": "^3.4.0", "doctoc": "^0.15.0", @@ -50,6 +51,7 @@ "postcss-cli": "^2.3.2", "pretty-bytes": "^2.0.1", "semver": "^5.1.0", + "sinon": "^1.17.2", "uglify-js": "^2.6.1", "webpack": "^1.12.2", "webpack-dev-server": "^1.12.1" diff --git a/scripts/bump-package-version.js b/scripts/bump-package-version.js index b257b247..989bf5cd 100644 --- a/scripts/bump-package-version.js +++ b/scripts/bump-package-version.js @@ -1,3 +1,4 @@ +/* eslint no-console:0 max-len:0 */ import fs from 'fs'; import path from 'path'; diff --git a/scripts/docs/gh-pages.js b/scripts/docs/gh-pages.js index 47dbdab3..83ae8fae 100644 --- a/scripts/docs/gh-pages.js +++ b/scripts/docs/gh-pages.js @@ -1,3 +1,4 @@ +/* eslint no-console:0 */ import ghpages from 'gh-pages'; import {join} from 'path'; diff --git a/src/lib/DocSearch.js b/src/lib/DocSearch.js index 3ae47bd2..ab493d07 100644 --- a/src/lib/DocSearch.js +++ b/src/lib/DocSearch.js @@ -36,12 +36,18 @@ class DocSearch { hint: false } }) { - this.checkArguments({apiKey, indexName, inputSelector, algoliaOptions, autocompleteOptions}); + DocSearch.checkArguments({apiKey, indexName, inputSelector, algoliaOptions, autocompleteOptions}); + + this.apiKey = apiKey; + this.indexName = indexName; + this.input = DocSearch.getInputFromSelector(inputSelector); + this.algoliaOptions = algoliaOptions; + this.autocompleteOptions = autocompleteOptions; this.client = algoliasearch('BH4D9OD16A', this.apiKey); this.client.addAlgoliaAgent('docsearch.js ' + version); this.autocomplete = autocomplete(this.input, autocompleteOptions, [{ - source: this.getSource(), + source: this.getAutocompleteSource(), templates: { suggestion: this.getSuggestionTemplate(), footer: '
' @@ -50,26 +56,42 @@ class DocSearch { this.autocomplete.on('autocomplete:selected', this.handleSelected); } - checkArguments(args) { + /** + * Checks that the passed arguments are valid. Will throw errors otherwise + * @function checkArguments + * @param {object} args Arguments as an option object + * @returns {void} + */ + static checkArguments(args) { if (!args.apiKey || !args.indexName) { throw new Error(usage); } - const input = $(args.inputSelector).filter('input'); - if (input.length === 0) { + if (!DocSearch.getInputFromSelector(args.inputSelector)) { throw new Error(`Error: No input element in the page matches ${args.inputSelector}`); } - - this.apiKey = args.apiKey; - this.indexName = args.indexName; - this.input = input; - this.algoliaOptions = args.algoliaOptions; - this.autocompleteOptions = args.autocompleteOptions; } - // Returns a `source` method to be used by `autocomplete`. This will query the - // Algolia index. - getSource() { + /** + * Returns the matching input from a CSS selector, null if none matches + * @function getInputFromSelector + * @param {string} selector CSS selector that matches the search + * input of the page + * @returns {void} + */ + static getInputFromSelector(selector) { + let input = $(selector).filter('input'); + return input.length ? $(input[0]) : null; + } + + /** + * Returns the `source` method to be passed to autocomplete.js. It will query + * the Algolia index and call the callbacks with the formatted hits. + * @function getAutocompleteSource + * @returns {function} Method to be passed as the `source` option of + * autocomplete + */ + getAutocompleteSource() { return (query, callback) => { this.client.search([{ indexName: this.indexName, diff --git a/src/lib/templates.js b/src/lib/templates.js index 138432ca..660fce69 100644 --- a/src/lib/templates.js +++ b/src/lib/templates.js @@ -2,7 +2,10 @@ let prefix = 'ads-suggestion'; let templates = { suggestion: ` -