| 1 |
/** @odoo-module */ |
| 2 |
|
| 3 |
import { after, describe, expect, test } from "@odoo/hoot"; |
| 4 |
import { animationFrame, click, Deferred } from "@odoo/hoot-dom"; |
| 5 |
import { Component, reactive, useState, xml } from "@odoo/owl"; |
| 6 |
import { mountForTest, parseUrl } from "../local_helpers"; |
| 7 |
|
| 8 |
import { logger } from "../../core/logger"; |
| 9 |
import { HootTechnicalValue } from "../../ui/hoot_technical_value"; |
| 10 |
|
| 11 |
const mountTechnicalValue = async (defaultValue) => { |
| 12 |
const updateValue = async (value) => { |
| 13 |
state.value = value; |
| 14 |
await animationFrame(); |
| 15 |
}; |
| 16 |
|
| 17 |
const state = reactive({ value: defaultValue }); |
| 18 |
|
| 19 |
class TechnicalValueParent extends Component { |
| 20 |
static components = { HootTechnicalValue }; |
| 21 |
static props = {}; |
| 22 |
static template = xml`<HootTechnicalValue value="state.value" />`; |
| 23 |
|
| 24 |
setup() { |
| 25 |
this.state = useState(state); |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
await mountForTest(TechnicalValueParent); |
| 30 |
|
| 31 |
return updateValue; |
| 32 |
}; |
| 33 |
|
| 34 |
describe(parseUrl(import.meta.url), () => { |
| 35 |
test("technical value with primitive values", async () => { |
| 36 |
const updateValue = await mountTechnicalValue("oui"); |
| 37 |
expect(".hoot-string").toHaveText(`"oui"`); |
| 38 |
|
| 39 |
await updateValue(`"stringified"`); |
| 40 |
expect(".hoot-string").toHaveText(`'"stringified"'`); |
| 41 |
|
| 42 |
await updateValue(3); |
| 43 |
expect(".hoot-integer").toHaveText(`3`); |
| 44 |
|
| 45 |
await updateValue(undefined); |
| 46 |
expect(".hoot-undefined").toHaveText(`undefined`); |
| 47 |
|
| 48 |
await updateValue(null); |
| 49 |
expect(".hoot-null").toHaveText(`null`); |
| 50 |
}); |
| 51 |
|
| 52 |
test("technical value with objects", async () => { |
| 53 |
const logDebug = logger.debug; |
| 54 |
logger.debug = expect.step; |
| 55 |
after(() => (logger.debug = logDebug)); |
| 56 |
|
| 57 |
const updateValue = await mountTechnicalValue({}); |
| 58 |
expect(".hoot-technical").toHaveText(`Object(0)`); |
| 59 |
|
| 60 |
await updateValue([1, 2, "3"]); |
| 61 |
|
| 62 |
expect(".hoot-technical").toHaveText(`Array(3)`); |
| 63 |
expect.verifySteps([]); |
| 64 |
|
| 65 |
await click(".hoot-object"); |
| 66 |
await animationFrame(); |
| 67 |
|
| 68 |
expect(".hoot-technical").toHaveText(`Array(3)[\n1\n,\n2\n,\n"3"\n,\n]`); |
| 69 |
expect.verifySteps([[1, 2, "3"]]); |
| 70 |
|
| 71 |
await updateValue({ a: true }); |
| 72 |
expect(".hoot-technical").toHaveText(`Object(1)`); |
| 73 |
|
| 74 |
await click(".hoot-object"); |
| 75 |
await animationFrame(); |
| 76 |
|
| 77 |
expect(".hoot-technical").toHaveText(`Object(1){\na\n:\ntrue\n,\n}`); |
| 78 |
|
| 79 |
await updateValue({ |
| 80 |
a: true, |
| 81 |
sub: { |
| 82 |
key: "oui", |
| 83 |
}, |
| 84 |
}); |
| 85 |
expect(".hoot-technical").toHaveText(`Object(2)`); |
| 86 |
|
| 87 |
await click(".hoot-object:first"); |
| 88 |
await animationFrame(); |
| 89 |
|
| 90 |
expect(".hoot-technical:first").toHaveText( |
| 91 |
`Object(2){\na\n:\ntrue\n,\nsub\n:\nObject(1)\n}` |
| 92 |
); |
| 93 |
expect.verifySteps([]); |
| 94 |
|
| 95 |
await click(".hoot-object:last"); |
| 96 |
await animationFrame(); |
| 97 |
|
| 98 |
expect(".hoot-technical:first").toHaveText( |
| 99 |
`Object(2){\na\n:\ntrue\n,\nsub\n:\nObject(1){\nkey\n:\n"oui"\n,\n}\n}` |
| 100 |
); |
| 101 |
expect.verifySteps([{ key: "oui" }]); |
| 102 |
}); |
| 103 |
|
| 104 |
test("technical value with special cases", async () => { |
| 105 |
const updateValue = await mountTechnicalValue(new Date(0)); |
| 106 |
expect(".hoot-technical").toHaveText(`1970-01-01T00:00:00.000Z`); |
| 107 |
|
| 108 |
await updateValue(/ab[c]/gi); |
| 109 |
expect(".hoot-technical").toHaveText(`/ab[c]/gi`); |
| 110 |
|
| 111 |
const def = new Deferred(() => {}); |
| 112 |
await updateValue(def); |
| 113 |
expect(".hoot-technical").toHaveText(`Deferred<\npending\n>`); |
| 114 |
|
| 115 |
def.resolve("oui"); |
| 116 |
await animationFrame(); |
| 117 |
expect(".hoot-technical").toHaveText(`Deferred<\nfulfilled\n:\n"oui"\n>`); |
| 118 |
}); |
| 119 |
|
| 120 |
test("evaluation of unsafe value does not crash", async () => { |
| 121 |
const logDebug = logger.debug; |
| 122 |
logger.debug = () => expect.step("debug"); |
| 123 |
after(() => (logger.debug = logDebug)); |
| 124 |
|
| 125 |
class UnsafeString extends String { |
| 126 |
toString() { |
| 127 |
return this.valueOf(); |
| 128 |
} |
| 129 |
valueOf() { |
| 130 |
throw new Error("UNSAFE"); |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
await mountTechnicalValue(new UnsafeString("some value")); |
| 135 |
await click(".hoot-object"); |
| 136 |
|
| 137 |
expect(".hoot-object").toHaveText("UnsafeString(0)", { |
| 138 |
message: "size is 0 because it couldn't be evaluated", |
| 139 |
}); |
| 140 |
|
| 141 |
expect.verifySteps(["debug"]); |
| 142 |
}); |
| 143 |
}); |