Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jinyus committed Jan 31, 2024
1 parent 47bfdd6 commit 7f29674
Showing 1 changed file with 82 additions and 3 deletions.
85 changes: 82 additions & 3 deletions packages/state_beacon_core/test/src/wrapping_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void main() {
final wrapper = Beacon.writable<int>(0, name: 'wrapper');

wrapper.wrap(count, disposeTogether: true);
final buff = doubledCount.buffer(1).filter();
final buff = doubledCount.filter().buffer(1);

expect(wrapper.value, equals(10));

Expand Down Expand Up @@ -224,10 +224,10 @@ void main() {
final count = Beacon.readable<int>(10);

final beacon = count
.buffer(2)
.filter()
.throttle(duration: k10ms)
.debounce(duration: k10ms);
.debounce(duration: k10ms)
.filter();

Beacon.effect(() => beacon.value);

Expand Down Expand Up @@ -264,6 +264,7 @@ void main() {
});

test('should ingest stream and transform values', () async {
// BeaconObserver.instance = LoggingObserver();
final beacon = Beacon.writable<int>(0);
final myStream = Stream.fromIterable([1, 2, 3]);
final buffered = beacon.buffer(4);
Expand Down Expand Up @@ -379,4 +380,82 @@ void main() {

expect(filtered.value, 30);
});

test('should delegate writes to parent when chained/5', () {
// BeaconObserver.instance = LoggingObserver();

final count = Beacon.writable<int>(10, name: 'count');

final buffered = count
.filter(name: 'f1', filter: (_, n) => n > 5)
.buffer(2, name: 'buffered');

expect(buffered.value, <int>[]);
expect(buffered.currentBuffer(), <int>[10]);

buffered.add(20);

expect(count.value, 20);
expect(buffered.value, <int>[10, 20]);
expect(buffered.currentBuffer(), <int>[]);

buffered.add(2); // doesn't pass filter

expect(count.value, 2);
expect(buffered.value, <int>[10, 20]); // no change
expect(buffered.currentBuffer(), <int>[]); // no change

buffered.add(50); // doesn't pass filter

expect(count.value, 50);
expect(buffered.value, <int>[10, 20]);
expect(buffered.currentBuffer(), <int>[50]);

buffered.add(70); // doesn't pass filter

expect(count.value, 70);
expect(buffered.value, <int>[50, 70]);
expect(buffered.currentBuffer(), <int>[]);

buffered.reset();

expect(count.value, 10);
expect(buffered.value, <int>[]);
expect(buffered.currentBuffer(), <int>[]);
});

test('should throw when trying to chain a buffered beacon', () async {
final count = Beacon.writable<int>(10, name: 'count');

final buffered = Beacon.bufferedCount<int>(5);
final buffTime = Beacon.bufferedTime<int>(duration: k10ms);

expect(
buffered.filter,
throwsA(isA<AssertionError>()),
);
expect(
() => buffTime.buffer(10),
throwsA(isA<AssertionError>()),
);
expect(
() => buffered.debounce(duration: k1ms),
throwsA(isA<AssertionError>()),
);
expect(
() => buffered.throttle(duration: k1ms),
throwsA(isA<AssertionError>()),
);
expect(
() => buffered.bufferTime(duration: k1ms),
throwsA(isA<AssertionError>()),
);
expect(
() => count
.filter(name: 'f1', filter: (_, n) => n > 5)
.buffer(2, name: 'buffered')
.debounce(duration: k10ms),
throwsA(isA<AssertionError>()),
);
});
}

0 comments on commit 7f29674

Please sign in to comment.