Debian Codegraph @

addons/web/static/lib/hoot/tests/ui/hoot_test_result.test.js

raw
1 /** @odoo-module */
2
3 import { describe, expect, makeExpect, test } from "@odoo/hoot";
4 import { mountForTest, parseUrl } from "../local_helpers";
5
6 import { animationFrame, click } from "@odoo/hoot-dom";
7 import { Component, xml } from "@odoo/owl";
8 import { Runner } from "../../core/runner";
9 import { Test } from "../../core/test";
10 import { HootTestResult } from "../../ui/hoot_test_result";
11 import { makeUiState } from "../../ui/setup_hoot_ui";
12
13 /**
14 * @param {(mockExpect: typeof expect) => any} callback
15 */
16 const mountTestResults = async (testFn, props) => {
17 const runner = new Runner();
18 const ui = makeUiState();
19 const mockTest = new Test(null, "test", {});
20 const [mockExpect, { after, before }] = makeExpect({});
21
22 class Parent extends Component {
23 static components = { HootTestResult };
24 static props = { test: Test, open: [Boolean, { value: "always" }] };
25 static template = xml`
26 <HootTestResult test="props.test" open="props.open">
27 Toggle
28 </HootTestResult>
29 `;
30
31 mockTest = mockTest;
32 }
33
34 before(mockTest);
35 testFn(mockExpect);
36 after(runner);
37
38 await mountForTest(Parent, {
39 env: { runner, ui },
40 props: {
41 test: mockTest,
42 open: "always",
43 ...props,
44 },
45 });
46
47 return mockTest;
48 };
49
50 const CLS_PASS = "text-emerald";
51 const CLS_FAIL = "text-rose";
52
53 describe(parseUrl(import.meta.url), () => {
54 test("test results: toBe and basic interactions", async () => {
55 const mockTest = await mountTestResults(
56 (mockExpect) => {
57 mockExpect(true).toBe(true);
58 mockExpect(true).toBe(false);
59 },
60 { open: false }
61 );
62
63 expect(".HootTestResult button:only").toHaveText("Toggle");
64 expect(".hoot-result-detail").not.toHaveCount();
65 expect(mockTest.lastResults.pass).toBe(false);
66
67 await click(".HootTestResult button");
68 await animationFrame();
69
70 expect(".hoot-result-detail").toHaveCount(1);
71
72 // First assertion: pass
73 expect(`.hoot-result-detail > .${CLS_PASS}`).toHaveText(
74 /received value is strictly equal to true/,
75 { inline: true }
76 );
77
78 // Second assertion: fail
79 expect(`.hoot-result-detail > .${CLS_FAIL}`).toHaveText(
80 /expected values to be strictly equal/,
81 { inline: true }
82 );
83 expect(`.hoot-info .${CLS_PASS}:contains(Expected)`).toHaveCount(1);
84 expect(`.hoot-info .${CLS_FAIL}:contains(Received)`).toHaveCount(1);
85 });
86 test("test results: toEqual", async () => {
87 await mountTestResults((mockExpect) => {
88 mockExpect([1, 2, { a: true }]).toEqual([1, 2, { a: true }]);
89 mockExpect([1, { a: false }, 3]).toEqual([1, { a: true }, 3]);
90 });
91
92 expect(".hoot-result-detail").toHaveCount(1);
93
94 // First assertion: pass
95 expect(`.hoot-result-detail > .${CLS_PASS}`).toHaveText(
96 /received value is deeply equal to \[1, 2, { a: true }\]/,
97 { inline: true }
98 );
99
100 // Second assertion: fail
101 expect(`.hoot-result-detail > .${CLS_FAIL}`).toHaveText(
102 /expected values to be deeply equal/,
103 { inline: true }
104 );
105 expect(`.hoot-info .${CLS_PASS}:contains(Expected)`).toHaveCount(1);
106 expect(`.hoot-info .${CLS_FAIL}:contains(Received)`).toHaveCount(1);
107 });
108
109 test("test results: toHaveCount", async () => {
110 await mountForTest(/* xml */ `
111 <span class="text" >abc</span>
112 <span class="text" >bcd</span>
113 `);
114 await mountTestResults((mockExpect) => {
115 mockExpect(".text").toHaveCount(2);
116 mockExpect(".text").toHaveCount(1);
117 });
118
119 expect(".hoot-result-detail").toHaveCount(1);
120
121 // First assertion: pass
122 expect(`.hoot-result-detail > .${CLS_PASS}`).toHaveText(
123 /found 2 elements matching ".text"/,
124 { inline: true }
125 );
126
127 // Second assertion: fail
128 expect(`.hoot-result-detail > .${CLS_FAIL}`).toHaveText(
129 /found 2 elements matching ".text"/,
130 { inline: true }
131 );
132 expect(`.hoot-info .${CLS_PASS}:contains(Expected)`).toHaveCount(1);
133 expect(`.hoot-info .${CLS_FAIL}:contains(Received)`).toHaveCount(1);
134 });
135
136 test("multiple test results: toHaveText", async () => {
137 await mountForTest(/* xml */ `
138 <span class="text" >abc</span>
139 <span class="text" >bcd</span>
140 `);
141 await mountTestResults((mockExpect) => {
142 mockExpect(".text:first").toHaveText("abc");
143 mockExpect(".text").toHaveText("abc");
144 mockExpect(".text").not.toHaveText("abc");
145 });
146
147 expect(".hoot-result-detail").toHaveCount(1);
148
149 // First assertion: pass
150 expect(`.hoot-result-detail > .${CLS_PASS}`).toHaveText(
151 /1 element matching ".text:first" has text "abc"/,
152 { inline: true }
153 );
154
155 // Second assertion: fail
156 expect(`.hoot-result-detail > .${CLS_FAIL}:eq(0)`).toHaveText(
157 /expected 2 elements matching ".text" to have the given text/,
158 { inline: true }
159 );
160 expect(".hoot-info:eq(0) .hoot-html").toHaveCount(2);
161 expect(".hoot-info:eq(0) .hoot-html").toHaveText("<span.text/>");
162 expect(`.hoot-info:eq(0) .${CLS_PASS}:contains(Received)`).toHaveCount(1);
163 expect(`.hoot-info:eq(0) .${CLS_PASS}:contains(Expected)`).toHaveCount(1);
164 expect(`.hoot-info:eq(0) .${CLS_FAIL}:contains(Received)`).toHaveCount(1);
165
166 // Third assertion: fail
167 expect(`.hoot-result-detail > .${CLS_FAIL}:eq(1)`).toHaveText(
168 /expected 2 elements matching ".text" not to have the given text/,
169 { inline: true }
170 );
171 expect(".hoot-info:eq(1) .hoot-html").toHaveCount(2);
172 expect(".hoot-info:eq(1) .hoot-html").toHaveText("<span.text/>");
173 expect(`.hoot-info:eq(1) .${CLS_PASS}:contains(Received)`).toHaveCount(1);
174 expect(`.hoot-info:eq(1) .${CLS_PASS}:contains(Expected)`).toHaveCount(1);
175 expect(`.hoot-info:eq(1) .${CLS_FAIL}:contains(Received)`).toHaveCount(1);
176 });
177 });