Skip to content

Commit e857442

Browse files
committed
fix(truncate): guard update() options in truncate action
1 parent dc699f6 commit e857442

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/Truncate/truncate.d.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@ interface TruncateOptions {
22
clamp?: "end" | "front";
33
}
44

5+
/**
6+
* Svelte action that applies single-line text truncation to an element.
7+
* @param node - The element to truncate
8+
* @param options - Optional clamp direction ("end" or "front")
9+
* @returns Object with update method (options may be undefined when action is updated with no args)
10+
*/
511
export function truncate(
612
node: HTMLElement,
713
options?: TruncateOptions,

src/Truncate/truncate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export function truncate(node, options = {}) {
2121

2222
return {
2323
update(options) {
24-
toggleClass(options.clamp === "front");
24+
toggleClass(options?.clamp === "front");
2525
},
2626
};
2727
}

tests/Truncate/Truncate.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { render, screen } from "@testing-library/svelte";
2+
import { truncate } from "../../src/Truncate/truncate.js";
23
import Truncate from "./Truncate.test.svelte";
34
import TruncateAction from "./TruncateAction.test.svelte";
45

@@ -86,5 +87,12 @@ describe("Truncate", () => {
8687
const element = screen.getByText(/This is a long text/);
8788
expect(element).toHaveClass("bx--text-truncate--end");
8889
});
90+
91+
it("update() with no arguments does not throw (options optional guard)", () => {
92+
const node = document.createElement("p");
93+
const { update } = truncate(node);
94+
expect(() => update()).not.toThrow();
95+
expect(() => update(undefined)).not.toThrow();
96+
});
8997
});
9098
});

0 commit comments

Comments
 (0)