mobx
mobx copied to clipboard
`ComputedValue` fails to trigger `onBecomeObservable` when used in action -> autorun
import { autorun, computed, createAtom, runInAction } from 'mobx';
test('works', () => {
const observed = jest.fn();
const atom = createAtom('test', observed);
const value = computed(() => atom.reportObserved());
value.get();
autorun(() => value.get());
expect(observed).toBeCalled();
});
test('does not work', () => {
const observed = jest.fn();
const atom = createAtom('test', observed);
const value = computed(() => atom.reportObserved());
runInAction(() => {
value.get();
autorun(() => value.get());
});
expect(observed).toBeCalled();
});
Both tests should pass or fail (preferably pass). Current behaviour is inconsistent.
Intended outcome:
Atom changes used in derivation function should be tracked in both test cases
Actual outcome:
Atom changes used in derivation function is not tracked inside autorun wrapped with runInAction.
How to reproduce the issue:
Run test above
Versions
MobX v6.9.0
I encountered a similar problem
test('fails to trigger onBecomeObservable', () => {
const observed = jest.fn();
const atom = createAtom('test', observed);
const withAtom = observable(false);
const derivedState = computed(() => withAtom.get() ? atom.reportObserved() : null);
autorun(() => derivedState.get());
runInAction(() => {
withAtom.set(true);
derivedState.get(); // getting derivedState in action causes computed to be updated outside the observer context
});
expect(observed).toBeCalled();
});