Skip to content

Commit e85cf94

Browse files
committed
✅ Adding tests.
1 parent 8797d7f commit e85cf94

File tree

1 file changed

+283
-1
lines changed

1 file changed

+283
-1
lines changed

test/store_test.dart

Lines changed: 283 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,289 @@ void main() {
718718
await future;
719719
});
720720

721-
group('selectMany', () {});
721+
group('selectMany', () {
722+
test('~= select2', () async {
723+
final store = RxReduxStore<int, _State>(
724+
initialState: _State(true, null, <String>[].build(), 1),
725+
sideEffects: [],
726+
reducer: (s, a) {
727+
// items [*]
728+
if (a == 0) {
729+
return _State(
730+
s.isLoading,
731+
s.term,
732+
List.generate(10, (i) => i.toString()).build(),
733+
s.otherState,
734+
);
735+
}
736+
737+
// loading
738+
if (a == 1) {
739+
return _State(!s.isLoading, s.term, s.items, s.otherState);
740+
}
741+
742+
// loading
743+
if (a == 2) {
744+
return _State(!s.isLoading, s.term, s.items, s.otherState);
745+
}
746+
747+
// term [*]
748+
if (a == 3) {
749+
return _State(s.isLoading, '4', s.items, s.otherState);
750+
}
751+
752+
// otherState
753+
if (a == 4) {
754+
return _State(s.isLoading, s.term, s.items, s.otherState + 2);
755+
}
756+
757+
// loading & otherState
758+
if (a == 5) {
759+
return _State(!s.isLoading, s.term, s.items, -s.otherState);
760+
}
761+
762+
throw a;
763+
},
764+
);
765+
766+
await pumpEventQueue(times: 50);
767+
768+
var termCount = 0;
769+
var itemsCount = 0;
770+
var projectorCount = 0;
771+
772+
final filtered = store.selectMany<Object?, BuiltList<String>>(
773+
[
774+
(s) {
775+
++termCount;
776+
return s.term;
777+
},
778+
(s) {
779+
++itemsCount;
780+
return s.items;
781+
}
782+
],
783+
[null, null],
784+
(List<Object?> subStates) {
785+
++projectorCount;
786+
return (subStates[1] as BuiltList<String>)
787+
.where((i) => i.contains((subStates[0] as String?) ?? ''))
788+
.toBuiltList();
789+
},
790+
);
791+
792+
expect(filtered.requireValue, <String>[].build());
793+
final future = expectLater(
794+
filtered,
795+
emitsInOrder(<Object>[
796+
List.generate(10, (i) => i.toString()).build(),
797+
['4'].build(),
798+
emitsDone,
799+
]),
800+
);
801+
802+
final numberOfActions = 6;
803+
for (var i = 0; i < numberOfActions; i++) {
804+
store.dispatch(i);
805+
}
806+
await pumpEventQueue(times: 50);
807+
await store.dispose();
808+
await future;
809+
810+
expect(termCount,
811+
numberOfActions + 1); // inc. calling to produce seed value
812+
expect(itemsCount,
813+
numberOfActions + 1); // inc. calling to produce seed value
814+
expect(projectorCount,
815+
2 + 1); // 2 [*] and inc. calling to produce seed value
816+
});
817+
818+
test('~= select3', () async {
819+
final store = RxReduxStore<int, _State>(
820+
initialState: _State(true, null, <String>[].build(), 2),
821+
sideEffects: [],
822+
reducer: (s, a) {
823+
// items [*]
824+
if (a == 0) {
825+
return _State(
826+
s.isLoading,
827+
s.term,
828+
List.generate(10, (i) => i.toString()).build(),
829+
s.otherState,
830+
);
831+
}
832+
833+
// loading
834+
if (a == 1) {
835+
return _State(!s.isLoading, s.term, s.items, s.otherState);
836+
}
837+
838+
// loading
839+
if (a == 2) {
840+
return _State(!s.isLoading, s.term, s.items, s.otherState);
841+
}
842+
843+
// term [*]
844+
if (a == 3) {
845+
return _State(s.isLoading, '4', s.items, s.otherState);
846+
}
847+
848+
// otherState [*]
849+
if (a == 4) {
850+
return _State(s.isLoading, s.term, s.items, s.otherState + 2);
851+
}
852+
853+
// loading & otherState [*]
854+
if (a == 5) {
855+
return _State(!s.isLoading, s.term, s.items, s.otherState - 1);
856+
}
857+
858+
throw a;
859+
},
860+
);
861+
862+
await pumpEventQueue(times: 50);
863+
864+
var termCount = 0;
865+
var itemsCount = 0;
866+
var otherStateCount = 0;
867+
var projectorCount = 0;
868+
869+
final filtered = store.selectMany(
870+
[
871+
(s) {
872+
++termCount;
873+
return s.term;
874+
},
875+
(s) {
876+
++itemsCount;
877+
return s.items;
878+
},
879+
(s) {
880+
++otherStateCount;
881+
return s.otherState.round();
882+
},
883+
],
884+
[null, null, null],
885+
(List<Object?> subStates) {
886+
++projectorCount;
887+
888+
final term = subStates[0] as String?;
889+
final items = subStates[1] as BuiltList<String>;
890+
final otherState = subStates[2] as int;
891+
892+
return items
893+
.where((i) => i.contains(term ?? ''))
894+
.take(otherState)
895+
.toBuiltList();
896+
},
897+
);
898+
899+
expect(filtered.requireValue, <String>[].build());
900+
final future = expectLater(
901+
filtered,
902+
emitsInOrder(<Object>[
903+
['0', '1'].build(),
904+
['4'].build(),
905+
emitsDone,
906+
]),
907+
);
908+
909+
final numberOfActions = 6;
910+
for (var i = 0; i < numberOfActions; i++) {
911+
store.dispatch(i);
912+
}
913+
await pumpEventQueue(times: 50);
914+
await store.dispose();
915+
await future;
916+
917+
expect(termCount,
918+
numberOfActions + 1); // inc. calling to produce seed value
919+
expect(itemsCount,
920+
numberOfActions + 1); // inc. calling to produce seed value
921+
expect(otherStateCount,
922+
numberOfActions + 1); // inc. calling to produce seed value
923+
expect(projectorCount,
924+
4 + 1); // 4 [*] and inc. calling to produce seed value
925+
});
926+
927+
test('~= select4', () async {
928+
final initial = Tuple5(0, 1.0, '', true, <String>[].build());
929+
930+
final store = RxReduxStore<int,
931+
Tuple5<int, double, String, bool, BuiltList<String>>>(
932+
initialState: initial,
933+
sideEffects: [],
934+
reducer: (s, a) {
935+
switch (a) {
936+
case 0:
937+
return s;
938+
case 1:
939+
return s.withItem5(s.item5.rebuild((b) => b.remove('01')));
940+
case 2:
941+
return s.withItem1(s.item1 + 1);
942+
case 3:
943+
return s.withItem2(s.item2 + 2);
944+
case 4:
945+
return s.withItem5(s.item5.rebuild((b) => b.add('01')));
946+
case 5:
947+
return s;
948+
case 6:
949+
return s.withItem3(s.item3 + '3');
950+
case 7:
951+
return s.withItem4(!s.item4);
952+
case 8:
953+
return s.withItem5(s.item5.rebuild((b) => b.add('5')));
954+
default:
955+
throw a;
956+
}
957+
},
958+
);
959+
960+
final tuple$ = store.selectMany(
961+
[
962+
expectAsync1((state) => state.item1, count: 7 + 1),
963+
// 7 action causes state changed
964+
expectAsync1((state) => state.item2, count: 7 + 1),
965+
// 7 action causes state changed
966+
expectAsync1((state) => state.item3, count: 7 + 1),
967+
// 7 action causes state changed
968+
expectAsync1((state) => state.item4, count: 7 + 1),
969+
// 7 action causes state changed
970+
],
971+
[null, null, null, null],
972+
expectAsync1(
973+
(List<Object?> subStates) => Tuple4(
974+
subStates[0] as int,
975+
subStates[1] as double,
976+
subStates[2] as String,
977+
subStates[3] as bool,
978+
),
979+
count: 4 + 1, // inc. calling to produce seed value
980+
),
981+
);
982+
983+
final tuple4 = Tuple4<int, double, String, bool>(0, 1.0, '', true);
984+
expect(tuple$.value, tuple4);
985+
final future = expectLater(
986+
tuple$,
987+
emitsInOrder(<Object>[
988+
Tuple4(0, 1.0, '', false), // 7
989+
Tuple4(0, 1.0, '3', false), // 6
990+
Tuple4(0, 3.0, '3', false), // 3
991+
Tuple4(1, 3.0, '3', false), // 2
992+
emitsDone,
993+
]),
994+
);
995+
996+
for (var i = 8; i >= 0; i--) {
997+
i.dispatchTo(store);
998+
}
999+
await pumpEventQueue(times: 100);
1000+
await store.dispose();
1001+
await future;
1002+
});
1003+
});
7221004
});
7231005
});
7241006
}

0 commit comments

Comments
 (0)