|
| 1 | +describe "search", -> |
| 2 | + it "should display only matching items when entering a search term", -> |
| 3 | + tmpl = " |
| 4 | + <select data-placeholder='Choose a Country...'> |
| 5 | + <option value=''></option> |
| 6 | + <option value='United States'>United States</option> |
| 7 | + <option value='United Kingdom'>United Kingdom</option> |
| 8 | + <option value='Afghanistan'>Afghanistan</option> |
| 9 | + </select> |
| 10 | + " |
| 11 | + div = $("<div>").html(tmpl) |
| 12 | + select = div.find("select") |
| 13 | + select.chosen() |
| 14 | + |
| 15 | + container = div.find(".chosen-container") |
| 16 | + container.trigger("mousedown") # open the drop |
| 17 | + # Expect all results to be shown |
| 18 | + results = div.find(".active-result") |
| 19 | + expect(results.size()).toBe(3) |
| 20 | + |
| 21 | + # Enter some text in the search field. |
| 22 | + search_field = div.find(".chosen-search input").first() |
| 23 | + search_field.val("Afgh") |
| 24 | + search_field.trigger('keyup') |
| 25 | + |
| 26 | + # Expect to only have one result: 'Afghanistan'. |
| 27 | + results = div.find(".active-result") |
| 28 | + expect(results.size()).toBe(1) |
| 29 | + expect(results.first().text()).toBe "Afghanistan" |
| 30 | + |
| 31 | + it "should only show max_shown_results items in results", -> |
| 32 | + tmpl = " |
| 33 | + <select data-placeholder='Choose a Country...'> |
| 34 | + <option value=''></option> |
| 35 | + <option value='United States'>United States</option> |
| 36 | + <option value='United Kingdom'>United Kingdom</option> |
| 37 | + <option value='Afghanistan'>Afghanistan</option> |
| 38 | + </select> |
| 39 | + " |
| 40 | + div = $("<div>").html(tmpl) |
| 41 | + select = div.find("select") |
| 42 | + select.chosen({max_shown_results: 1 }) |
| 43 | + |
| 44 | + container = div.find(".chosen-container") |
| 45 | + container.trigger("mousedown") # open the drop |
| 46 | + results = div.find(".active-result") |
| 47 | + expect(results.size()).toBe(1) |
| 48 | + |
| 49 | + # Enter some text in the search field. |
| 50 | + search_field = div.find(".chosen-search input").first() |
| 51 | + search_field.val("United") |
| 52 | + search_field.trigger("keyup") |
| 53 | + |
| 54 | + # Showing only one result: the one that occurs first. |
| 55 | + results = div.find(".active-result") |
| 56 | + expect(results.size()).toBe(1) |
| 57 | + expect(results.first().text()).toBe "United States" |
| 58 | + |
| 59 | + # Showing still only one result, but not the first one. |
| 60 | + search_field.val("United Ki") |
| 61 | + search_field.trigger("keyup") |
| 62 | + results = div.find(".active-result") |
| 63 | + expect(results.size()).toBe(1) |
| 64 | + expect(results.first().text()).toBe "United Kingdom" |
0 commit comments