|
| 1 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
| 2 | +import "../../../../src/panels/lovelace/cards/hui-updates-card"; |
| 3 | +import type { HuiUpdatesCard } from "../../../../src/panels/lovelace/cards/hui-updates-card"; |
| 4 | +import type { UpdatesCardConfig } from "../../../../src/panels/lovelace/cards/types"; |
| 5 | +import type { HomeAssistant } from "../../../../src/types"; |
| 6 | + |
| 7 | +const { filterUpdateEntities } = vi.hoisted(() => ({ |
| 8 | + filterUpdateEntities: vi.fn(() => []), |
| 9 | +})); |
| 10 | + |
| 11 | +// Bundler-defined globals the card's import graph reads at eval time. |
| 12 | +vi.hoisted(() => { |
| 13 | + Object.assign(globalThis, { |
| 14 | + __STATIC_PATH__: "/", |
| 15 | + __HASS_URL__: "", |
| 16 | + __BUILD__: "modern", |
| 17 | + __VERSION__: "test", |
| 18 | + __BACKWARDS_COMPAT__: false, |
| 19 | + __SUPERVISOR__: false, |
| 20 | + __NAMESPACE__: "frontend", |
| 21 | + }); |
| 22 | +}); |
| 23 | + |
| 24 | +vi.mock("../../../../src/data/update", () => ({ |
| 25 | + filterUpdateEntities, |
| 26 | + updateCanInstall: vi.fn(() => true), |
| 27 | +})); |
| 28 | + |
| 29 | +const createHass = (): HomeAssistant => |
| 30 | + ({ |
| 31 | + states: {}, |
| 32 | + locale: { |
| 33 | + language: "en", |
| 34 | + }, |
| 35 | + user: { |
| 36 | + is_admin: true, |
| 37 | + }, |
| 38 | + localize: vi.fn((key: string) => key), |
| 39 | + }) as unknown as HomeAssistant; |
| 40 | + |
| 41 | +let elements: HuiUpdatesCard[] = []; |
| 42 | + |
| 43 | +const mount = async (): Promise<HuiUpdatesCard> => { |
| 44 | + const el = document.createElement("hui-updates-card") as HuiUpdatesCard; |
| 45 | + el.hass = createHass(); |
| 46 | + el.setConfig({ |
| 47 | + type: "updates", |
| 48 | + hide_empty: false, |
| 49 | + } as UpdatesCardConfig); |
| 50 | + |
| 51 | + document.body.appendChild(el); |
| 52 | + elements.push(el); |
| 53 | + await el.updateComplete; |
| 54 | + |
| 55 | + return el; |
| 56 | +}; |
| 57 | + |
| 58 | +afterEach(() => { |
| 59 | + elements.forEach((el) => el.remove()); |
| 60 | + elements = []; |
| 61 | + vi.restoreAllMocks(); |
| 62 | +}); |
| 63 | + |
| 64 | +describe("hui-updates-card", () => { |
| 65 | + // willUpdate() and render() previously each called _getUpdateEntities() |
| 66 | + // independently; this pins the count to one call per update cycle so a |
| 67 | + // future edit can't silently reintroduce the duplicate. |
| 68 | + it("computes update entities once per update cycle", async () => { |
| 69 | + const element = await mount(); |
| 70 | + |
| 71 | + filterUpdateEntities.mockClear(); |
| 72 | + |
| 73 | + element.hass = { |
| 74 | + ...element.hass!, |
| 75 | + states: { ...element.hass!.states }, |
| 76 | + }; |
| 77 | + |
| 78 | + await element.updateComplete; |
| 79 | + |
| 80 | + expect(filterUpdateEntities).toHaveBeenCalledTimes(1); |
| 81 | + }); |
| 82 | +}); |
0 commit comments