Skip to content

Commit

Permalink
moving write policy inside the group...test!
Browse files Browse the repository at this point in the history
  • Loading branch information
pavanpodila committed Jan 15, 2020
1 parent 15f0d9b commit a08a9a5
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 84 deletions.
35 changes: 21 additions & 14 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

143 changes: 73 additions & 70 deletions mobx/test/action_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import 'shared_mocks.dart';
import 'util.dart';

void main() {
turnOffWritePolicy();

group('Action', () {
turnOffWritePolicy();

Expand Down Expand Up @@ -219,102 +217,107 @@ void main() {
});
});

test('runInAction works', () {
final x = Observable(10);
final y = Observable(20);

var executionCount = 0;
var total = 0;

final d = autorun((_) {
total = x.value + y.value;
executionCount++;
});

runInAction(() {
x.value = 100;
y.value = 200;
group('Action utility functions', () {
turnOffWritePolicy();

expect(executionCount, equals(1)); // No notifications are fired
});
test('runInAction works', () {
final x = Observable(10);
final y = Observable(20);

// Notifications are fired now
expect(executionCount, equals(2));
expect(total, equals(300));
var executionCount = 0;
var total = 0;

d();
});
final d = autorun((_) {
total = x.value + y.value;
executionCount++;
});

test('transaction works', () {
mainContext.config = ReactiveConfig(writePolicy: ReactiveWritePolicy.never);
runInAction(() {
x.value = 100;
y.value = 200;

final x = Observable(10);
final y = Observable(20);
expect(executionCount, equals(1)); // No notifications are fired
});

var total = 0;
// Notifications are fired now
expect(executionCount, equals(2));
expect(total, equals(300));

final d = autorun((_) {
total = x.value + y.value;
d();
});

transaction(() {
x.value = 100;
y.value = 200;

// within a transaction(), there are no notifications fired, so the total should not change
expect(total, equals(30));
});
test('transaction works', () {
mainContext.config =
ReactiveConfig(writePolicy: ReactiveWritePolicy.never);

// Notifications fire now, causing autorun() to execute
expect(total, equals(300));
final x = Observable(10);
final y = Observable(20);

d();
var total = 0;

mainContext.config = ReactiveConfig.main;
});
final d = autorun((_) {
total = x.value + y.value;
});

test('untracked works', () {
final x = Observable(0);
var count = 0;
transaction(() {
x.value = 100;
y.value = 200;

final d = autorun((_) {
// No tracking should be performed since we are reading inside untracked()
untracked(() {
x.value;
// within a transaction(), there are no notifications fired, so the total should not change
expect(total, equals(30));
});

count++;
// Notifications fire now, causing autorun() to execute
expect(total, equals(300));

d();

mainContext.config = ReactiveConfig.main;
});

expect(count, equals(1));
test('untracked works', () {
final x = Observable(0);
var count = 0;

x.value = 100;
final d = autorun((_) {
// No tracking should be performed since we are reading inside untracked()
untracked(() {
x.value;
});

// Should be no change in count
expect(count, equals(1));
count++;
});

d();
});
expect(count, equals(1));

test('runInAction return type', () {
final x = Observable(10);
x.value = 100;

var executionCount = 0;
var total = 0;
// Should be no change in count
expect(count, equals(1));

final d = autorun((_) {
total = x.value;
executionCount++;
d();
});

final value = runInAction(() => x.value = 100);
test('runInAction return type', () {
final x = Observable(10);

var executionCount = 0;
var total = 0;

final d = autorun((_) {
total = x.value;
executionCount++;
});

expect(value, equals(100));
final value = runInAction(() => x.value = 100);

// Notifications are fired now
expect(executionCount, equals(2));
expect(total, equals(100));
expect(value, equals(100));

d();
// Notifications are fired now
expect(executionCount, equals(2));
expect(total, equals(100));

d();
});
});
}

0 comments on commit a08a9a5

Please sign in to comment.