| 1 |
import { describe, expect, test } from "@odoo/hoot"; |
| 2 |
import { FAKE_FIELDS } from "./calendar_test_helpers"; |
| 3 |
|
| 4 |
import { parseXML } from "@web/core/utils/xml"; |
| 5 |
import { CalendarArchParser } from "@web/views/calendar/calendar_arch_parser"; |
| 6 |
|
| 7 |
describe.current.tags("headless"); |
| 8 |
|
| 9 |
const parser = new CalendarArchParser(); |
| 10 |
const DEFAULT_ARCH_RESULTS = { |
| 11 |
aggregate: null, |
| 12 |
canCreate: true, |
| 13 |
canDelete: true, |
| 14 |
canEdit: true, |
| 15 |
eventLimit: 5, |
| 16 |
fieldMapping: { |
| 17 |
date_start: "start_date", |
| 18 |
}, |
| 19 |
fieldNames: ["start_date"], |
| 20 |
filtersInfo: {}, |
| 21 |
formViewId: false, |
| 22 |
hasEditDialog: false, |
| 23 |
quickCreate: true, |
| 24 |
quickCreateViewId: null, |
| 25 |
isDateHidden: false, |
| 26 |
isTimeHidden: false, |
| 27 |
monthOverflow: true, |
| 28 |
multiCreateView: null, |
| 29 |
popoverFieldNodes: {}, |
| 30 |
scale: "week", |
| 31 |
scales: ["day", "week", "month", "year"], |
| 32 |
showUnusualDays: false, |
| 33 |
showDatePicker: true, |
| 34 |
}; |
| 35 |
|
| 36 |
function parseArch(arch) { |
| 37 |
return parser.parse(parseXML(arch), { fake: { fields: FAKE_FIELDS } }, "fake"); |
| 38 |
} |
| 39 |
|
| 40 |
function parseWith(attrs) { |
| 41 |
const str = Object.entries(attrs) |
| 42 |
.map(([k, v]) => `${k}="${v}"`) |
| 43 |
.join(" "); |
| 44 |
return parseArch(`<calendar date_start="start_date" ${str}/>`); |
| 45 |
} |
| 46 |
|
| 47 |
test(`throw if date_start is not set`, () => { |
| 48 |
expect(() => parseArch(`<calendar/>`)).toThrow( |
| 49 |
`Calendar view must define "date_start" attribute.` |
| 50 |
); |
| 51 |
}); |
| 52 |
|
| 53 |
test(`defaults`, () => { |
| 54 |
expect(parseArch(`<calendar date_start="start_date"/>`)).toEqual(DEFAULT_ARCH_RESULTS); |
| 55 |
}); |
| 56 |
|
| 57 |
test("canCreate", () => { |
| 58 |
expect(parseWith({ create: "" }).canCreate).toBe(true); |
| 59 |
|
| 60 |
expect(parseWith({ create: "true" }).canCreate).toBe(true); |
| 61 |
expect(parseWith({ create: "True" }).canCreate).toBe(true); |
| 62 |
expect(parseWith({ create: "1" }).canCreate).toBe(true); |
| 63 |
|
| 64 |
expect(parseWith({ create: "false" }).canCreate).toBe(false); |
| 65 |
expect(parseWith({ create: "False" }).canCreate).toBe(false); |
| 66 |
expect(parseWith({ create: "0" }).canCreate).toBe(false); |
| 67 |
}); |
| 68 |
|
| 69 |
test("canDelete", () => { |
| 70 |
expect(parseWith({ delete: "" }).canDelete).toBe(true); |
| 71 |
|
| 72 |
expect(parseWith({ delete: "true" }).canDelete).toBe(true); |
| 73 |
expect(parseWith({ delete: "True" }).canDelete).toBe(true); |
| 74 |
expect(parseWith({ delete: "1" }).canDelete).toBe(true); |
| 75 |
|
| 76 |
expect(parseWith({ delete: "false" }).canDelete).toBe(false); |
| 77 |
expect(parseWith({ delete: "False" }).canDelete).toBe(false); |
| 78 |
expect(parseWith({ delete: "0" }).canDelete).toBe(false); |
| 79 |
}); |
| 80 |
|
| 81 |
test("canEdit", () => { |
| 82 |
expect(parseWith({ edit: "" }).canEdit).toBe(true); |
| 83 |
|
| 84 |
expect(parseWith({ edit: "true" }).canEdit).toBe(true); |
| 85 |
expect(parseWith({ edit: "True" }).canEdit).toBe(true); |
| 86 |
expect(parseWith({ edit: "1" }).canEdit).toBe(true); |
| 87 |
|
| 88 |
expect(parseWith({ edit: "false" }).canEdit).toBe(false); |
| 89 |
expect(parseWith({ edit: "False" }).canEdit).toBe(false); |
| 90 |
expect(parseWith({ edit: "0" }).canEdit).toBe(false); |
| 91 |
}); |
| 92 |
|
| 93 |
test("eventLimit", () => { |
| 94 |
expect(parseWith({ event_limit: "2" }).eventLimit).toBe(2); |
| 95 |
expect(parseWith({ event_limit: "5" }).eventLimit).toBe(5); |
| 96 |
|
| 97 |
expect(() => parseWith({ event_limit: "five" })).toThrow(); |
| 98 |
expect(() => parseWith({ event_limit: "" })).toThrow(); |
| 99 |
}); |
| 100 |
|
| 101 |
test("hasEditDialog", () => { |
| 102 |
expect(parseWith({ event_open_popup: "" }).hasEditDialog).toBe(false); |
| 103 |
|
| 104 |
expect(parseWith({ event_open_popup: "true" }).hasEditDialog).toBe(true); |
| 105 |
expect(parseWith({ event_open_popup: "True" }).hasEditDialog).toBe(true); |
| 106 |
expect(parseWith({ event_open_popup: "1" }).hasEditDialog).toBe(true); |
| 107 |
|
| 108 |
expect(parseWith({ event_open_popup: "false" }).hasEditDialog).toBe(false); |
| 109 |
expect(parseWith({ event_open_popup: "False" }).hasEditDialog).toBe(false); |
| 110 |
expect(parseWith({ event_open_popup: "0" }).hasEditDialog).toBe(false); |
| 111 |
}); |
| 112 |
|
| 113 |
test("quickCreate", () => { |
| 114 |
expect(parseWith({ quick_create: "" }).quickCreate).toBe(true); |
| 115 |
|
| 116 |
expect(parseWith({ quick_create: "true" }).quickCreate).toBe(true); |
| 117 |
expect(parseWith({ quick_create: "True" }).quickCreate).toBe(true); |
| 118 |
expect(parseWith({ quick_create: "1" }).quickCreate).toBe(true); |
| 119 |
|
| 120 |
expect(parseWith({ quick_create: "false" }).quickCreate).toBe(false); |
| 121 |
expect(parseWith({ quick_create: "False" }).quickCreate).toBe(false); |
| 122 |
expect(parseWith({ quick_create: "0" }).quickCreate).toBe(false); |
| 123 |
|
| 124 |
expect(parseWith({ quick_create: "12" }).quickCreate).toBe(true); |
| 125 |
}); |
| 126 |
|
| 127 |
test("quickCreateViewId", () => { |
| 128 |
expect(parseWith({ quick_create: "0", quick_create_view_id: "12" })).toEqual({ |
| 129 |
...DEFAULT_ARCH_RESULTS, |
| 130 |
quickCreate: false, |
| 131 |
quickCreateViewId: null, |
| 132 |
}); |
| 133 |
expect(parseWith({ quick_create: "1", quick_create_view_id: "12" })).toEqual({ |
| 134 |
...DEFAULT_ARCH_RESULTS, |
| 135 |
quickCreate: true, |
| 136 |
quickCreateViewId: 12, |
| 137 |
}); |
| 138 |
expect(parseWith({ quick_create: "1" })).toEqual({ |
| 139 |
...DEFAULT_ARCH_RESULTS, |
| 140 |
quickCreate: true, |
| 141 |
quickCreateViewId: null, |
| 142 |
}); |
| 143 |
}); |
| 144 |
|
| 145 |
test("isDateHidden", () => { |
| 146 |
expect(parseWith({ hide_date: "" }).isDateHidden).toBe(false); |
| 147 |
|
| 148 |
expect(parseWith({ hide_date: "true" }).isDateHidden).toBe(true); |
| 149 |
expect(parseWith({ hide_date: "True" }).isDateHidden).toBe(true); |
| 150 |
expect(parseWith({ hide_date: "1" }).isDateHidden).toBe(true); |
| 151 |
|
| 152 |
expect(parseWith({ hide_date: "false" }).isDateHidden).toBe(false); |
| 153 |
expect(parseWith({ hide_date: "False" }).isDateHidden).toBe(false); |
| 154 |
expect(parseWith({ hide_date: "0" }).isDateHidden).toBe(false); |
| 155 |
}); |
| 156 |
|
| 157 |
test("isTimeHidden", () => { |
| 158 |
expect(parseWith({ hide_time: "" }).isTimeHidden).toBe(false); |
| 159 |
|
| 160 |
expect(parseWith({ hide_time: "true" }).isTimeHidden).toBe(true); |
| 161 |
expect(parseWith({ hide_time: "True" }).isTimeHidden).toBe(true); |
| 162 |
expect(parseWith({ hide_time: "1" }).isTimeHidden).toBe(true); |
| 163 |
|
| 164 |
expect(parseWith({ hide_time: "false" }).isTimeHidden).toBe(false); |
| 165 |
expect(parseWith({ hide_time: "False" }).isTimeHidden).toBe(false); |
| 166 |
expect(parseWith({ hide_time: "0" }).isTimeHidden).toBe(false); |
| 167 |
}); |
| 168 |
|
| 169 |
test("scale", () => { |
| 170 |
expect(parseWith({ mode: "day" }).scale).toBe("day"); |
| 171 |
expect(parseWith({ mode: "week" }).scale).toBe("week"); |
| 172 |
expect(parseWith({ mode: "month" }).scale).toBe("month"); |
| 173 |
expect(parseWith({ mode: "year" }).scale).toBe("year"); |
| 174 |
|
| 175 |
expect(() => parseWith({ mode: "" })).toThrow(`Calendar view cannot display mode: `); |
| 176 |
expect(() => parseWith({ mode: "other" })).toThrow(`Calendar view cannot display mode: other`); |
| 177 |
}); |
| 178 |
|
| 179 |
test("scales", () => { |
| 180 |
expect(parseWith({ scales: "" }).scales).toEqual(["day", "week", "month", "year"]); |
| 181 |
expect(parseWith({ scales: "day" }).scales).toEqual(["day"]); |
| 182 |
expect(parseWith({ scales: "day,week" }).scales).toEqual(["day", "week"]); |
| 183 |
expect(parseWith({ scales: "day,week,month" }).scales).toEqual(["day", "week", "month"]); |
| 184 |
expect(parseWith({ scales: "day,week,month,year" }).scales).toEqual([ |
| 185 |
"day", |
| 186 |
"week", |
| 187 |
"month", |
| 188 |
"year", |
| 189 |
]); |
| 190 |
expect(parseWith({ scales: "week" }).scales).toEqual(["week"]); |
| 191 |
expect(parseWith({ scales: "week,month" }).scales).toEqual(["week", "month"]); |
| 192 |
expect(parseWith({ scales: "week,month,year" }).scales).toEqual(["week", "month", "year"]); |
| 193 |
expect(parseWith({ scales: "month" }).scales).toEqual(["month"]); |
| 194 |
expect(parseWith({ scales: "month,year" }).scales).toEqual(["month", "year"]); |
| 195 |
expect(parseWith({ scales: "year" }).scales).toEqual(["year"]); |
| 196 |
expect(parseWith({ scales: "year,day,month,week" }).scales).toEqual([ |
| 197 |
"year", |
| 198 |
"day", |
| 199 |
"month", |
| 200 |
"week", |
| 201 |
]); |
| 202 |
|
| 203 |
expect(() => |
| 204 |
parseArch(`<calendar date_start="start_date" scales="month" mode="day"/>`) |
| 205 |
).toThrow(); |
| 206 |
}); |
| 207 |
|
| 208 |
test("showUnusualDays", () => { |
| 209 |
expect(parseWith({ show_unusual_days: "" }).showUnusualDays).toBe(false); |
| 210 |
|
| 211 |
expect(parseWith({ show_unusual_days: "true" }).showUnusualDays).toBe(true); |
| 212 |
expect(parseWith({ show_unusual_days: "True" }).showUnusualDays).toBe(true); |
| 213 |
expect(parseWith({ show_unusual_days: "1" }).showUnusualDays).toBe(true); |
| 214 |
|
| 215 |
expect(parseWith({ show_unusual_days: "false" }).showUnusualDays).toBe(false); |
| 216 |
expect(parseWith({ show_unusual_days: "False" }).showUnusualDays).toBe(false); |
| 217 |
expect(parseWith({ show_unusual_days: "0" }).showUnusualDays).toBe(false); |
| 218 |
}); |