/** @odoo-module */ import { Component, onWillRender, useState, xml } from "@odoo/owl"; import { isFirefox } from "../../hoot-dom/hoot_dom_utils"; import { Tag } from "../core/tag"; import { Test } from "../core/test"; import { subscribeToURLParams } from "../core/url"; import { CASE_EVENT_TYPES, formatHumanReadable, formatTime, getTypeOf, isLabel, Markup, ordinal, } from "../hoot_utils"; import { HootCopyButton } from "./hoot_copy_button"; import { HootLink } from "./hoot_link"; import { HootTechnicalValue } from "./hoot_technical_value"; /** * @typedef {import("../core/expect").CaseEvent} CaseEvent * @typedef {import("../core/expect").CaseEventType} CaseEventType * @typedef {import("../core/expect").CaseResult} CaseResult * @typedef {import("./setup_hoot_ui").StatusFilter} StatusFilter */ //----------------------------------------------------------------------------- // Global //----------------------------------------------------------------------------- const { Boolean, Object: { entries: $entries, fromEntries: $fromEntries }, } = globalThis; //----------------------------------------------------------------------------- // Internal //----------------------------------------------------------------------------- /** * @param {[number, CaseEvent][]} indexedResults * @param {number} events */ function filterEvents(indexedResults, events) { /** @type {Record} */ const filteredEvents = {}; for (const [i, result] of indexedResults) { filteredEvents[i] = result.getEvents(events); } return filteredEvents; } /** * @param {CaseEvent[]} results * @param {StatusFilter} statusFilter */ function filterResults(results, statusFilter) { const ordinalResults = []; const hasFailed = results.some((r) => !r.pass); const shouldPass = statusFilter === "passed"; for (let i = 0; i < results.length; i++) { if (!hasFailed || results[i].pass === shouldPass) { ordinalResults.push([i + 1, results[i]]); } } return ordinalResults; } /** * @param {string} label * @param {string} owner */ function stackTemplate(label, owner) { // Defined with string concat because line returns are taken into account in
 tags.
    const preContent =
        /* xml */ `` +
        /* xml */ `` +
        /* xml */ `` +
        /* xml */ ``;
    return /* xml */ `
        
            
${label}:
${preContent}
`; } const DOC_URL = `https://www.odoo.com/documentation/18.0/developer/reference/frontend/unit_testing/hoot.html#`; const ERROR_TEMPLATE = /* xml */ `
${stackTemplate("Source", "event")} ${stackTemplate("Cause", "event.cause")} `; const EVENT_TEMPLATE = /* xml */ `
.
${stackTemplate("Source", "event")}
`; const CASE_EVENT_TYPES_INVERSE = $fromEntries( $entries(CASE_EVENT_TYPES).map(([k, v]) => [v.value, k]) ); const R_STACK_LINE_START = isFirefox() ? /^\s*(?@)(?.*)/i : /^\s*(?at)(?.*)/i; //----------------------------------------------------------------------------- // Exports //----------------------------------------------------------------------------- /** * @typedef {{ * open?: boolean | "always"; * slots: any; * test: Test; * }} TestResultProps */ /** @extends {Component} */ export class HootTestResult extends Component { static components = { HootCopyButton, HootLink, HootTechnicalValue }; static props = { open: [{ type: Boolean }, { value: "always" }], slots: { type: Object, shape: { default: Object, }, }, test: Test, }; static template = xml`
run:
No test event to show ${ERROR_TEMPLATE} ${EVENT_TEMPLATE}
`; CASE_EVENT_TYPES = CASE_EVENT_TYPES; DOC_URL = DOC_URL; Tag = Tag; formatHumanReadable = formatHumanReadable; formatTime = formatTime; getTypeOf = getTypeOf; isLabel = isLabel; isMarkup = Markup.isMarkup; ordinal = ordinal; /** @type {ReturnType} */ filteredEvents; /** @type {[number, CaseEvent][]} */ filteredResults; setup() { subscribeToURLParams("*"); const { runner, ui } = this.env; this.config = useState(runner.config); this.logs = useState(this.props.test.logs); this.results = useState(this.props.test.results); this.state = useState({ showCode: false, showDetails: Boolean(this.props.open), }); this.uiState = useState(ui); onWillRender(this.onWillRender.bind(this)); } getClassName() { if (this.logs.error) { return "bg-rose-900"; } switch (this.props.test.status) { case Test.ABORTED: { return "bg-amber-900"; } case Test.FAILED: { if (this.props.test.config.todo) { return "bg-purple-900"; } else { return "bg-rose-900"; } } case Test.PASSED: { if (this.logs.warn) { return "bg-amber-900"; } else if (this.props.test.config.todo) { return "bg-purple-900"; } else { return "bg-emerald-900"; } } default: { return "bg-cyan-900"; } } } /** * @param {number} nType */ getTypeName(nType) { return CASE_EVENT_TYPES_INVERSE[nType]; } onWillRender() { this.filteredResults = filterResults(this.results, this.uiState.statusFilter); this.filteredEvents = filterEvents(this.filteredResults, this.config.events); } /** * @param {string} stack */ parseStack(stack) { const result = []; for (const line of stack.split("\n")) { const match = line.match(R_STACK_LINE_START); if (match) { result.push( { className: "text-rose", value: match.groups.prefix }, match.groups.rest + "\n" ); } else { result.push(line + "\n"); } } return result; } toggleCode() { this.state.showCode = !this.state.showCode; } toggleDetails() { if (this.props.open === "always") { return; } this.state.showDetails = !this.state.showDetails; } }