Skip to content

Commit da95991

Browse files
committed
Add a test with useObservable reading a computed, for documentation purposes
1 parent 3ff5230 commit da95991

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

‎tests/hooks/useObservable.test.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,25 @@ test("can read from an observable", () => {
2222
expect(element.text()).toBe("Jack");
2323
});
2424

25+
test("can read from a computed", () => {
26+
const Component = ({text: textComputed}: { text: KnockoutComputed<string> }) => {
27+
const text = useObservable(textComputed);
28+
return <h1>{text}</h1>;
29+
};
30+
const firstName = ko.observable("Joe");
31+
const lastName = ko.observable("Smith");
32+
const fullName = ko.computed(() => `${firstName()} ${lastName()}`);
33+
34+
const element = mount(<Component text={fullName} />);
35+
36+
expect(element.text()).toBe("Joe Smith");
37+
38+
act(() => {
39+
lastName("Danger");
40+
});
41+
expect(element.text()).toBe("Joe Danger");
42+
});
43+
2544
test("behaves appropriately if the observable is swapped for a different observable", () => {
2645
const Child = ({count}: { count: KnockoutObservable<number> }) => {
2746
const countValue = useObservable(count);

0 commit comments

Comments
 (0)