| 1 |
import { NameAndSignature } from "@web/core/signature/name_and_signature"; |
| 2 |
import { SignatureWidget } from "@web/views/widgets/signature/signature"; |
| 3 |
|
| 4 |
import { |
| 5 |
defineModels, |
| 6 |
fields, |
| 7 |
models, |
| 8 |
mountView, |
| 9 |
onRpc, |
| 10 |
patchWithCleanup, |
| 11 |
contains, |
| 12 |
clickModalButton, |
| 13 |
} from "@web/../tests/web_test_helpers"; |
| 14 |
import { beforeEach, test, expect } from "@odoo/hoot"; |
| 15 |
import { click, queryFirst, waitFor } from "@odoo/hoot-dom"; |
| 16 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 17 |
|
| 18 |
class Partner extends models.Model { |
| 19 |
display_name = fields.Char(); |
| 20 |
product_id = fields.Many2one({ string: "Product Name", relation: "product" }); |
| 21 |
sign = fields.Binary({ string: "Signature" }); |
| 22 |
signature = fields.Char(); |
| 23 |
|
| 24 |
_records = [ |
| 25 |
{ |
| 26 |
id: 1, |
| 27 |
display_name: "Pop's Chock'lit", |
| 28 |
product_id: 7, |
| 29 |
}, |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
class Product extends models.Model { |
| 34 |
name = fields.Char({ string: "Product Name" }); |
| 35 |
_records = [ |
| 36 |
{ |
| 37 |
id: 7, |
| 38 |
name: "Veggie Burger", |
| 39 |
}, |
| 40 |
]; |
| 41 |
} |
| 42 |
|
| 43 |
defineModels([Partner, Product]); |
| 44 |
|
| 45 |
beforeEach(async () => { |
| 46 |
onRpc("/web/sign/get_fonts/", () => ({})); |
| 47 |
}); |
| 48 |
|
| 49 |
test.tags("desktop"); |
| 50 |
test("Signature widget renders a Sign button on desktop", async () => { |
| 51 |
patchWithCleanup(NameAndSignature.prototype, { |
| 52 |
setup() { |
| 53 |
super.setup(); |
| 54 |
expect(this.props.signature.name).toBe(""); |
| 55 |
}, |
| 56 |
}); |
| 57 |
await mountView({ |
| 58 |
type: "form", |
| 59 |
resModel: "partner", |
| 60 |
resId: 1, |
| 61 |
arch: /* xml */ ` |
| 62 |
<form> |
| 63 |
<header> |
| 64 |
<widget name="signature" string="Sign"/> |
| 65 |
</header> |
| 66 |
</form>`, |
| 67 |
}); |
| 68 |
|
| 69 |
expect("button.o_sign_button").toHaveClass("btn-secondary", { |
| 70 |
message: `The button must have the 'btn-secondary' class as "highlight=0"`, |
| 71 |
}); |
| 72 |
expect(".o_widget_signature button.o_sign_button").toHaveCount(1, { |
| 73 |
message: "Should have a signature widget button", |
| 74 |
}); |
| 75 |
expect(".modal-dialog").toHaveCount(0, { |
| 76 |
message: "Should not have any modal", |
| 77 |
}); |
| 78 |
// Clicks on the sign button to open the sign modal. |
| 79 |
await click(".o_widget_signature button.o_sign_button"); |
| 80 |
await waitFor(".modal .modal-body"); |
| 81 |
expect(".modal-dialog").toHaveCount(1, { |
| 82 |
message: "Should have one modal opened", |
| 83 |
}); |
| 84 |
}); |
| 85 |
|
| 86 |
test.tags("mobile"); |
| 87 |
test("Signature widget renders a Sign button on mobile", async () => { |
| 88 |
patchWithCleanup(NameAndSignature.prototype, { |
| 89 |
setup() { |
| 90 |
super.setup(); |
| 91 |
expect(this.props.signature.name).toBe(""); |
| 92 |
}, |
| 93 |
}); |
| 94 |
await mountView({ |
| 95 |
type: "form", |
| 96 |
resModel: "partner", |
| 97 |
resId: 1, |
| 98 |
arch: /* xml */ ` |
| 99 |
<form> |
| 100 |
<header> |
| 101 |
<widget name="signature" string="Sign"/> |
| 102 |
</header> |
| 103 |
</form>`, |
| 104 |
}); |
| 105 |
|
| 106 |
await contains(`.o_cp_action_menus button:has(.fa-cog)`).click(); |
| 107 |
expect("button.o_sign_button").toHaveClass("btn-secondary", { |
| 108 |
message: `The button must have the 'btn-secondary' class as "highlight=0"`, |
| 109 |
}); |
| 110 |
expect(".o_widget_signature button.o_sign_button").toHaveCount(1, { |
| 111 |
message: "Should have a signature widget button", |
| 112 |
}); |
| 113 |
expect(".modal-dialog").toHaveCount(0, { |
| 114 |
message: "Should not have any modal", |
| 115 |
}); |
| 116 |
// Clicks on the sign button to open the sign modal. |
| 117 |
await click(".o_widget_signature button.o_sign_button"); |
| 118 |
await waitFor(".modal .modal-body"); |
| 119 |
expect(".modal-dialog").toHaveCount(1, { |
| 120 |
message: "Should have one modal opened", |
| 121 |
}); |
| 122 |
}); |
| 123 |
|
| 124 |
test.tags("desktop"); |
| 125 |
test("Signature widget: full_name option on desktop", async () => { |
| 126 |
patchWithCleanup(NameAndSignature.prototype, { |
| 127 |
setup() { |
| 128 |
super.setup(); |
| 129 |
expect.step(this.props.signature.name); |
| 130 |
}, |
| 131 |
}); |
| 132 |
|
| 133 |
await mountView({ |
| 134 |
type: "form", |
| 135 |
resModel: "partner", |
| 136 |
resId: 1, |
| 137 |
arch: /* xml */ ` |
| 138 |
<form> |
| 139 |
<header> |
| 140 |
<widget name="signature" string="Sign" full_name="display_name"/> |
| 141 |
</header> |
| 142 |
<field name="display_name"/> |
| 143 |
</form>`, |
| 144 |
}); |
| 145 |
// Clicks on the sign button to open the sign modal. |
| 146 |
await click("span.o_sign_label"); |
| 147 |
await waitFor(".modal .modal-body"); |
| 148 |
expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(1); |
| 149 |
expect.verifySteps(["Pop's Chock'lit"]); |
| 150 |
}); |
| 151 |
|
| 152 |
test.tags("mobile"); |
| 153 |
test("Signature widget: full_name option on mobile", async () => { |
| 154 |
patchWithCleanup(NameAndSignature.prototype, { |
| 155 |
setup() { |
| 156 |
super.setup(); |
| 157 |
expect.step(this.props.signature.name); |
| 158 |
}, |
| 159 |
}); |
| 160 |
|
| 161 |
await mountView({ |
| 162 |
type: "form", |
| 163 |
resModel: "partner", |
| 164 |
resId: 1, |
| 165 |
arch: /* xml */ ` |
| 166 |
<form> |
| 167 |
<header> |
| 168 |
<widget name="signature" string="Sign" full_name="display_name"/> |
| 169 |
</header> |
| 170 |
<field name="display_name"/> |
| 171 |
</form>`, |
| 172 |
}); |
| 173 |
|
| 174 |
await contains(`.o_cp_action_menus button:has(.fa-cog)`).click(); |
| 175 |
// Clicks on the sign button to open the sign modal. |
| 176 |
await click("span.o_sign_label"); |
| 177 |
await waitFor(".modal .modal-body"); |
| 178 |
expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(1); |
| 179 |
expect.verifySteps(["Pop's Chock'lit"]); |
| 180 |
}); |
| 181 |
|
| 182 |
test.tags("desktop"); |
| 183 |
test("Signature widget: highlight option on desktop", async () => { |
| 184 |
await mountView({ |
| 185 |
type: "form", |
| 186 |
resModel: "partner", |
| 187 |
resId: 1, |
| 188 |
arch: /* xml */ ` |
| 189 |
<form> |
| 190 |
<header> |
| 191 |
<widget name="signature" string="Sign" highlight="1"/> |
| 192 |
</header> |
| 193 |
</form>`, |
| 194 |
}); |
| 195 |
|
| 196 |
expect("button.o_sign_button").toHaveClass("btn-primary", { |
| 197 |
message: `The button must have the 'btn-primary' class as "highlight=1"`, |
| 198 |
}); |
| 199 |
|
| 200 |
// Clicks on the sign button to open the sign modal. |
| 201 |
await click(".o_widget_signature button.o_sign_button"); |
| 202 |
await waitFor(".modal .modal-body"); |
| 203 |
expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(0); |
| 204 |
}); |
| 205 |
|
| 206 |
test.tags("mobile"); |
| 207 |
test("Signature widget: highlight option on mobile", async () => { |
| 208 |
await mountView({ |
| 209 |
type: "form", |
| 210 |
resModel: "partner", |
| 211 |
resId: 1, |
| 212 |
arch: /* xml */ ` |
| 213 |
<form> |
| 214 |
<header> |
| 215 |
<widget name="signature" string="Sign" highlight="1"/> |
| 216 |
</header> |
| 217 |
</form>`, |
| 218 |
}); |
| 219 |
|
| 220 |
await contains(`.o_cp_action_menus button:has(.fa-cog)`).click(); |
| 221 |
expect("button.o_sign_button").toHaveClass("btn-primary", { |
| 222 |
message: `The button must have the 'btn-primary' class as "highlight=1"`, |
| 223 |
}); |
| 224 |
|
| 225 |
// Clicks on the sign button to open the sign modal. |
| 226 |
await click(".o_widget_signature button.o_sign_button"); |
| 227 |
await waitFor(".modal .modal-body"); |
| 228 |
expect(".modal .modal-body a.o_web_sign_auto_button").toHaveCount(0); |
| 229 |
}); |
| 230 |
|
| 231 |
test.tags("mobile"); |
| 232 |
test("Signature widget works inside of a dropdown", async () => { |
| 233 |
patchWithCleanup(SignatureWidget.prototype, { |
| 234 |
async onClickSignature() { |
| 235 |
await super.onClickSignature(...arguments); |
| 236 |
expect.step("onClickSignature"); |
| 237 |
}, |
| 238 |
async uploadSignature({ signatureImage }) { |
| 239 |
await super.uploadSignature(...arguments); |
| 240 |
expect.step("uploadSignature"); |
| 241 |
}, |
| 242 |
}); |
| 243 |
|
| 244 |
await mountView({ |
| 245 |
type: "form", |
| 246 |
resModel: "partner", |
| 247 |
resId: 1, |
| 248 |
arch: ` |
| 249 |
<form> |
| 250 |
<header> |
| 251 |
<button string="Dummy"/> |
| 252 |
<widget name="signature" string="Sign" full_name="display_name"/> |
| 253 |
</header> |
| 254 |
<field name="display_name" /> |
| 255 |
</form> |
| 256 |
`, |
| 257 |
}); |
| 258 |
|
| 259 |
// change display_name to enable auto-sign feature |
| 260 |
await contains(".o_field_widget[name=display_name] input").edit("test"); |
| 261 |
|
| 262 |
// open the signature dialog |
| 263 |
await contains(".o_statusbar_buttons button:has(.oi-ellipsis-v").click(); |
| 264 |
await contains(".o_widget_signature button.o_sign_button").click(); |
| 265 |
await waitFor(".modal .modal-body"); |
| 266 |
|
| 267 |
// use auto-sign feature, might take a while |
| 268 |
await contains(".o_web_sign_auto_button").click(); |
| 269 |
|
| 270 |
expect(".modal-footer button.btn-primary").toHaveCount(1); |
| 271 |
|
| 272 |
let maxDelay = 100; |
| 273 |
while (queryFirst(".modal-footer button.btn-primary")["disabled"] && maxDelay > 0) { |
| 274 |
await animationFrame(); |
| 275 |
maxDelay--; |
| 276 |
} |
| 277 |
|
| 278 |
expect(maxDelay).toBeGreaterThan(0, { message: "Timeout exceeded" }); |
| 279 |
|
| 280 |
// close the dialog and save the signature |
| 281 |
await clickModalButton({ text: "Adopt & Sign" }); |
| 282 |
|
| 283 |
expect(".modal-dialog").toHaveCount(0, { message: "Should have no modal opened" }); |
| 284 |
expect.verifySteps(["onClickSignature", "uploadSignature"]); |
| 285 |
}); |