| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { click } from "@odoo/hoot-dom"; |
| 3 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 4 |
import { Component, useState, xml } from "@odoo/owl"; |
| 5 |
import { contains, mountWithCleanup } from "@web/../tests/web_test_helpers"; |
| 6 |
|
| 7 |
import { ViewScaleSelector } from "@web/views/view_components/view_scale_selector"; |
| 8 |
|
| 9 |
test("basic ViewScaleSelector component usage", async () => { |
| 10 |
class Parent extends Component { |
| 11 |
static components = { ViewScaleSelector }; |
| 12 |
static template = xml`<ViewScaleSelector t-props="compProps" />`; |
| 13 |
static props = ["*"]; |
| 14 |
setup() { |
| 15 |
this.state = useState({ |
| 16 |
scale: "week", |
| 17 |
}); |
| 18 |
} |
| 19 |
get compProps() { |
| 20 |
return { |
| 21 |
scales: { |
| 22 |
day: { |
| 23 |
description: "Daily", |
| 24 |
}, |
| 25 |
week: { |
| 26 |
description: "Weekly", |
| 27 |
hotkey: "o", |
| 28 |
}, |
| 29 |
year: { |
| 30 |
description: "Yearly", |
| 31 |
}, |
| 32 |
}, |
| 33 |
isWeekendVisible: true, |
| 34 |
setScale: (scale) => { |
| 35 |
this.state.scale = scale; |
| 36 |
expect.step(scale); |
| 37 |
}, |
| 38 |
toggleWeekendVisibility: () => { |
| 39 |
expect.step("toggleWeekendVisibility"); |
| 40 |
}, |
| 41 |
currentScale: this.state.scale, |
| 42 |
}; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
await mountWithCleanup(Parent); |
| 47 |
expect(".o_view_scale_selector").toHaveCount(1); |
| 48 |
expect.verifySteps([]); |
| 49 |
expect(".o_view_scale_selector").toHaveText("Weekly"); |
| 50 |
expect(".scale_button_selection").toHaveAttribute("data-hotkey", "v"); |
| 51 |
await click(".scale_button_selection"); |
| 52 |
await animationFrame(); |
| 53 |
expect(".o-dropdown--menu").toHaveCount(1); |
| 54 |
expect(".o-dropdown--menu .active:first").toHaveText("Weekly", { |
| 55 |
message: "the active option is selected", |
| 56 |
}); |
| 57 |
expect(".o-dropdown--menu span:nth-child(2)").toHaveAttribute("data-hotkey", "o", { |
| 58 |
message: "'week' scale has the right hotkey", |
| 59 |
}); |
| 60 |
await click(".o_scale_button_day"); |
| 61 |
await animationFrame(); |
| 62 |
expect.verifySteps(["day"]); |
| 63 |
expect(".o_view_scale_selector").toHaveText("Daily"); |
| 64 |
await click(".scale_button_selection"); |
| 65 |
expect(".dropdown-item:last:interactive").not.toHaveCount(); |
| 66 |
await contains(".dropdown-item:contains(Yearly)").click(); |
| 67 |
await click(".scale_button_selection"); |
| 68 |
await contains(".dropdown-item:last").click(); |
| 69 |
expect.verifySteps(["year", "toggleWeekendVisibility"]); |
| 70 |
}); |
| 71 |
|
| 72 |
test("ViewScaleSelector with only one scale available", async () => { |
| 73 |
class Parent extends Component { |
| 74 |
static components = { ViewScaleSelector }; |
| 75 |
static template = xml`<ViewScaleSelector t-props="compProps" />`; |
| 76 |
static props = ["*"]; |
| 77 |
setup() { |
| 78 |
this.state = useState({ |
| 79 |
scale: "day", |
| 80 |
}); |
| 81 |
} |
| 82 |
get compProps() { |
| 83 |
return { |
| 84 |
scales: { |
| 85 |
day: { |
| 86 |
description: "Daily", |
| 87 |
}, |
| 88 |
}, |
| 89 |
setScale: () => {}, |
| 90 |
isWeekendVisible: false, |
| 91 |
toggleWeekendVisibility: () => {}, |
| 92 |
currentScale: this.state.scale, |
| 93 |
}; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
await mountWithCleanup(Parent); |
| 98 |
expect(".o_view_scale_selector").toHaveCount(0); |
| 99 |
}); |
| 100 |
|
| 101 |
test("ViewScaleSelector show weekends button is disabled when scale is day", async () => { |
| 102 |
class Parent extends Component { |
| 103 |
static components = { ViewScaleSelector }; |
| 104 |
static template = xml`<ViewScaleSelector t-props="compProps"/>`; |
| 105 |
static props = ["*"]; |
| 106 |
setup() { |
| 107 |
this.state = useState({ |
| 108 |
scale: "day", |
| 109 |
}); |
| 110 |
} |
| 111 |
get compProps() { |
| 112 |
return { |
| 113 |
scales: { |
| 114 |
day: { |
| 115 |
description: "Daily", |
| 116 |
}, |
| 117 |
week: { |
| 118 |
description: "Weekly", |
| 119 |
hotkey: "o", |
| 120 |
}, |
| 121 |
year: { |
| 122 |
description: "Yearly", |
| 123 |
}, |
| 124 |
}, |
| 125 |
setScale: (key) => (this.state.scale = key), |
| 126 |
isWeekendVisible: false, |
| 127 |
toggleWeekendVisibility: () => {}, |
| 128 |
currentScale: this.state.scale, |
| 129 |
}; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
await mountWithCleanup(Parent); |
| 134 |
expect(".o_view_scale_selector").toHaveCount(1); |
| 135 |
await click(".scale_button_selection"); |
| 136 |
await animationFrame(); |
| 137 |
expect(".o_show_weekends").toHaveClass("disabled"); |
| 138 |
await click(".dropdown-item:nth-child(2)"); |
| 139 |
await animationFrame(); |
| 140 |
await click(".scale_button_selection"); |
| 141 |
await animationFrame(); |
| 142 |
expect(".o_show_weekends").not.toHaveClass("disabled"); |
| 143 |
}); |