diff --git a/typeahead/typeahead-tests.ts b/typeahead/typeahead-tests.ts index ec2b2e2f3..be2f3b34b 100644 --- a/typeahead/typeahead-tests.ts +++ b/typeahead/typeahead-tests.ts @@ -1,302 +1,469 @@ /// /// -// -// Examples from http://twitter.github.com/typeahead.js/examples -// - -var substringMatcher = function (strs: any) { - return function findMatches(q: string, syncResults: (x: Array) => void) { - var matches: Array<{ value: string }> = []; - // regex used to determine if a string contains the substring `q` - var substrRegex = new RegExp(q, 'i'); - - // iterate through the pool of strings and for any string that - // contains the substring `q`, add it to the `matches` array - $.each(strs, function (i, str) { - if (substrRegex.test(str)) { - // the typeahead jQuery plugin expects suggestions to a - // JavaScript object, refer to typeahead docs for more info - matches.push({ value: str }); - } - }); - - syncResults(matches); - } -} - -var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', - 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', - 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', - 'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota', - 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', - 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', - 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island', - 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', - 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming' -]; - - -function test_method_names() { - $('#the-basics .typeahead').typeahead('destroy'); - $('#the-basics .typeahead').typeahead('open'); - $('#the-basics .typeahead').typeahead('close'); - $('#the-basics .typeahead').typeahead('val'); - $('#the-basics .typeahead').typeahead('val', 'test value'); -} - - -function test_options() { - - var dataSets: Twitter.Typeahead.Dataset[] = []; - - function with_empty_options() { - $('#the-basics .typeahead').typeahead({}, dataSets); - } - - function with_hint_option() { - $('#the-basics .typeahead').typeahead({ hint: true }, dataSets); - } - - function with_highlight_option() { - $('#the-basics .typeahead').typeahead({ highlight: true }, dataSets); - } - - function with_minLength_option() { - $('#the-basics .typeahead').typeahead({ minLength: 1 }, dataSets); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead({ - hint: true, - highlight: true, - minLength: 1 - }, - dataSets - ); - } -} - -function test_datasets_array() { - +function test_typeahead() { var options: Twitter.Typeahead.Options = {}; + var dataset: Twitter.Typeahead.Dataset = { source: null }; - function with_only_source() { - $('#the-basics .typeahead').typeahead(options, [{ - source: substringMatcher(states) - }]); + function test_typeahead_methods() { + $('.typeahead').typeahead(options, dataset); + $('.typeahead').typeahead(options, new Array(dataset)); + $('.typeahead').typeahead('val'); + $('.typeahead').typeahead('val', 'test value'); + $('.typeahead').typeahead('open'); + $('.typeahead').typeahead('close'); + $('.typeahead').typeahead('destroy'); } - function with_name_option() { + function test_typeahead_options() { + function options_empty() { + $('.typeahead').typeahead({}, dataset); + } - $('#the-basics .typeahead').typeahead(options, [{ - name: 'states', - source: substringMatcher(states), - }]); + function test_typeahead_option_hint() { + $('.typeahead').typeahead({ hint: true }, dataset); + } + + function test_typeahead_option_minLength() { + $('.typeahead').typeahead({ minLength: 1 }, dataset); + } + + function test_typeahead_option_highlight() { + $('.typeahead').typeahead({ highlight: true }, dataset); + } + + function test_typeahead_option_classNames() { + $('.typeahead').typeahead({ classNames: { input: 'tt-input' } }, dataset); + } + + function test_typeahead_options_all() { + $('.typeahead').typeahead({ + hint: true, + minLength: 1, + highlight: true, + classNames: { input: 'tt-input' } + }, dataset); + } } - function with_displayKey_option() { - $('#the-basics .typeahead').typeahead(options, [{ - display: 'value', - source: substringMatcher(states) - }] - ); + function test_typeahead_classNames() { + function test_typeahead_classNames_empty() { + var className: Twitter.Typeahead.ClassNames = {}; + } + + function test_typeahead_className_input() { + var className: Twitter.Typeahead.ClassNames = { input: 'tt-input' }; + } + + function test_typeahead_className_hint() { + var className: Twitter.Typeahead.ClassNames = { hint: 'tt-hint' }; + } + + function test_typeahead_className_menu() { + var className: Twitter.Typeahead.ClassNames = { menu: 'tt-menu' }; + } + + function test_typeahead_className_dataset() { + var className: Twitter.Typeahead.ClassNames = { dataset: 'tt-dataset' }; + } + + function test_typeahead_className_suggestion() { + var className: Twitter.Typeahead.ClassNames = { suggestion: 'tt-suggestion' }; + } + + function test_typeahead_className_empty() { + var className: Twitter.Typeahead.ClassNames = { empty: 'tt-empty' }; + } + + function test_typeahead_className_open() { + var className: Twitter.Typeahead.ClassNames = { open: 'tt-open' }; + } + + function test_typeahead_className_cursor() { + var className: Twitter.Typeahead.ClassNames = { cursor: 'tt-cursor' }; + } + + function test_typeahead_className_highlight() { + var className: Twitter.Typeahead.ClassNames = { highlight: 'tt-highlight' }; + } + + function test_typeahead_classNames_all() { + var className: Twitter.Typeahead.ClassNames = { + input: 'tt-input', + hint: 'tt-hint', + menu: 'tt-menu', + dataset: 'tt-dataset', + suggestion: 'tt-suggestion', + empty: 'tt-empty', + open: 'tt-open', + cursor: 'tt-cursor', + highlight: 'tt-highlight' + }; + } } - function with_templates_option() { - $('#the-basics .typeahead').typeahead(options, [{ - templates: {}, - source: substringMatcher(states) - }] - ); + function test_typeahead_datasets() { + function test_typeahead_dataset_source_bloodhout() { + var bo: Bloodhound.BloodhoundOptions = { datumTokenizer: null, queryTokenizer: null }; + var engine: Bloodhound = new Bloodhound(bo); + var dataset: Twitter.Typeahead.Dataset = { source: engine }; + } + + function test_typeahead_dataset_source_function() { + var dataset: Twitter.Typeahead.Dataset = { source: (query: string, syncResults: (result: string[]) => void, asyncResults?: (result: string[]) => void) => { } }; + } + + function test_typeahead_dataset_async() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + async: true + }; + } + + function test_typeahead_dataset_name() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + name: 'name' + }; + } + + function test_typeahead_dataset_limit() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + limit: 5 + }; + } + + function test_typeahead_dataset_display_string() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + display: "key" + }; + } + + function test_typeahead_dataset_display_function() { + var dataset: Twitter.Typeahead.Dataset = { + source: null, + display: (obj: string) => { return 'key'; } + }; + } } - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, [{ - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }] - ); - } + function test_typeahead_templates() { + function test_typeahead_templates_empty() { + var templates: Twitter.Typeahead.Templates = {}; + } - function with_multiple_datasets() { - $('#the-basics .typeahead').typeahead(options, [ - { - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }, - { - name: 'states alternative', - display: 'value', - templates: {}, - source: substringMatcher(states) - } - ]); + function dataset_template_notfound_string() { + var templates: Twitter.Typeahead.Templates = { notFound: 'not found' }; + } + + function dataset_template_notfound_function() { + var templates: Twitter.Typeahead.Templates = { notFound: (query: string) => { return 'not found'; } }; + } + + function dataset_template_pending_string() { + var templates: Twitter.Typeahead.Templates = { pending: 'pending' }; + } + + function dataset_template_pending_function() { + var templates: Twitter.Typeahead.Templates = { pending: (query: string) => { return 'pending'; } }; + } + + function dataset_template_header_string() { + var templates: Twitter.Typeahead.Templates = { header: 'header' }; + } + + function dataset_template_header_function() { + var templates: Twitter.Typeahead.Templates = { header: (query: string) => { return 'header'; } }; + } + + function dataset_template_footer_string() { + var templates: Twitter.Typeahead.Templates = { footer: 'footer' }; + } + + function dataset_template_footer_function() { + var templates: Twitter.Typeahead.Templates = { footer: (query: string) => { return 'footer'; } }; + } + + function dataset_template_suggestion() { + var templates: Twitter.Typeahead.Templates = { suggestion: (suggestion: string) => { return 'suggestion'; } }; + } } } +function test_bloodhout() { + var options: Bloodhound.BloodhoundOptions = { datumTokenizer: null, queryTokenizer: null }; + var engine: Bloodhound = new Bloodhound(options); -function test_datasets_objects() { - - var options: Twitter.Typeahead.Options = {}; - - function with_only_source() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states) - }); + function test_bloodhout_static() { + var old: Bloodhound = Bloodhound.noConflict(); + var tokenizers: Bloodhound.Tokenizers = Bloodhound.tokenizers; } - function with_name_option() { + function test_bloodhout_methods() { + // initialize + var promise1: JQueryPromise = engine.initialize(); + var promise2: JQueryPromise = engine.initialize(); + var promise3: JQueryPromise = engine.initialize(true); - $('#the-basics .typeahead').typeahead(options, { - name: 'states', - source: substringMatcher(states), - }); + // add + engine.add(new Array()); + + // get + var data1: string[] = engine.get(new Array()); + + // search + var sync: (datums: string[]) => {}; + var async: (datums: string[]) => {}; + var data2: string[] = engine.search("query", sync, async); + + // all + var data3: string[] = engine.all(); + + // clear + var engine1: Bloodhound = engine.clear(); + + // clearPrefetchCache + var engine2: Bloodhound = engine.clearPrefetchCache(); + + // clearRemoteCache + var engine3: Bloodhound = engine.clearRemoteCache(); } - function with_displayKey_option() { - $('#the-basics .typeahead').typeahead(options, - { - display: 'value', - source: substringMatcher(states) + function test_bloodhout_options() { + function test_bloodhout_options_datumTokenizer() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: (datum: string) => { return new Array(); }, + queryTokenizer: null + }; + } + + function test_bloodhout_options_queryTokenizer() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: (query: string) => { return new Array(); } + }; + } + + function test_bloodhout_options_initialize() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + initialize: true + }; + } + + function test_bloodhout_options_sufficient() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + sufficient: 5 + }; + } + + function test_bloodhout_options_sorter() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + sorter: (a: string, b: string) => { return 0 } + }; + } + + function test_bloodhout_options_local_array() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + local: new Array() + }; + } + + function test_bloodhout_options_local_function() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + local: () => { return new Array() } + }; + } + + function test_bloodhout_options_prefetch_string() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + prefetch: 'url' + }; + } + + function test_bloodhout_options_prefetch_object() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + prefetch: { url: 'url' } + }; + } + + function test_bloodhout_options_remote_string() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + remote: 'url' + }; + } + + function test_bloodhout_options_remote_object() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: null, + queryTokenizer: null, + remote: { url: 'url' } + }; + } + + function test_bloodhout_options_all() { + var options: Bloodhound.BloodhoundOptions = { + datumTokenizer: (datum: string) => { return new Array(); }, + queryTokenizer: (query: string) => { return new Array(); }, + initialize: true, + sufficient: 5, + sorter: (a: string, b: string) => { return 0 }, + local: () => { return new Array() }, + prefetch: { url: 'url' }, + remote: { url: 'url' } + }; + } + } + + function test_bloodhout_prefetch_options() { + function test_bloodhout_prefetch_options_url() { + var options: Bloodhound.PrefetchOptions = { + url: 'url' + }; + } + + function test_bloodhout_prefetch_options_cache() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cache: true + }; + } + + function test_bloodhout_prefetch_options_ttl() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + ttl: 86400000 // 1 day + }; + } + + function test_bloodhout_prefetch_options_cacheKey() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cacheKey: 'url' + }; + } + + function test_bloodhout_prefetch_options_thumbprint() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + thumbprint: 'thumbprint' + }; + } + + function test_bloodhout_prefetch_options_prepare() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.PrefetchOptions = { + url: 'url', + prepare: (settings: JQueryAjaxSettings) => { return ajaxSettings; } + }; + } + + function test_bloodhout_prefetch_options_transform() { + var options: Bloodhound.PrefetchOptions = { + url: 'url', + transform: (response: string[]) => { return new Array(); } + }; + } + + function test_bloodhout_prefetch_options_all() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.PrefetchOptions = { + url: 'url', + cache: true, + ttl: 86400000, + cacheKey: 'url', + thumbprint: 'thumbprint', + prepare: (settings: JQueryAjaxSettings) => { return ajaxSettings; }, + transform: (response: string[]) => { return new Array(); } + }; + } + } + + function test_bloodhout_remote_options() { + function test_bloodhout_remote_options_url() { + var options: Bloodhound.RemoteOptions = { + url: 'url' + }; + } + + function test_bloodhout_remote_options_prepare() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.RemoteOptions = { + url: 'url', + prepare: (query: string, settings: JQueryAjaxSettings) => { return ajaxSettings; } + }; + } + + function test_bloodhout_remote_options_wildcard() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + wildcard: '%QUERY' + }; + } + + function test_bloodhout_remote_options_rateLimitby() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + rateLimitby: 'debounce' + }; + } + + function test_bloodhout_remote_options_rateLimitWait() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + rateLimitWait: 300 + }; + } + + function test_bloodhout_remote_options_transform() { + var options: Bloodhound.RemoteOptions = { + url: 'url', + transform: (response: string[]) => { return new Array(); } + }; + } + + function test_bloodhout_remote_options_all() { + var ajaxSettings: JQueryAjaxSettings = { url: 'url' }; + + var options: Bloodhound.RemoteOptions = { + url: 'url', + prepare: (query: string, settings: JQueryAjaxSettings) => { return ajaxSettings; }, + wildcard: '%QUERY', + rateLimitby: 'debounce', + rateLimitWait: 300, + transform: (response: string[]) => { return new Array(); } + }; + } + } + + function test_bloodhout_tokenizers() { + var tokenizers: Bloodhound.Tokenizers = { + whitespace: (str: string) => { return new Array(); }, + nonword: (str: string) => { return new Array(); }, + obj: { + whitespace: (str: string) => { return new Array(); }, + nonword: (str: string) => { return new Array(); } } - ); + }; } - - function with_templates_option() { - $('#the-basics .typeahead').typeahead(options, - { - templates: {}, - source: substringMatcher(states) - } - ); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, - { - name: 'states', - display: x => x.value, - templates: {}, - source: substringMatcher(states) - } - ); - } - - function with_multiple_objects() { - $('#the-basics .typeahead').typeahead(options, - { - name: 'states', - display: 'value', - templates: {}, - source: substringMatcher(states) - }, - { - name: 'states alternative', - display: 'value', - templates: {}, - source: substringMatcher(states) - } - ); - } -} - -function test_dataset_templates() { - - var options: Twitter.Typeahead.Options = {}; - - function with_no_options() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: {} - }); - } - - function with_empty_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { empty: 'no results' } - }); - } - - function with_empty_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - empty: function (context: any) { - return context.name; - } - } - }); - } - - function with_footer_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { footer: 'custom footer' } - }); - } - - function with_footer_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - footer: function (context: any) { - return context.name; - } - } - }); - } - - function with_header_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { header: 'custom header' } - }); - } - - function with_header_option_as_a_function() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - header: function (context: any) { - return context.name; - } - } - }); - } - - function with_suggestion_option() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - suggestion: function (context) { - return context.name; - } - } - }); - } - - function with_all_options() { - $('#the-basics .typeahead').typeahead(options, { - source: substringMatcher(states), - templates: { - empty: 'no results', - footer: 'custom footer', - header: 'custom header', - suggestion: function (context) { - return context.name; - } - }, - }); - } -} - -function test_value() { - var value: string = $('foo').typeahead('val'); - $('foo').typeahead('val', value); } \ No newline at end of file diff --git a/typeahead/typeahead.d.ts b/typeahead/typeahead.d.ts index b9228b430..b842786cc 100644 --- a/typeahead/typeahead.d.ts +++ b/typeahead/typeahead.d.ts @@ -20,9 +20,10 @@ interface JQuery { * * @constructor * @param options Options hash that's used for configuration - * @param datasets One or more datasets passed as rest parameters. + * @param dataset At least one dataset is required + * @param datasets Rest of the datasets. */ - typeahead(options: Twitter.Typeahead.Options, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; + typeahead(options: Twitter.Typeahead.Options, dataset: Twitter.Typeahead.Dataset, ...datasets: Twitter.Typeahead.Dataset[]): JQuery; /** * Returns the current value of the typeahead. @@ -1122,7 +1123,7 @@ declare class Bloodhound { * Returns a reference to Bloodhound and reverts window.Bloodhound to its * previous value. Can be used to avoid naming collisions. */ - public static noConflict(): any; + public static noConflict(): Bloodhound; /** * The Bloodhound suggestion engine is token-based, so how datums and queries are tokenized plays a vital role in the quality of search results.