| 1 |
import { beforeEach, describe, expect, test } from "@odoo/hoot"; |
| 2 |
import { allowTranslations, patchWithCleanup } from "@web/../tests/web_test_helpers"; |
| 3 |
|
| 4 |
import { markup } from "@odoo/owl"; |
| 5 |
import { currencies } from "@web/core/currency"; |
| 6 |
import { localization } from "@web/core/l10n/localization"; |
| 7 |
import { |
| 8 |
formatFloat, |
| 9 |
formatFloatFactor, |
| 10 |
formatFloatTime, |
| 11 |
formatJson, |
| 12 |
formatInteger, |
| 13 |
formatMany2one, |
| 14 |
formatMany2oneReference, |
| 15 |
formatMonetary, |
| 16 |
formatPercentage, |
| 17 |
formatReference, |
| 18 |
formatText, |
| 19 |
formatX2many, |
| 20 |
formatDate, |
| 21 |
formatDateTime, |
| 22 |
} from "@web/views/fields/formatters"; |
| 23 |
|
| 24 |
const { DateTime } = luxon; |
| 25 |
|
| 26 |
describe.current.tags("headless"); |
| 27 |
|
| 28 |
beforeEach(() => { |
| 29 |
allowTranslations(); |
| 30 |
patchWithCleanup(localization, { |
| 31 |
dateTimeFormat: "MM/dd/yyyy HH:mm:ss", |
| 32 |
dateFormat: "MM/dd/yyyy", |
| 33 |
decimalPoint: ".", |
| 34 |
thousandsSep: ",", |
| 35 |
grouping: [3, 0], |
| 36 |
}); |
| 37 |
}); |
| 38 |
|
| 39 |
test("formatFloat", () => { |
| 40 |
expect(formatFloat(false)).toBe(""); |
| 41 |
expect(formatFloat(200)).toBe("200.00"); |
| 42 |
expect(formatFloat(200, { trailingZeros: false })).toBe("200"); |
| 43 |
}); |
| 44 |
|
| 45 |
test("formatFloatFactor", () => { |
| 46 |
expect(formatFloatFactor(false)).toBe(""); |
| 47 |
expect(formatFloatFactor(6000)).toBe("6,000.00"); |
| 48 |
expect(formatFloatFactor(6000, { factor: 3 })).toBe("18,000.00"); |
| 49 |
expect(formatFloatFactor(6000, { factor: 0.5 })).toBe("3,000.00"); |
| 50 |
}); |
| 51 |
|
| 52 |
test("formatFloatTime", () => { |
| 53 |
expect(formatFloatTime(2)).toBe("02:00"); |
| 54 |
expect(formatFloatTime(3.5)).toBe("03:30"); |
| 55 |
expect(formatFloatTime(0.25)).toBe("00:15"); |
| 56 |
expect(formatFloatTime(0.58)).toBe("00:35"); |
| 57 |
expect(formatFloatTime(2 / 60, { displaySeconds: true })).toBe("00:02:00"); |
| 58 |
expect(formatFloatTime(2 / 60 + 1 / 3600, { displaySeconds: true })).toBe("00:02:01"); |
| 59 |
expect(formatFloatTime(2 / 60 + 2 / 3600, { displaySeconds: true })).toBe("00:02:02"); |
| 60 |
expect(formatFloatTime(2 / 60 + 3 / 3600, { displaySeconds: true })).toBe("00:02:03"); |
| 61 |
expect(formatFloatTime(0.25, { displaySeconds: true })).toBe("00:15:00"); |
| 62 |
expect(formatFloatTime(0.25 + 15 / 3600, { displaySeconds: true })).toBe("00:15:15"); |
| 63 |
expect(formatFloatTime(0.25 + 45 / 3600, { displaySeconds: true })).toBe("00:15:45"); |
| 64 |
expect(formatFloatTime(56 / 3600, { displaySeconds: true })).toBe("00:00:56"); |
| 65 |
expect(formatFloatTime(-0.5)).toBe("-00:30"); |
| 66 |
|
| 67 |
const options = { noLeadingZeroHour: true }; |
| 68 |
expect(formatFloatTime(2, options)).toBe("2:00"); |
| 69 |
expect(formatFloatTime(3.5, options)).toBe("3:30"); |
| 70 |
expect(formatFloatTime(3.5, { ...options, displaySeconds: true })).toBe("3:30:00"); |
| 71 |
expect(formatFloatTime(3.5 + 15 / 3600, { ...options, displaySeconds: true })).toBe("3:30:15"); |
| 72 |
expect(formatFloatTime(3.5 + 45 / 3600, { ...options, displaySeconds: true })).toBe("3:30:45"); |
| 73 |
expect(formatFloatTime(56 / 3600, { ...options, displaySeconds: true })).toBe("0:00:56"); |
| 74 |
expect(formatFloatTime(-0.5, options)).toBe("-0:30"); |
| 75 |
}); |
| 76 |
|
| 77 |
test("formatJson", () => { |
| 78 |
expect(formatJson(false)).toBe(""); |
| 79 |
expect(formatJson({})).toBe("{}"); |
| 80 |
expect(formatJson({ 1: 111 })).toBe('{"1":111}'); |
| 81 |
expect(formatJson({ 9: 11, 666: 42 })).toBe('{"9":11,"666":42}'); |
| 82 |
}); |
| 83 |
|
| 84 |
test("formatInteger", () => { |
| 85 |
expect(formatInteger(false)).toBe(""); |
| 86 |
expect(formatInteger(0)).toBe("0"); |
| 87 |
|
| 88 |
patchWithCleanup(localization, { grouping: [3, 3, 3, 3] }); |
| 89 |
expect(formatInteger(1000000)).toBe("1,000,000"); |
| 90 |
|
| 91 |
patchWithCleanup(localization, { grouping: [3, 2, -1] }); |
| 92 |
expect(formatInteger(106500)).toBe("1,06,500"); |
| 93 |
|
| 94 |
patchWithCleanup(localization, { grouping: [1, 2, -1] }); |
| 95 |
expect(formatInteger(106500)).toBe("106,50,0"); |
| 96 |
|
| 97 |
const options = { grouping: [2, 0], thousandsSep: "€" }; |
| 98 |
expect(formatInteger(6000, options)).toBe("60€00"); |
| 99 |
}); |
| 100 |
|
| 101 |
test("formatMany2one", () => { |
| 102 |
expect(formatMany2one(false)).toBe(""); |
| 103 |
expect(formatMany2one([false, "M2O value"])).toBe("M2O value"); |
| 104 |
expect(formatMany2one([1, false])).toBe("Unnamed"); |
| 105 |
expect(formatMany2one([1, "M2O value"])).toBe("M2O value"); |
| 106 |
expect(formatMany2one([1, "M2O value"], { escape: true })).toBe("M2O%20value"); |
| 107 |
expect(formatMany2one({ id: false, display_name: "M2O value" })).toBe("M2O value"); |
| 108 |
expect(formatMany2one({ id: 1, display_name: false })).toBe("Unnamed"); |
| 109 |
expect(formatMany2one({ id: 1, display_name: "M2O value" })).toBe("M2O value"); |
| 110 |
expect(formatMany2one({ id: 1, display_name: "M2O value" }, { escape: true })).toBe( |
| 111 |
"M2O%20value" |
| 112 |
); |
| 113 |
}); |
| 114 |
|
| 115 |
test("formatText", () => { |
| 116 |
expect(formatText(false)).toBe(""); |
| 117 |
expect(formatText("value")).toBe("value"); |
| 118 |
expect(formatText(1)).toBe("1"); |
| 119 |
expect(formatText(1.5)).toBe("1.5"); |
| 120 |
expect(formatText(markup`<p>This is a Test</p>`)).toBe("<p>This is a Test</p>"); |
| 121 |
expect(formatText([1, 2, 3, 4, 5])).toBe("1,2,3,4,5"); |
| 122 |
expect(formatText({ a: 1, b: 2 })).toBe("[object Object]"); |
| 123 |
}); |
| 124 |
|
| 125 |
test("formatX2many", () => { |
| 126 |
// Results are cast as strings since they're lazy translated. |
| 127 |
expect(String(formatX2many({ currentIds: [] }))).toBe("No records"); |
| 128 |
expect(String(formatX2many({ currentIds: [1] }))).toBe("1 record"); |
| 129 |
expect(String(formatX2many({ currentIds: [1, 3] }))).toBe("2 records"); |
| 130 |
}); |
| 131 |
|
| 132 |
test("formatMonetary", () => { |
| 133 |
patchWithCleanup(currencies, { |
| 134 |
10: { |
| 135 |
digits: [69, 2], |
| 136 |
position: "after", |
| 137 |
symbol: "€", |
| 138 |
}, |
| 139 |
11: { |
| 140 |
digits: [69, 2], |
| 141 |
position: "before", |
| 142 |
symbol: "$", |
| 143 |
}, |
| 144 |
12: { |
| 145 |
digits: [69, 2], |
| 146 |
position: "after", |
| 147 |
symbol: "&", |
| 148 |
}, |
| 149 |
}); |
| 150 |
|
| 151 |
expect(formatMonetary(false)).toBe(""); |
| 152 |
|
| 153 |
const field = { |
| 154 |
type: "monetary", |
| 155 |
currency_field: "c_x", |
| 156 |
}; |
| 157 |
let data = { |
| 158 |
c_x: [11], |
| 159 |
c_y: 12, |
| 160 |
}; |
| 161 |
expect(formatMonetary(200, { field, currencyId: 10, data })).toBe("200.00\u00a0€"); |
| 162 |
expect(formatMonetary(200, { field, currencyId: 10, data, trailingZeros: false })).toBe( |
| 163 |
"200\u00a0€" |
| 164 |
); |
| 165 |
expect(formatMonetary(200, { field, data })).toBe("$\u00a0200.00"); |
| 166 |
expect(formatMonetary(200, { field, currencyField: "c_y", data })).toBe("200.00\u00a0&"); |
| 167 |
|
| 168 |
const floatField = { type: "float" }; |
| 169 |
data = { |
| 170 |
currency_id: [11], |
| 171 |
}; |
| 172 |
expect(formatMonetary(200, { field: floatField, data })).toBe("$\u00a0200.00"); |
| 173 |
}); |
| 174 |
|
| 175 |
test("formatPercentage", () => { |
| 176 |
expect(formatPercentage(false)).toBe("0%"); |
| 177 |
expect(formatPercentage(0)).toBe("0%"); |
| 178 |
expect(formatPercentage(0.5)).toBe("50%"); |
| 179 |
|
| 180 |
expect(formatPercentage(1)).toBe("100%"); |
| 181 |
|
| 182 |
expect(formatPercentage(-0.2)).toBe("-20%"); |
| 183 |
expect(formatPercentage(2.5)).toBe("250%"); |
| 184 |
|
| 185 |
expect(formatPercentage(0.125)).toBe("12.5%"); |
| 186 |
expect(formatPercentage(0.666666)).toBe("66.67%"); |
| 187 |
expect(formatPercentage(125)).toBe("12500%"); |
| 188 |
|
| 189 |
expect(formatPercentage(50, { humanReadable: true })).toBe("5k%"); |
| 190 |
expect(formatPercentage(0.5, { noSymbol: true })).toBe("50"); |
| 191 |
|
| 192 |
patchWithCleanup(localization, { grouping: [3, 0], decimalPoint: ",", thousandsSep: "." }); |
| 193 |
expect(formatPercentage(0.125)).toBe("12,5%"); |
| 194 |
expect(formatPercentage(0.666666)).toBe("66,67%"); |
| 195 |
}); |
| 196 |
|
| 197 |
test("formatReference", () => { |
| 198 |
expect(formatReference(false)).toBe(""); |
| 199 |
const value = { resModel: "product", resId: 2, displayName: "Chair" }; |
| 200 |
expect(formatReference(value)).toBe("Chair"); |
| 201 |
}); |
| 202 |
|
| 203 |
test("formatMany2oneReference", () => { |
| 204 |
expect(formatMany2oneReference(false)).toBe(""); |
| 205 |
expect(formatMany2oneReference({ resId: 9, displayName: "Chair" })).toBe("Chair"); |
| 206 |
}); |
| 207 |
|
| 208 |
test("formatDate", () => { |
| 209 |
expect(formatDate(false)).toBe(""); |
| 210 |
expect(formatDate(DateTime.fromObject({ day: 22, month: 1, year: 1990 }))).toBe("Jan 22, 1990"); |
| 211 |
expect( |
| 212 |
formatDate(DateTime.fromObject({ day: 22, month: 1, year: 1990 }), { numeric: true }) |
| 213 |
).toBe("01/22/1990"); |
| 214 |
expect(formatDate(DateTime.fromObject({ day: 22, month: 1 }))).toBe("Jan 22"); |
| 215 |
}); |
| 216 |
|
| 217 |
test("formatDateTime", () => { |
| 218 |
const datetime = DateTime.fromObject({ |
| 219 |
day: 22, |
| 220 |
month: 1, |
| 221 |
year: 1990, |
| 222 |
hour: 10, |
| 223 |
minute: 30, |
| 224 |
second: 45, |
| 225 |
}); |
| 226 |
expect(formatDateTime(false)).toBe(""); |
| 227 |
expect(formatDateTime(datetime)).toBe("Jan 22, 1990, 10:30 AM"); |
| 228 |
expect(formatDateTime(datetime, { showDate: false })).toBe("10:30 AM"); |
| 229 |
expect(formatDateTime(datetime, { showSeconds: true })).toBe("Jan 22, 1990, 10:30:45 AM"); |
| 230 |
expect(formatDateTime(datetime, { showTime: false })).toBe("Jan 22, 1990"); |
| 231 |
expect(formatDateTime(datetime, { numeric: true })).toBe("01/22/1990 10:30:45"); |
| 232 |
expect(formatDateTime(DateTime.fromObject({ day: 22, month: 1, hour: 10, minute: 30 }))).toBe( |
| 233 |
"Jan 22, 10:30 AM" |
| 234 |
); |
| 235 |
}); |