Skip to content

Commit

Permalink
test condiitonal listening for derivedFuture
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyus committed Jan 27, 2024
1 parent 5997fac commit 886b97e
Showing 1 changed file with 56 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ void main() {
expect(fullName.value.unwrap(), 'Sally 1 Smith 2');
});

test('should return error when dependency throws error', () async {
test('should return error when callback throws error', () async {
final count = Beacon.writable(0);

final firstName = Beacon.derivedFuture(() async {
Expand Down Expand Up @@ -424,4 +424,59 @@ void main() {

expect(ran, 4);
});

test('should conditionally stop listening to dependencies', () async {
final num1 = Beacon.writable<int>(10);
final num2 = Beacon.writable<int>(10);
final num3 = Beacon.writable<int>(10);
final guard = Beacon.writable<bool>(true);

final derivedBeacon = Beacon.derivedFuture(() async {
if (guard.value) return num1.value;

return num2.value + num3.value;
});

expect(num1.listenersCount, 1);
expect(num2.listenersCount, 0);
expect(num3.listenersCount, 0);

expect(derivedBeacon.isLoading, true);

await Future<void>.delayed(k1ms);

expect(derivedBeacon.unwrapValue(), 10);

num1.increment();

expect(derivedBeacon.isLoading, true);

await Future<void>.delayed(k1ms);

expect(derivedBeacon.unwrapValue(), 11);

guard.value = false;

expect(num1.listenersCount, 0);
expect(num2.listenersCount, 1);
expect(num3.listenersCount, 1);

expect(derivedBeacon.isLoading, true);

await Future<void>.delayed(k1ms);

expect(derivedBeacon.unwrapValue(), 20);

num1.increment();

expect(derivedBeacon.unwrapValue(), 20);

num2.increment();

expect(derivedBeacon.isLoading, true);

await Future<void>.delayed(k1ms);

expect(derivedBeacon.unwrapValue(), 21);
});
}

0 comments on commit 886b97e

Please sign in to comment.