Skip to content

Commit

Permalink
test: add tests for Computed.dispose
Browse files Browse the repository at this point in the history
  • Loading branch information
fzyzcjy committed Sep 4, 2022
1 parent 20fe5d3 commit 63d57ef
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion mobx/test/computed_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void main() {
});

group(
'when the computation is called twice, should dispose the value generated in the first computation',
'when the computation is called twice, should dispose the unused value',
() {
test('when access Computed twice *outside* reactive environment', () {
expect(computedCallCount, 0);
Expand Down Expand Up @@ -339,6 +339,40 @@ void main() {
expect(computedCallCount, 1);
expect(disposeValueArguments, const <String>['A1-Computed#0']);
});

group('when call Computed.dispose()', () {
test('when nobody was observing it, should do nothing', () {
computed.dispose();
expect(computedCallCount, 0);
expect(disposeValueArguments, const <String>[]);
});

test(
'when somebody was observing it, should still dispose the current value',
() {
expect(computedCallCount, 0);
expect(disposeValueArguments, const <String>[]);

late final String computedValue;
final autorunDisposer =
autorun((_) => computedValue = computed.value);
expect(computedValue, 'A1-Computed#0');

expect(computedCallCount, 1);
expect(disposeValueArguments, const <String>[]);

// NOTE this
computed.dispose();

expect(computedCallCount, 1);
expect(disposeValueArguments, const <String>['A1-Computed#0']);

autorunDisposer();

expect(computedCallCount, 1);
expect(disposeValueArguments, const <String>['A1-Computed#0']);
});
});
});
});
}

0 comments on commit 63d57ef

Please sign in to comment.