diff --git a/ui/src/tool/__snapshots__/entry.test.ts.snap b/ui/src/tool/__snapshots__/entry.test.ts.snap index 2a7d83e2af..7e53e21c42 100644 --- a/ui/src/tool/__snapshots__/entry.test.ts.snap +++ b/ui/src/tool/__snapshots__/entry.test.ts.snap @@ -10130,6 +10130,14 @@ Address: 0xb0 Type: data" `; +exports[`entry > unknownImpl > toString includes size and unknown part description 1`] = ` +"Size: 1 KB + +The unknown part in the binary. +Can be ELF Header, Program Header, align offset... +We just don't know." +`; + exports[`entry match 1`] = `"bin-linux-1.21-amd64"`; exports[`entry match 2`] = `"result"`; diff --git a/ui/src/tool/entry.test.ts b/ui/src/tool/entry.test.ts index db3320bb65..2820855ab7 100644 --- a/ui/src/tool/entry.test.ts +++ b/ui/src/tool/entry.test.ts @@ -3,7 +3,7 @@ import path from "node:path"; import { assert, describe, expect, expectTypeOf, it } from "vitest"; import { parseResult } from "../generated/schema.ts"; import type { EntryChildren, EntryLike, EntryType } from "./entry.ts"; -import { BaseImpl, DisasmImpl, createEntry } from "./entry.ts"; +import { BaseImpl, DisasmImpl, UnknownImpl, createEntry } from "./entry.ts"; describe("entry", () => { it("type should met children type", () => { @@ -75,4 +75,32 @@ describe("entry", () => { expect(disasm.getType()).toBe("disasm"); }); }); + + describe("unknownImpl", () => { + it("getName returns 'Unknown'", () => { + const unknown = new UnknownImpl(1024); + expect(unknown.getName()).toBe("Unknown"); + }); + + it("getSize returns correct size", () => { + const unknown = new UnknownImpl(2048); + expect(unknown.getSize()).toBe(2048); + }); + + it("getChildren returns empty array", () => { + const unknown = new UnknownImpl(1024); + expect(unknown.getChildren()).toEqual([]); + }); + + it("toString includes size and unknown part description", () => { + const unknown = new UnknownImpl(1024); + const str = unknown.toString(); + expect(str).toMatchSnapshot(); + }); + + it("getType returns 'unknown'", () => { + const unknown = new UnknownImpl(1024); + expect(unknown.getType()).toBe("unknown"); + }); + }); }); diff --git a/ui/src/tool/useHash.test.ts b/ui/src/tool/useHash.test.ts new file mode 100644 index 0000000000..94f2509bcf --- /dev/null +++ b/ui/src/tool/useHash.test.ts @@ -0,0 +1,50 @@ +import { act, renderHook } from "@testing-library/react"; +import { beforeAll, describe, expect, it } from "vitest"; +import { useHash } from "./useHash"; + +describe("useHash", () => { + beforeAll(() => { + window.location = { hash: "" } as any; + }); + + it("initializes with the current window location hash", () => { + window.location.hash = "#initial"; + const { result } = renderHook(() => useHash()); + expect(result.current[0]).toBe("#initial"); + }); + + it("updates hash when setHash is called with a new value", () => { + const { result } = renderHook(() => useHash()); + act(() => { + result.current[1]("#newHash"); + }); + expect(window.location.hash).toBe("#newHash"); + }); + + it("does not update hash if setHash is called with the current hash value", () => { + window.location.hash = "#sameHash"; + const { result } = renderHook(() => useHash()); + act(() => { + result.current[1]("#sameHash"); + }); + expect(window.location.hash).toBe("#sameHash"); + }); + + it("removes hash when setHash is called with an empty string", () => { + window.location.hash = "#toBeRemoved"; + const { result } = renderHook(() => useHash()); + act(() => { + result.current[1](""); + }); + expect(window.location.hash).toBe(""); + }); + + it("responds to window hashchange event", () => { + const { result } = renderHook(() => useHash()); + act(() => { + window.location.hash = "#changedViaEvent"; + window.dispatchEvent(new HashChangeEvent("hashchange")); + }); + expect(result.current[0]).toBe("#changedViaEvent"); + }); +}); diff --git a/ui/src/tool/useHash.ts b/ui/src/tool/useHash.ts index d5eb14f87d..4f2e77a3cf 100644 --- a/ui/src/tool/useHash.ts +++ b/ui/src/tool/useHash.ts @@ -24,10 +24,8 @@ export function useHash() { const _setHash = useCallback( (newHash: string) => { if (newHash !== hash) { - if (newHash !== "") { - window.location.hash = newHash; - } - else { + window.location.hash = newHash; + if (newHash === "") { history.pushState(null, "", " "); } } diff --git a/ui/vitest.config.ts b/ui/vitest.config.ts index 3ddc637e23..ee6785bbae 100644 --- a/ui/vitest.config.ts +++ b/ui/vitest.config.ts @@ -15,6 +15,7 @@ export default defineConfig({ exclude: [ "src/tool/wasm_exec.js", "src/schema/schema.ts", + "src/generated/schema.ts", "vite.*.ts", ...coverageConfigDefaults.exclude, ],