| 1 | import { describe, expect, test } from "@odoo/hoot"; |
| 2 | import { press, queryAll, queryOne } from "@odoo/hoot-dom"; |
| 3 | import { startInteractions, setupInteractionWhiteList } from "@web/../tests/public/helpers"; |
| 4 | |
| 5 | setupInteractionWhiteList("website.search_bar_results"); |
| 6 | describe.current.tags("interaction_dev"); |
| 7 | |
| 8 | const searchTemplate = ` |
| 9 | <form method="get" class="o_searchbar_form s_searchbar_input" action="/website/search" data-snippet="s_searchbar_input"> |
| 10 | <div role="search" class="input-group input-group-lg"> |
| 11 | <input type="search" name="search" class="search-query form-control oe_search_box" placeholder="Search..." |
| 12 | data-search-type="test" |
| 13 | data-limit="3" |
| 14 | data-display-image="false" |
| 15 | data-display-description="false" |
| 16 | data-display-extra-link="true" |
| 17 | data-display-detail="false" |
| 18 | data-order-by="name asc" |
| 19 | autocomplete="off" |
| 20 | /> |
| 21 | <button type="submit" aria-label="Search" title="Search" class="btn oe_search_button border border-start-0 px-4 bg-o-color-4"> |
| 22 | <i class="oi oi-search"></i> |
| 23 | </button> |
| 24 | </div> |
| 25 | <input name="order" type="hidden" class="o_search_order_by" value="test desc"/> |
| 26 | <div class="o_dropdown_menu show position-absolute dropdown"> |
| 27 | <a class="dropdown-item" href="/website/test/xyz-1"> |
| 28 | <div class="o_search_result_item"> |
| 29 | Xyz 1 |
| 30 | </div> |
| 31 | </a> |
| 32 | <a class="dropdown-item" href="/website/test/xyz-2"> |
| 33 | <div class="o_search_result_item"> |
| 34 | Xyz 2 |
| 35 | </div> |
| 36 | </a> |
| 37 | </div> |
| 38 | </form> |
| 39 | `; |
| 40 | |
| 41 | test("searchbar selects next result on cursor down", async () => { |
| 42 | await startInteractions(searchTemplate); |
| 43 | const resultEls = queryAll("form a:has(.o_search_result_item)"); |
| 44 | resultEls[0].focus(); |
| 45 | await press("down"); |
| 46 | expect(document.activeElement).toBe(resultEls[1]); |
| 47 | }); |
| 48 | |
| 49 | test("searchbar selects input on cursor down on last result", async () => { |
| 50 | await startInteractions(searchTemplate); |
| 51 | queryOne("form a:has(.o_search_result_item):eq(1)").focus(); |
| 52 | await press("down"); |
| 53 | expect(document.activeElement).toBe(queryOne("form input[type=search]")); |
| 54 | }); |
| 55 | |
| 56 | test("searchbar selects previous result on cursor up", async () => { |
| 57 | await startInteractions(searchTemplate); |
| 58 | const resultEls = queryAll("form a:has(.o_search_result_item)"); |
| 59 | resultEls[1].focus(); |
| 60 | await press("up"); |
| 61 | expect(document.activeElement).toBe(resultEls[0]); |
| 62 | }); |
| 63 | |
| 64 | test("searchbar selects input on cursor up on first result", async () => { |
| 65 | await startInteractions(searchTemplate); |
| 66 | queryOne("form a:has(.o_search_result_item):first").focus(); |
| 67 | await press("up"); |
| 68 | expect(document.activeElement).toBe(queryOne("form input[type=search]")); |
| 69 | }); |