Skip to content

Commit d413e55

Browse files
committed
test: Add tests for WorldRoute
1 parent d5e7702 commit d413e55

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import 'package:flame/components.dart';
2+
import 'package:flame/game.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
class _TestWorld1 extends World {
6+
int value = 0;
7+
}
8+
9+
class _TestWorld2 extends World {}
10+
11+
void main() {
12+
group('WorldRoute', () {
13+
testWidgets('can set a new world', (tester) async {
14+
final router = RouterComponent(
15+
initialRoute: '/',
16+
routes: {
17+
'/': Route(Component.new),
18+
'/world': WorldRoute(_TestWorld1.new),
19+
},
20+
);
21+
final game = FlameGame(children: [router]);
22+
await tester.pumpWidget(GameWidget(game: game));
23+
await tester.pump();
24+
await tester.pump();
25+
expect(router.currentRoute.name, '/');
26+
expect(game.world, isNot(isA<_TestWorld1>));
27+
28+
router.pushNamed('/world');
29+
await tester.pump();
30+
expect(router.currentRoute.name, '/world');
31+
expect(game.world, isA<_TestWorld1>());
32+
});
33+
34+
testWidgets('changes back to previous world on pop', (tester) async {
35+
final router = RouterComponent(
36+
initialRoute: '/world1',
37+
routes: {
38+
'/world1': WorldRoute(_TestWorld1.new),
39+
'/world2': WorldRoute(_TestWorld2.new),
40+
},
41+
);
42+
final game = FlameGame(children: [router]);
43+
await tester.pumpWidget(GameWidget(game: game));
44+
await tester.pump();
45+
await tester.pump();
46+
expect(router.currentRoute.name, '/world1');
47+
expect(game.world, isA<_TestWorld1>());
48+
49+
router.pushNamed('/world2');
50+
expect(router.currentRoute.name, '/world2');
51+
expect(game.world, isA<_TestWorld2>());
52+
53+
router.pop();
54+
expect(router.currentRoute.name, '/world1');
55+
expect(game.world, isA<_TestWorld1>());
56+
});
57+
58+
testWidgets('retains the state of the world', (tester) async {
59+
final router = RouterComponent(
60+
initialRoute: '/world1',
61+
routes: {
62+
'/world1': WorldRoute(_TestWorld1.new),
63+
'/world2': WorldRoute(_TestWorld2.new),
64+
},
65+
);
66+
final game = FlameGame(children: [router]);
67+
await tester.pumpWidget(GameWidget(game: game));
68+
await tester.pump();
69+
await tester.pump();
70+
expect(router.currentRoute.name, '/world1');
71+
expect(game.world, isA<_TestWorld1>());
72+
expect((game.world as _TestWorld1).value, isZero);
73+
(game.world as _TestWorld1).value = 1;
74+
75+
router.pushReplacementNamed('/world2');
76+
expect(router.currentRoute.name, '/world2');
77+
expect(game.world, isA<_TestWorld2>());
78+
79+
router.pushReplacementNamed('/world1');
80+
expect(router.currentRoute.name, '/world1');
81+
expect(game.world, isA<_TestWorld1>());
82+
expect((game.world as _TestWorld1).value, 1);
83+
});
84+
85+
testWidgets('does not retain the state of world', (tester) async {
86+
final router = RouterComponent(
87+
initialRoute: '/world1',
88+
routes: {
89+
'/world1': WorldRoute(_TestWorld1.new, maintainState: false),
90+
'/world2': WorldRoute(_TestWorld2.new),
91+
},
92+
);
93+
final game = FlameGame(children: [router]);
94+
await tester.pumpWidget(GameWidget(game: game));
95+
await tester.pump();
96+
await tester.pump();
97+
expect(router.currentRoute.name, '/world1');
98+
expect(game.world, isA<_TestWorld1>());
99+
expect((game.world as _TestWorld1).value, isZero);
100+
(game.world as _TestWorld1).value = 1;
101+
102+
router.pushReplacementNamed('/world2');
103+
expect(router.currentRoute.name, '/world2');
104+
expect(game.world, isA<_TestWorld2>());
105+
106+
router.pushReplacementNamed('/world1');
107+
expect(router.currentRoute.name, '/world1');
108+
expect(game.world, isA<_TestWorld1>());
109+
expect((game.world as _TestWorld1).value, isZero);
110+
});
111+
});
112+
}

0 commit comments

Comments
 (0)