describe "Searching", ->
it "should not match the actual text of HTML entities", ->
tmpl = "
"
div = $("
").html(tmpl)
select = div.find("select")
select.chosen({search_contains: true})
container = div.find(".chosen-container")
container.trigger("mousedown") # open the drop
# Both options should be active
results = div.find(".active-result")
expect(results.length).toBe(2)
# Search for the html entity by name
search_field = div.find(".chosen-search input").first()
search_field.val("mp")
search_field.trigger("keyup")
results = div.find(".active-result")
expect(results.length).toBe(0)
it "renders options correctly when they contain characters that require HTML encoding", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result").first().html()).toBe("A & B")
search_field = div.find(".chosen-search-input").first()
search_field.val("A")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result").first().html()).toBe("A & B")
it "renders optgroups correctly when they contain html encoded tags", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".group-result").length).toBe(1)
expect(div.find(".group-result").first().html()).toBe("A <b>hi</b> B")
it "renders optgroups correctly when they contain characters that require HTML encoding when searching", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".group-result").length).toBe(1)
expect(div.find(".group-result").first().html()).toBe("A & B")
search_field = div.find(".chosen-search-input").first()
search_field.val("A")
search_field.trigger("keyup")
expect(div.find(".group-result").length).toBe(1)
expect(div.find(".group-result").first().html()).toBe("A & B")
it "renders no results message correctly when it contains characters that require HTML encoding", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
search_field = div.find(".chosen-search-input").first()
search_field.val("&")
search_field.trigger("keyup")
expect(div.find(".no-results").length).toBe(1)
expect(div.find(".no-results").first().html().trim()).toBe("No results match &")
search_field.val("&")
search_field.trigger("keyup")
expect(div.find(".no-results").length).toBe(1)
expect(div.find(".no-results").first().html().trim()).toBe("No results match &")
it "matches in non-ascii languages like Chinese when selecting a single item", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(12)
search_field = div.find(".chosen-search-input").first()
search_field.val("一")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result")[0].innerHTML).toBe("一")
it "matches in non-ascii languages like Chinese when selecting a single item with search_contains", ->
div = $("
").html("""
""")
div.find("select").chosen({search_contains: true})
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(12)
search_field = div.find(".chosen-search-input").first()
search_field.val("一")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(2)
expect(div.find(".active-result")[0].innerHTML).toBe("一")
expect(div.find(".active-result")[1].innerHTML).toBe("十一")
it "matches in non-ascii languages like Chinese when selecting multiple items", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(12)
search_field = div.find(".chosen-search-input")
search_field.val("一")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result")[0].innerHTML).toBe("一")
it "matches in non-ascii languages like Chinese when selecting multiple items with search_contains", ->
div = $("
").html("""
""")
div.find("select").chosen({search_contains: true})
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(12)
search_field = div.find(".chosen-search-input")
search_field.val("一")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(2)
expect(div.find(".active-result")[0].innerHTML).toBe("一")
expect(div.find(".active-result")[1].innerHTML).toBe("十一")
it "highlights results correctly when multiple words are present", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
expect(div.find(".active-result").length).toBe(1)
search_field = div.find(".chosen-search-input")
search_field.val("h")
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result")[0].innerHTML).toBe("oh hello")
describe "respects word boundaries when not using search_contains", ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
search_field = div.find(".chosen-search-input")
div.find("option").each () ->
boundary_thing = this.value.slice(1)
it "correctly finds words that start after a(n) #{boundary_thing}", ->
search_field.val(boundary_thing)
search_field.trigger("keyup")
expect(div.find(".active-result").length).toBe(1)
expect(div.find(".active-result")[0].innerText.slice(1)).toBe(boundary_thing)
it "does not clear search text or highlight first suggestion when holding ctrl/cmd and selecting multiple items", () ->
div = $("
").html("""
""")
div.find("select").chosen()
div.find(".chosen-container").trigger("mousedown") # open the drop
search_field = div.find(".chosen-search-input")
search_field.val("s").trigger("keyup")
results = div.find(".chosen-results")
first_result = results.find("li:nth-of-type(1)")
second_result = results.find("li:nth-of-type(2)")
expect(first_result.prop("class")).toContain("highlighted")
mouseup_with_meta = $.Event("mouseup")
mouseup_with_meta.metaKey = true
second_result.mouseover().trigger(mouseup_with_meta)
expect(div.find(".search-choice").length).toBe(1)
expect(search_field.val()).toBe("s")
expect(first_result.prop("class")).not.toContain("highlighted")
third_result = results.find("li:nth-of-type(3)")
mouseup_with_ctrl = $.Event("mouseup")
mouseup_with_ctrl.ctrlKey = true
third_result.mouseover().trigger(mouseup_with_ctrl)
expect(div.find(".search-choice").length).toBe(2)
expect(search_field.val()).toBe("s")
expect(first_result.prop("class")).not.toContain("highlighted")
it "clears search text and highlights first suggestion without ctrl/cmd and selecting multiple items without hide_results_on_select", () ->
div = $("
").html("""
""")
div.find("select").chosen(hide_results_on_select: false)
div.find(".chosen-container").trigger("mousedown") # open the drop
search_field = div.find(".chosen-search-input")
search_field.val("s").trigger("keyup")
results = div.find(".chosen-results")
first_result = results.find("li:nth-of-type(1)")
second_result = results.find("li:nth-of-type(2)")
expect(first_result.prop("class")).toContain("highlighted")
second_result.mouseover().mouseup()
expect(div.find(".search-choice").length).toBe(1)
expect(search_field.val()).toBe("")
expect(results.find("li:nth-of-type(1)").prop("class")).toContain("highlighted")