2026-05-14 17:12:01 +08:00
|
|
|
// test/observer.test.js
|
2026-06-06 11:45:52 +08:00
|
|
|
window.testObserver = async function() {
|
|
|
|
|
const { NewState, onNotifyUpdate, setActiveBinding } = ApigoState;
|
2026-05-14 17:12:01 +08:00
|
|
|
console.log('Testing observer.js...');
|
|
|
|
|
|
2026-06-06 11:45:52 +08:00
|
|
|
const state = NewState({ a: 1, b: 2 });
|
|
|
|
|
let notifyCount = 0;
|
2026-05-14 17:12:01 +08:00
|
|
|
|
2026-06-06 11:45:52 +08:00
|
|
|
onNotifyUpdate(() => {
|
|
|
|
|
notifyCount++;
|
2026-05-14 17:12:01 +08:00
|
|
|
});
|
|
|
|
|
|
2026-06-06 11:45:52 +08:00
|
|
|
// Test binding registration
|
|
|
|
|
setActiveBinding({ node: { isConnected: true }, attr: 'text', tpl: 'a' });
|
|
|
|
|
state.a; // Trigger getter
|
|
|
|
|
setActiveBinding(null);
|
|
|
|
|
|
|
|
|
|
// Test notification
|
|
|
|
|
state.a = 2;
|
|
|
|
|
if (notifyCount !== 1) throw new Error('Notification failed');
|
|
|
|
|
|
|
|
|
|
// Test unproxy data
|
|
|
|
|
if (state.a !== 2) throw new Error('State update failed');
|
|
|
|
|
|
2026-05-14 17:12:01 +08:00
|
|
|
console.log('observer.js tests passed');
|
|
|
|
|
return true;
|
|
|
|
|
}
|