|
| 1 | +from unittest.mock import Mock |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from observ import ( |
| 6 | + computed, |
| 7 | + readonly, |
| 8 | + shallow_reactive, |
| 9 | + trigger_ref, |
| 10 | + watch, |
| 11 | + watch_effect, |
| 12 | +) |
| 13 | + |
| 14 | + |
| 15 | +def test_trigger_ref_shallow_key_watcher(): |
| 16 | + state = shallow_reactive({"value": {"count": 0}}) |
| 17 | + watcher = watch(lambda: state["value"], Mock(), sync=True) |
| 18 | + |
| 19 | + # Deep mutations bypass a shallow proxy, so no watcher fires |
| 20 | + state["value"]["count"] = 1 |
| 21 | + watcher.callback.assert_not_called() |
| 22 | + |
| 23 | + trigger_ref(state) |
| 24 | + watcher.callback.assert_called_once() |
| 25 | + |
| 26 | + |
| 27 | +def test_trigger_ref_notifies_dep_and_keydeps(): |
| 28 | + state = shallow_reactive({"items": [1]}) |
| 29 | + keyed = watch(lambda: state["items"], Mock(), sync=True) |
| 30 | + container = watch(lambda: dict(state.items()), Mock(), sync=True) |
| 31 | + |
| 32 | + trigger_ref(state) |
| 33 | + |
| 34 | + keyed.callback.assert_called_once() |
| 35 | + container.callback.assert_called_once() |
| 36 | + |
| 37 | + |
| 38 | +def test_trigger_ref_watch_effect(): |
| 39 | + state = shallow_reactive({"value": [1, 2]}) |
| 40 | + lengths = [] |
| 41 | + watcher = watch_effect(lambda: lengths.append(len(state["value"])), sync=True) |
| 42 | + |
| 43 | + assert lengths == [2] |
| 44 | + |
| 45 | + state["value"].append(3) |
| 46 | + assert lengths == [2] |
| 47 | + |
| 48 | + trigger_ref(state) |
| 49 | + assert lengths == [2, 3] |
| 50 | + |
| 51 | + assert watcher.active |
| 52 | + |
| 53 | + |
| 54 | +def test_trigger_ref_computed(): |
| 55 | + state = shallow_reactive({"data": {"count": 1}}) |
| 56 | + |
| 57 | + @computed |
| 58 | + def doubled(): |
| 59 | + return state["data"]["count"] * 2 |
| 60 | + |
| 61 | + assert doubled() == 2 |
| 62 | + |
| 63 | + state["data"]["count"] = 3 |
| 64 | + # The computed doesn't know its (shallow) dependency changed |
| 65 | + assert doubled() == 2 |
| 66 | + |
| 67 | + trigger_ref(state) |
| 68 | + assert doubled() == 6 |
| 69 | + |
| 70 | + |
| 71 | +def test_trigger_ref_shallow_list(): |
| 72 | + items = shallow_reactive([{"a": 0}]) |
| 73 | + watcher = watch(lambda: items[0], Mock(), sync=True) |
| 74 | + |
| 75 | + items[0]["a"] = 1 |
| 76 | + watcher.callback.assert_not_called() |
| 77 | + |
| 78 | + trigger_ref(items) |
| 79 | + watcher.callback.assert_called_once() |
| 80 | + |
| 81 | + |
| 82 | +def test_trigger_ref_through_readonly_view(): |
| 83 | + state = shallow_reactive({"data": {"x": 0}}) |
| 84 | + view = readonly(state) |
| 85 | + watcher = watch(lambda: view["data"], Mock(), sync=True) |
| 86 | + |
| 87 | + state["data"]["x"] = 1 |
| 88 | + watcher.callback.assert_not_called() |
| 89 | + |
| 90 | + # All proxies for the same target share their deps, so triggering |
| 91 | + # any view notifies watchers on all of them |
| 92 | + trigger_ref(view) |
| 93 | + watcher.callback.assert_called_once() |
| 94 | + |
| 95 | + |
| 96 | +def test_trigger_ref_plain_value_unchanged(): |
| 97 | + # Callbacks that watch a plain (non-container) value only fire |
| 98 | + # when that value actually differs from the previous evaluation |
| 99 | + state = shallow_reactive({"value": 0}) |
| 100 | + watcher = watch(lambda: state["value"], Mock(), sync=True) |
| 101 | + |
| 102 | + trigger_ref(state) |
| 103 | + watcher.callback.assert_not_called() |
| 104 | + |
| 105 | + # But the watched function is re-evaluated, so effects do re-run |
| 106 | + reads = [] |
| 107 | + effect = watch_effect(lambda: reads.append(state["value"]), sync=True) |
| 108 | + assert reads == [0] |
| 109 | + trigger_ref(state) |
| 110 | + assert reads == [0, 0] |
| 111 | + |
| 112 | + assert effect.active |
| 113 | + |
| 114 | + |
| 115 | +def test_trigger_ref_requires_proxy(): |
| 116 | + with pytest.raises(TypeError): |
| 117 | + trigger_ref({"value": 1}) |
| 118 | + |
| 119 | + with pytest.raises(TypeError): |
| 120 | + trigger_ref(None) |
| 121 | + |
| 122 | + |
| 123 | +def test_trigger_ref_no_watchers(): |
| 124 | + # Triggering a proxy nobody watches is a no-op |
| 125 | + state = shallow_reactive({"value": 1}) |
| 126 | + trigger_ref(state) |
0 commit comments