Skip to content

Extend checkArguments to cover all arguments #446

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions src/lib/DocsSearchBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import { MeiliSearch } from 'meilisearch'
/**
* Adds an autocomplete dropdown to an input field
* @function DocsSearchBar
* @param {string} options.hostUrl URL where MeiliSearch instance is hosted
* @param {string} options.apiKey Read-only API key
* @param {string} options.indexUid UID of the index to target
* @param {string} options.inputSelector CSS selector that targets the input
* @param {Object} [options.meilisearchOptions] Options to pass the underlying MeiliSearch client
* @param {Object} [options.autocompleteOptions] Options to pass to the underlying autocomplete instance
* @param {string} options.hostUrl URL where MeiliSearch instance is hosted
* @param {string} options.apiKey Read-only API key
* @param {string} options.indexUid UID of the index to target
* @param {string} options.inputSelector CSS selector that targets the input
* @param {boolean} [options.debug] When set to true, the dropdown will not be closed on blur
* @param {Object} [options.meilisearchOptions] Options to pass the underlying MeiliSearch client
* @param {function} [options.queryDataCallback] This function will be called when querying MeiliSearch
* @param {Object} [options.autocompleteOptions] Options to pass to the underlying autocomplete instance
* @param {function} [options.transformData] An optional function to transform the hits
* @param {function} [options.queryHook] An optional function to transform the query
* @param {function} [options.handleSelected] This function is called when a suggestion is selected
* @param {function} [options.enhancedSearchInput] When set to true, a theme is applied to the search box to improve its appearance
* @param {'column'|'simple'} [options.layout] Layout of the search bar
* @param {boolean} [options.enableDarkMode] Allows you to display the searchbar in dark mode
* @return {Object}
*/
const usage = `Usage:
Expand All @@ -22,8 +30,16 @@ const usage = `Usage:
apiKey,
indexUid,
inputSelector,
[ meilisearchOptions ]
[ autocompleteOptions ]
[ debug ],
[ meilisearchOptions ],
[ queryDataCallback ],
[ autocompleteOptions ],
[ transformData ],
[ queryHook ],
[ handleSelected ],
[ enhancedSearchInput ],
[ layout ],
[ enableDarkMode ]
})`
class DocsSearchBar {
constructor({
Expand Down Expand Up @@ -196,13 +212,56 @@ class DocsSearchBar {
)
}

if (typeof args.enableDarkMode !== 'boolean') {
DocsSearchBar.typeCheck(
args,
['meilisearchOptions', 'autocompleteOptions'],
'object',
true,
)

DocsSearchBar.typeCheck(
args,
['debug', 'enableDarkMode', 'enhancedSearchInput'],
'boolean',
false,
)

DocsSearchBar.typeCheck(
args,
['queryDataCallback', 'transformData', 'queryHook', 'handleSelected'],
'function',
true,
)

if (args.layout && !['simple', 'columns'].includes(args.layout)) {
throw new Error(
`Error: "enableDarkMode" must be of type: boolean. Found type: ${typeof args.inputSelector}`,
`Error: "layout" must be either 'columns' or 'simple'. Supplied value: ${args.layout}`,
)
}
}

/**
* Checks if the arguments defined in the check variable are of the supplied
* type
* @param {any[]} args all arguments
* @param {string[]} checkArguments array with the argument names to check
* @param {string} type required type for the arguments
* @param {boolean} optional don't check argument if it's falsy
* @returns {void}
*/
static typeCheck(args, checkArguments, type, optional) {
checkArguments
.filter((argument) => !optional || args[argument])
.forEach((argument) => {
const value = args[argument]
if (typeof args[argument] !== type) {
throw new Error(
`Error: "${argument}" must be of type: ${type}. Found type: ${typeof value}`,
)
}
})
}

static injectSearchBox(input) {
input.before(templates.searchBox)
const newInput = input.prev().prev().find('input')
Expand Down
126 changes: 108 additions & 18 deletions src/lib/__tests__/DocsSearchBar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,26 @@ describe('DocsSearchBar', () => {

describe('checkArguments', () => {
let checkArguments
let defaultOptions

beforeEach(() => {
checkArguments = DocsSearchBar.checkArguments
defaultOptions = {
hostUrl: 'https://test.getmeili.com',
apiKey: 'apiKey',
indexUid: 'indexUID',
inputSelector: '#input',
debug: false,
meilisearchOptions: {},
queryDataCallback: null,
autocompleteOptions: {},
transformData: false,
queryHook: false,
handleSelected: false,
enhancedSearchInput: false,
layout: 'columns',
enableDarkMode: false,
}
})

afterEach(() => {
Expand All @@ -242,11 +260,8 @@ describe('DocsSearchBar', () => {

it('should throw an error if no hostUrl defined', () => {
// Given
const options = {
apiKey: 'apiKey',
indexUid: 'indexUID',
inputSelector: '#',
}
const options = defaultOptions
delete options.hostUrl

// When
expect(() => {
Expand All @@ -255,10 +270,8 @@ describe('DocsSearchBar', () => {
})
it('should throw an error if no inputSelector defined', () => {
// Given
const options = {
hostUrl: 'test.com',
indexUid: 'indexUID',
}
const options = defaultOptions
delete options.inputSelector

// When
expect(() => {
Expand All @@ -267,25 +280,102 @@ describe('DocsSearchBar', () => {
})
it('should throw an error if no indexUid defined', () => {
// Given
const options = {
hostUrl: 'test.com',
inputSelector: '#',
}
const options = defaultOptions
delete options.indexUid

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Usage:/)
})
it('should throw an error if no selector matches', () => {
// Given
const options = { ...defaultOptions, inputSelector: '#' }
sinon.stub(DocsSearchBar, 'getInputFromSelector').returns(false)

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if enableDarkMode is not a boolean', () => {
// Given
const options = { ...defaultOptions, enableDarkMode: 'yes' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if debug is not a boolean', () => {
// Given
const options = { ...defaultOptions, debug: null }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if enhancedSearchInput is not a boolean', () => {
// Given
const options = { ...defaultOptions, enhancedSearchInput: 'yes' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if meilisearchOptions is not an object', () => {
// Given
const options = { ...defaultOptions, meilisearchOptions: 'not-an-object' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if autocompleteOptions is not a object', () => {
// Given
const options = {
hostUrl: 'test.com',
apiKey: 'apiKey',
indexUid: 'indexUID',
inputSelector: '#',
...defaultOptions,
autocompleteOptions: 'not-an-object',
}
sinon.stub(DocsSearchBar, 'getInputFromSelector').returns(false)

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if queryDataCallback is not a function', () => {
// Given
const options = { ...defaultOptions, queryDataCallback: 'not-a-function' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if transformData is not a function', () => {
// Given
const options = { ...defaultOptions, transformData: 'not-a-function' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if queryHook is not a function', () => {
// Given
const options = { ...defaultOptions, queryHook: 'not-a-function' }

// When
expect(() => {
checkArguments(options)
}).toThrow(/^Error:/)
})
it('should throw an error if handleSelected is not a function', () => {
// Given
const options = { ...defaultOptions, handleSelected: 'not-a-function' }

// When
expect(() => {
Expand Down