| 1 |
/* global ace */ |
| 2 |
|
| 3 |
import { expect, getFixture, test } from "@odoo/hoot"; |
| 4 |
import { queryOne } from "@odoo/hoot-dom"; |
| 5 |
import { animationFrame } from "@odoo/hoot-mock"; |
| 6 |
|
| 7 |
import { |
| 8 |
clickSave, |
| 9 |
contains, |
| 10 |
defineModels, |
| 11 |
editAce, |
| 12 |
fields, |
| 13 |
models, |
| 14 |
mountView, |
| 15 |
onRpc, |
| 16 |
pagerNext, |
| 17 |
preloadBundle, |
| 18 |
preventResizeObserverError, |
| 19 |
} from "@web/../tests/web_test_helpers"; |
| 20 |
|
| 21 |
class Partner extends models.Model { |
| 22 |
_name = "res.partner"; |
| 23 |
_rec_name = "display_name"; |
| 24 |
|
| 25 |
foo = fields.Text({ default: "My little Foo Value" }); |
| 26 |
|
| 27 |
_records = [ |
| 28 |
{ id: 1, foo: "yop" }, |
| 29 |
{ id: 2, foo: "blip" }, |
| 30 |
]; |
| 31 |
} |
| 32 |
|
| 33 |
defineModels([Partner]); |
| 34 |
|
| 35 |
preloadBundle("web.ace_lib"); |
| 36 |
preventResizeObserverError(); |
| 37 |
|
| 38 |
test("AceEditorField on text fields works", async () => { |
| 39 |
await mountView({ |
| 40 |
resModel: "res.partner", |
| 41 |
resId: 1, |
| 42 |
type: "form", |
| 43 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 44 |
}); |
| 45 |
expect(window).toInclude("ace", { message: "the ace library should be loaded" }); |
| 46 |
expect(`div.ace_content`).toHaveCount(1); |
| 47 |
expect(".o_field_code").toHaveText(/yop/); |
| 48 |
}); |
| 49 |
|
| 50 |
test("AceEditorField mark as dirty as soon at onchange", async () => { |
| 51 |
await mountView({ |
| 52 |
resModel: "res.partner", |
| 53 |
resId: 1, |
| 54 |
type: "form", |
| 55 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 56 |
}); |
| 57 |
|
| 58 |
const aceEditor = queryOne`.ace_editor`; |
| 59 |
expect(aceEditor).toHaveText(/yop/); |
| 60 |
|
| 61 |
// edit the foo field |
| 62 |
ace.edit(aceEditor).setValue("blip"); |
| 63 |
await animationFrame(); |
| 64 |
expect(`.o_form_status_indicator_buttons`).toHaveCount(1); |
| 65 |
expect(`.o_form_status_indicator_buttons`).not.toHaveClass("invisible"); |
| 66 |
|
| 67 |
// revert edition |
| 68 |
ace.edit(aceEditor).setValue("yop"); |
| 69 |
await animationFrame(); |
| 70 |
expect(`.o_form_status_indicator_buttons`).toHaveCount(1); |
| 71 |
expect(`.o_form_status_indicator_buttons`).toHaveClass("invisible"); |
| 72 |
}); |
| 73 |
|
| 74 |
test("AceEditorField on html fields works", async () => { |
| 75 |
Partner._fields.html_field = fields.Html(); |
| 76 |
Partner._records.push({ id: 3, html_field: `<p>My little HTML Test</p>` }); |
| 77 |
|
| 78 |
onRpc(({ method }) => expect.step(method)); |
| 79 |
|
| 80 |
await mountView({ |
| 81 |
resModel: "res.partner", |
| 82 |
resId: 3, |
| 83 |
type: "form", |
| 84 |
arch: `<form><field name="html_field" widget="code" /></form>`, |
| 85 |
}); |
| 86 |
expect(".o_field_code").toHaveText(/My little HTML Test/); |
| 87 |
expect.verifySteps(["get_views", "web_read"]); |
| 88 |
|
| 89 |
// Modify foo and save |
| 90 |
await editAce("DEF"); |
| 91 |
await clickSave(); |
| 92 |
expect.verifySteps(["web_save"]); |
| 93 |
}); |
| 94 |
|
| 95 |
test.tags("desktop", "focus required"); |
| 96 |
test("AceEditorField doesn't crash when editing", async () => { |
| 97 |
await mountView({ |
| 98 |
resModel: "res.partner", |
| 99 |
resId: 1, |
| 100 |
type: "form", |
| 101 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 102 |
}); |
| 103 |
|
| 104 |
await contains(".ace_editor .ace_content").click(); |
| 105 |
expect(".ace-view-editor").toHaveClass("ace_focus"); |
| 106 |
}); |
| 107 |
|
| 108 |
test("AceEditorField is updated on value change", async () => { |
| 109 |
await mountView({ |
| 110 |
resModel: "res.partner", |
| 111 |
resId: 1, |
| 112 |
resIds: [1, 2], |
| 113 |
type: "form", |
| 114 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 115 |
}); |
| 116 |
expect(".o_field_code").toHaveText(/yop/); |
| 117 |
|
| 118 |
await pagerNext(); |
| 119 |
await animationFrame(); |
| 120 |
await animationFrame(); |
| 121 |
expect(".o_field_code").toHaveText(/blip/); |
| 122 |
}); |
| 123 |
|
| 124 |
test("leaving an untouched record with an unset ace field should not write", async () => { |
| 125 |
for (const record of Partner._records) { |
| 126 |
record.foo = false; |
| 127 |
} |
| 128 |
|
| 129 |
onRpc(({ args, method }) => { |
| 130 |
if (method) { |
| 131 |
expect.step(`${method}: ${JSON.stringify(args)}`); |
| 132 |
} |
| 133 |
}); |
| 134 |
|
| 135 |
await mountView({ |
| 136 |
resModel: "res.partner", |
| 137 |
resId: 1, |
| 138 |
resIds: [1, 2], |
| 139 |
type: "form", |
| 140 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 141 |
}); |
| 142 |
expect.verifySteps(["get_views: []", "web_read: [[1]]"]); |
| 143 |
|
| 144 |
await pagerNext(); |
| 145 |
expect.verifySteps(["web_read: [[2]]"]); |
| 146 |
}); |
| 147 |
|
| 148 |
test.tags("focus required"); |
| 149 |
test("AceEditorField only trigger onchanges when blurred", async () => { |
| 150 |
Partner._onChanges.foo = () => {}; |
| 151 |
for (const record of Partner._records) { |
| 152 |
record.foo = false; |
| 153 |
} |
| 154 |
|
| 155 |
onRpc(({ args, method }) => { |
| 156 |
expect.step(`${method}: ${JSON.stringify(args)}`); |
| 157 |
}); |
| 158 |
|
| 159 |
await mountView({ |
| 160 |
resModel: "res.partner", |
| 161 |
resId: 1, |
| 162 |
resIds: [1, 2], |
| 163 |
type: "form", |
| 164 |
arch: `<form><field name="display_name"/><field name="foo" widget="code"/></form>`, |
| 165 |
}); |
| 166 |
expect.verifySteps(["get_views: []", "web_read: [[1]]"]); |
| 167 |
|
| 168 |
await editAce("a"); |
| 169 |
await contains(getFixture()).focus(); // blur ace editor |
| 170 |
expect.verifySteps([`onchange: [[1],{"foo":"a"},["foo"],{"display_name":{},"foo":{}}]`]); |
| 171 |
|
| 172 |
await clickSave(); |
| 173 |
expect.verifySteps([`web_save: [[1],{"foo":"a"}]`]); |
| 174 |
}); |
| 175 |
|
| 176 |
test("Save and Discard buttons are displayed when necessary", async () => { |
| 177 |
await mountView({ |
| 178 |
resModel: "res.partner", |
| 179 |
resId: 1, |
| 180 |
type: "form", |
| 181 |
arch: `<form><field name="foo" widget="code"/></form>`, |
| 182 |
}); |
| 183 |
|
| 184 |
await editAce("a"); |
| 185 |
expect(`.o_form_status_indicator_buttons`).toHaveCount(1); |
| 186 |
expect(`.o_form_status_indicator_buttons`).not.toHaveClass("invisible"); |
| 187 |
await clickSave(); |
| 188 |
expect(`.o_form_status_indicator_buttons`).toHaveCount(1); |
| 189 |
expect(`.o_form_status_indicator_buttons`).toHaveClass("invisible"); |
| 190 |
}); |