| 1 |
import { defineMailModels } from "@mail/../tests/mail_test_helpers"; |
| 2 |
import { beforeEach, describe, expect, test } from "@odoo/hoot"; |
| 3 |
import { queryAll, queryAllProperties, queryAllTexts, queryFirst } from "@odoo/hoot-dom"; |
| 4 |
import { mockDate } from "@odoo/hoot-mock"; |
| 5 |
import { toggleFilter } from "@web/../tests/views/calendar/calendar_test_helpers"; |
| 6 |
import { contains, defineModels, fields, mockService, models, mountView, onRpc, serverState } from "@web/../tests/web_test_helpers"; |
| 7 |
|
| 8 |
const { DateTime, Interval } = luxon; |
| 9 |
|
| 10 |
describe.current.tags("desktop"); |
| 11 |
|
| 12 |
class CalendarEvent extends models.Model { |
| 13 |
_records = [ |
| 14 |
{ |
| 15 |
id: 1, |
| 16 |
user_id: serverState.userId, |
| 17 |
partner_id: serverState.partnerId, |
| 18 |
name: "event 1", |
| 19 |
start: "2016-12-11 00:00:00", |
| 20 |
stop: "2016-12-11 01:00:00", |
| 21 |
allday: false, |
| 22 |
partner_ids: [models.Command.link(serverState.partnerId), models.Command.link(2), models.Command.link(3)], |
| 23 |
}, |
| 24 |
{ |
| 25 |
id: 2, |
| 26 |
user_id: serverState.userId, |
| 27 |
partner_id: serverState.partnerId, |
| 28 |
name: "event 2", |
| 29 |
start: "2016-12-12 10:55:05", |
| 30 |
stop: "2016-12-12 14:55:05", |
| 31 |
allday: false, |
| 32 |
partner_ids: [models.Command.link(serverState.partnerId), models.Command.link(2)], |
| 33 |
}, |
| 34 |
]; |
| 35 |
|
| 36 |
user_id = fields.Many2one({ relation: "users" }); |
| 37 |
partner_id = fields.Many2one({ relation: "partner" }); |
| 38 |
name = fields.Char(); |
| 39 |
start = fields.Datetime(); |
| 40 |
stop = fields.Datetime(); |
| 41 |
allday = fields.Boolean(); |
| 42 |
partner_ids = fields.One2many({ relation: "partner" }); |
| 43 |
} |
| 44 |
|
| 45 |
class CalendarFilter extends models.Model { |
| 46 |
_records = [ |
| 47 |
{ id: 1, user_id: serverState.userId, partner_id: serverState.partnerId, partner_checked: true }, |
| 48 |
{ id: 2, user_id: 2, partner_id: 2, partner_checked: true }, |
| 49 |
]; |
| 50 |
|
| 51 |
user_id = fields.Many2one({ relation: "users" }); |
| 52 |
partner_id = fields.Many2one({ relation: "partner" }); |
| 53 |
partner_checked = fields.Boolean(); |
| 54 |
} |
| 55 |
|
| 56 |
class HrEmployee extends models.Model { |
| 57 |
_records = [ |
| 58 |
{ id: 1, name: "Aaron", partner_id: serverState.partnerId }, |
| 59 |
{ id: 2, name: "Brian", partner_id: 2 }, |
| 60 |
]; |
| 61 |
|
| 62 |
name = fields.Char(); |
| 63 |
partner_id = fields.Many2one({ relation: "partner" }); |
| 64 |
} |
| 65 |
|
| 66 |
class HrWorkLocation extends models.Model { |
| 67 |
_records = [ |
| 68 |
{ id: 1, name: "Office", location_type: "office" }, |
| 69 |
{ id: 2, name: "Home", location_type: "home" }, |
| 70 |
]; |
| 71 |
|
| 72 |
name = fields.Char(); |
| 73 |
location_type = fields.Selection({ |
| 74 |
selection: [['home', "Home"], ['office', "Office"], ["other", "Other"]], |
| 75 |
}); |
| 76 |
} |
| 77 |
|
| 78 |
class Partner extends models.Model { |
| 79 |
_records = [ |
| 80 |
{ id: serverState.partnerId, name: "Partner 1", image: "AAA" }, |
| 81 |
{ id: 2, name: "Partner 2", image: "BBB" }, |
| 82 |
{ id: 3, name: "Partner 3", image: "CCC" }, |
| 83 |
]; |
| 84 |
|
| 85 |
name = fields.Char(); |
| 86 |
image = fields.Binary(); |
| 87 |
} |
| 88 |
|
| 89 |
class Users extends models.Model { |
| 90 |
_records = [ |
| 91 |
{ id: serverState.userId, name: "User 1", partner_id: serverState.partnerId }, |
| 92 |
{ id: 2, name: "User 2", partner_id: 2 }, |
| 93 |
]; |
| 94 |
|
| 95 |
name = fields.Char(); |
| 96 |
partner_id = fields.Many2one({ relation: "partner" }); |
| 97 |
} |
| 98 |
|
| 99 |
defineModels([ |
| 100 |
CalendarEvent, |
| 101 |
CalendarFilter, |
| 102 |
HrEmployee, |
| 103 |
HrWorkLocation, |
| 104 |
Partner, |
| 105 |
Users, |
| 106 |
]); |
| 107 |
defineMailModels(); |
| 108 |
|
| 109 |
onRpc("/calendar/check_credentials", async () => ({})); |
| 110 |
onRpc("check_synchronization_status", async () => ({})); |
| 111 |
onRpc("get_attendee_detail", () => []); |
| 112 |
onRpc("get_default_duration", () => 1); |
| 113 |
onRpc("get_state_selections", () => [ |
| 114 |
["accepted", "Yes"], |
| 115 |
["declined", "No"], |
| 116 |
["tentative", "Maybe"], |
| 117 |
["needsAction", "Needs Action"], |
| 118 |
]); |
| 119 |
onRpc("res.users", "read", () => [{ user: serverState.userId }]); |
| 120 |
|
| 121 |
beforeEach(() => { |
| 122 |
mockDate("2020-12-10 15:00:00"); |
| 123 |
}); |
| 124 |
|
| 125 |
const WORK_LOCATION_FALSE = { |
| 126 |
location_type: false, |
| 127 |
location_name: false, |
| 128 |
work_location_id: false, |
| 129 |
}; |
| 130 |
|
| 131 |
const WORK_LOCATION_HOME = { |
| 132 |
location_type: "home", |
| 133 |
location_name: "Home", |
| 134 |
work_location_id: 2, |
| 135 |
}; |
| 136 |
|
| 137 |
const WORK_LOCATION_OFFICE = { |
| 138 |
location_type: "office", |
| 139 |
location_name: "Office", |
| 140 |
work_location_id: 1, |
| 141 |
}; |
| 142 |
|
| 143 |
const EMPLOYEE_WORK_LOCATIONS = { |
| 144 |
[1]: { |
| 145 |
user_id: serverState.userId, |
| 146 |
employee_id: 1, |
| 147 |
partner_id: serverState.partnerId, |
| 148 |
employee_name: "Aaron", |
| 149 |
monday_location_id: WORK_LOCATION_OFFICE, |
| 150 |
tuesday_location_id: WORK_LOCATION_OFFICE, |
| 151 |
wednesday_location_id: WORK_LOCATION_HOME, |
| 152 |
thursday_location_id: WORK_LOCATION_FALSE, |
| 153 |
friday_location_id: WORK_LOCATION_FALSE, |
| 154 |
saturday_location_id: WORK_LOCATION_OFFICE, |
| 155 |
sunday_location_id: WORK_LOCATION_OFFICE, |
| 156 |
}, |
| 157 |
2: { |
| 158 |
user_id: 2, |
| 159 |
employee_id: 2, |
| 160 |
partner_id: 2, |
| 161 |
employee_name: "Brian", |
| 162 |
monday_location_id: WORK_LOCATION_HOME, |
| 163 |
tuesday_location_id: WORK_LOCATION_OFFICE, |
| 164 |
wednesday_location_id: WORK_LOCATION_HOME, |
| 165 |
thursday_location_id: WORK_LOCATION_HOME, |
| 166 |
friday_location_id: WORK_LOCATION_FALSE, |
| 167 |
saturday_location_id: WORK_LOCATION_OFFICE, |
| 168 |
sunday_location_id: WORK_LOCATION_HOME, |
| 169 |
}, |
| 170 |
}; |
| 171 |
|
| 172 |
function mountHomeWorkingView() { |
| 173 |
return mountView({ |
| 174 |
type: "calendar", |
| 175 |
resModel: "calendar.event", |
| 176 |
arch: ` |
| 177 |
<calendar js_class="attendee_calendar" event_open_popup="1" date_start="start" date_stop="stop" all_day="allday"> |
| 178 |
<field name="partner_ids" options="{'block': True, 'icon': 'fa fa-users'}" filters="1" write_model="calendar.filter" write_field="partner_id" filter_field="partner_checked" avatar_field="avatar_128"/> |
| 179 |
<field name="partner_id" string="Organizer" options="{'icon': 'fa fa-user-o'}"/> |
| 180 |
<field name="user_id"/> |
| 181 |
<field name="start"/> |
| 182 |
<field name="stop"/> |
| 183 |
<field name="allday"/> |
| 184 |
</calendar> |
| 185 |
`, |
| 186 |
}); |
| 187 |
} |
| 188 |
|
| 189 |
test(`basic rendering`, async () => { |
| 190 |
mockService("action", { |
| 191 |
async doAction(action, options) { |
| 192 |
expect.step([action, options?.additionalContext?.default_date]); |
| 193 |
}, |
| 194 |
}); |
| 195 |
onRpc("get_worklocation", () => ({ 1: EMPLOYEE_WORK_LOCATIONS[1] })); |
| 196 |
|
| 197 |
await mountHomeWorkingView(); |
| 198 |
await toggleFilter("partner_ids", "2"); |
| 199 |
|
| 200 |
const sundayDate = DateTime.fromISO("2020-12-06"); |
| 201 |
const saturdayDate = DateTime.fromISO("2020-12-12"); |
| 202 |
const intervals = Interval.fromDateTimes(sundayDate.startOf("day"), saturdayDate.endOf("day")).splitBy({ day: 1 }); |
| 203 |
const workLocations = intervals.map(({ start }) => { |
| 204 |
return queryFirst(`.fc-col-header-cell[data-date="${start.toISODate()}"] .o_worklocation_btn`); |
| 205 |
}); |
| 206 |
expect(queryAllTexts(workLocations)).toEqual(["Office", "", "", "Home", "Set Location", "Set Location", "Office"]); |
| 207 |
|
| 208 |
await contains(`.o_worklocation_text`, { root: workLocations[0] }).click(); |
| 209 |
expect(`.o_cw_popover div[name="employee_name"]`).toHaveText("Aaron"); |
| 210 |
expect(`.o_cw_popover .o_cw_popover_edit`).toHaveCount(1); |
| 211 |
expect(`.o_cw_popover .o_cw_popover_delete`).toHaveCount(1); |
| 212 |
|
| 213 |
await contains(`.o_cw_popover_close`).click(); |
| 214 |
await contains(`.o_worklocation_line`, { root: workLocations.at(-2), visible: false }).click(); |
| 215 |
expect.verifySteps([["hr_homeworking_calendar.set_location_wizard_action", "2020-12-11"]]); |
| 216 |
}); |
| 217 |
|
| 218 |
test(`multicalendar`, async () => { |
| 219 |
onRpc("get_worklocation", () => EMPLOYEE_WORK_LOCATIONS); |
| 220 |
await mountHomeWorkingView(); |
| 221 |
|
| 222 |
const sundayDate = DateTime.fromISO("2020-12-06"); |
| 223 |
const saturdayDate = DateTime.fromISO("2020-12-12"); |
| 224 |
const intervals = Interval.fromDateTimes(sundayDate.startOf("day"), saturdayDate.endOf("day")).splitBy({ day: 1 }); |
| 225 |
|
| 226 |
const dataSetsByDates = intervals.map(({ start }) => queryAllProperties(`.fc-col-header-cell[data-date="${start.toISODate()}"] .o_worklocation_btn .o_homeworking_content`, "dataset")); |
| 227 |
const locations = dataSetsByDates.flatMap((dataSets) => dataSets.length ? dataSets.map((ds) => ds.location) : [false]); |
| 228 |
expect(locations).toEqual([ |
| 229 |
"office", // sunday |
| 230 |
"home", // sunday |
| 231 |
"office", // monday |
| 232 |
"home", // monday |
| 233 |
"office", // tuesday |
| 234 |
"office", // tuesday |
| 235 |
"home", // wednesday |
| 236 |
"home", // wednesday |
| 237 |
"home", // thursday |
| 238 |
false, // friday |
| 239 |
"office", // saturday |
| 240 |
"office", // saturday |
| 241 |
]); |
| 242 |
expect(queryAll(`.fc-col-header-cell[data-date="2020-12-10"] .o_worklocation_text i.add_wl`, { visible: false })).toHaveCount(1); |
| 243 |
expect(queryAll(`.fc-col-header-cell[data-date="2020-12-12"] .o_worklocation_text i.add_wl`, { visible: false })).toHaveCount(0); |
| 244 |
|
| 245 |
await contains(`.fc-col-header-cell[data-date="2020-12-10"] .o_homework_content`).click(); |
| 246 |
expect(`.o_cw_popover div[name="employee_name"]`).toHaveText("Brian"); |
| 247 |
expect(`.o_cw_popover .o_cw_popover_edit`).toHaveCount(0); |
| 248 |
expect(`.o_cw_popover .o_cw_popover_delete`).toHaveCount(0); |
| 249 |
await contains(`.o_cw_popover_close`).click(); |
| 250 |
}); |
| 251 |
|
| 252 |
test(`test exceptions are correctly rendered`, async () => { |
| 253 |
mockService("action", { |
| 254 |
async doAction(action, options) { |
| 255 |
expect.step([action, options?.additionalContext?.default_date]); |
| 256 |
}, |
| 257 |
}); |
| 258 |
onRpc("get_worklocation", () => ({ |
| 259 |
1: { |
| 260 |
...EMPLOYEE_WORK_LOCATIONS[1], |
| 261 |
exceptions: { |
| 262 |
"2020-12-11": { |
| 263 |
hr_employee_location_id: 234, |
| 264 |
...WORK_LOCATION_HOME, |
| 265 |
}, |
| 266 |
}, |
| 267 |
}, |
| 268 |
})); |
| 269 |
|
| 270 |
await mountHomeWorkingView(); |
| 271 |
expect(`.fc-col-header-cell[data-date="2020-12-11"] .o_worklocation_btn`).toHaveText("Home"); |
| 272 |
|
| 273 |
await contains(`.fc-col-header-cell[data-date="2020-12-10"] .o_worklocation_text`, { visible: false }).click(); |
| 274 |
expect.verifySteps([["hr_homeworking_calendar.set_location_wizard_action", "2020-12-10"]]); |
| 275 |
}); |
| 276 |
|
| 277 |
test(`test exceptions are correctly rendered in multicalendar`, async () => { |
| 278 |
mockService("action", { |
| 279 |
async doAction(action, options) { |
| 280 |
expect.step([action, options?.additionalContext?.default_date]); |
| 281 |
}, |
| 282 |
}); |
| 283 |
onRpc("get_worklocation", () => ({ |
| 284 |
1: { |
| 285 |
...EMPLOYEE_WORK_LOCATIONS[1], |
| 286 |
exceptions: { |
| 287 |
"2020-12-10": { |
| 288 |
hr_employee_location_id: 3, |
| 289 |
...WORK_LOCATION_HOME, |
| 290 |
}, |
| 291 |
}, |
| 292 |
}, |
| 293 |
2: { |
| 294 |
...EMPLOYEE_WORK_LOCATIONS[2], |
| 295 |
exceptions: { |
| 296 |
"2020-12-11": { |
| 297 |
hr_employee_location_id: 4, |
| 298 |
...WORK_LOCATION_OFFICE, |
| 299 |
}, |
| 300 |
}, |
| 301 |
}, |
| 302 |
})); |
| 303 |
|
| 304 |
await mountHomeWorkingView(); |
| 305 |
expect(`.fc-col-header-cell[data-date="2020-12-11"] .o_homework_content`).toHaveCount(1); |
| 306 |
expect(`.fc-col-header-cell[data-date="2020-12-11"] .o_worklocation_btn`).toHaveText("Office"); |
| 307 |
expect(`.fc-col-header-cell[data-date="2020-12-10"] .o_homework_content`).toHaveCount(2); |
| 308 |
expect(`.fc-col-header-cell[data-date="2020-12-10"] .o_worklocation_btn`).toHaveText("Home"); |
| 309 |
expect(queryAll(`.fc-col-header-cell[data-date="2020-12-11"] .add_wl`, { visible: false })).toHaveCount(1); |
| 310 |
|
| 311 |
await contains(`.fc-col-header-cell[data-date="2020-12-11"] .add_wl`, { visible: false }).click(); |
| 312 |
expect.verifySteps([["hr_homeworking_calendar.set_location_wizard_action", "2020-12-11"]]); |
| 313 |
}); |