get_instance_test.dart
4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import 'package:flutter_test/flutter_test.dart';
import 'package:get/get.dart';
import 'util/matcher.dart' as m;
class Mock {
static Future<String> test() async {
await Future.delayed(Duration.zero);
return 'test';
}
}
class DisposableController extends GetLifeCycle {}
// ignore: one_member_abstracts
abstract class Service {
String post();
}
class Api implements Service {
@override
String post() {
return 'test';
}
}
void main() {
test('Get.putAsync test', () async {
await Get.putAsync<String>(Mock.test);
expect('test', Get.find<String>());
Get.reset();
});
test('Get.put test', () async {
final instance = Get.put<Controller>(Controller());
expect(instance, Get.find<Controller>());
Get.reset();
});
test('Get start and delete called just one time', () async {
Get..put(Controller())..put(Controller());
final controller = Get.find<Controller>();
expect(controller.init, 1);
Get..delete<Controller>()..delete<Controller>();
expect(controller.close, 1);
Get.reset();
});
test('Get.put tag test', () async {
final instance = Get.put<Controller>(Controller(), tag: 'one');
final instance2 = Get.put<Controller>(Controller(), tag: 'two');
expect(instance == instance2, false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'),
false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'),
true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'),
true);
Get.reset();
});
test('Get.lazyPut tag test', () async {
Get.lazyPut<Controller>(() => Controller(), tag: 'one');
Get.lazyPut<Controller>(() => Controller(), tag: 'two');
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'two'),
false);
expect(Get.find<Controller>(tag: 'one') == Get.find<Controller>(tag: 'one'),
true);
expect(Get.find<Controller>(tag: 'two') == Get.find<Controller>(tag: 'two'),
true);
Get.reset();
});
test('Get.lazyPut test', () async {
final controller = Controller();
Get.lazyPut<Controller>(() => controller);
final ct1 = Get.find<Controller>();
expect(ct1, controller);
Get.reset();
});
test('Get.lazyPut fenix test', () async {
Get.lazyPut<Controller>(() => Controller(), fenix: true);
Get.find<Controller>().increment();
expect(Get.find<Controller>().count, 1);
Get.delete<Controller>();
expect(Get.find<Controller>().count, 0);
Get.reset();
});
test('Get.lazyPut without fenix', () async {
Get.lazyPut<Controller>(() => Controller());
Get.find<Controller>().increment();
expect(Get.find<Controller>().count, 1);
Get.delete<Controller>();
expect(() => Get.find<Controller>(), throwsA(m.TypeMatcher<String>()));
Get.reset();
});
test('Get.reloadInstance test', () async {
Get.lazyPut<Controller>(() => Controller());
var ct1 = Get.find<Controller>();
ct1.increment();
expect(ct1.count, 1);
ct1 = Get.find<Controller>();
expect(ct1.count, 1);
GetInstance().reload<Controller>();
ct1 = Get.find<Controller>();
expect(ct1.count, 0);
Get.reset();
});
test('Get.lazyPut with abstract class test', () async {
final api = Api();
Get.lazyPut<Service>(() => api);
final ct1 = Get.find<Service>();
expect(ct1, api);
Get.reset();
});
test('Get.create with abstract class test', () async {
Get.create<Service>(() => Api());
final ct1 = Get.find<Service>();
final ct2 = Get.find<Service>();
expect(ct1 is Service, true);
expect(ct2 is Service, true);
expect(ct1 == ct2, false);
Get.reset();
});
group('test put, delete and check onInit execution', () {
tearDownAll(Get.reset);
test('Get.put test with init check', () async {
final instance = Get.put(DisposableController());
expect(instance, Get.find<DisposableController>());
expect(instance.initialized, true);
});
test('Get.delete test with disposable controller', () async {
// Get.put(DisposableController());
expect(await Get.delete<DisposableController>(), true);
expect(() => Get.find<DisposableController>(),
throwsA(m.TypeMatcher<String>()));
});
test('Get.put test after delete with disposable controller and init check',
() async {
final instance = Get.put<DisposableController>(DisposableController());
expect(instance, Get.find<DisposableController>());
expect(instance.initialized, true);
});
});
}
class Controller extends DisposableController {
int init = 0;
int close = 0;
int count = 0;
@override
void onInit() {
init++;
super.onInit();
}
@override
void onClose() {
close++;
super.onClose();
}
void increment() {
count++;
}
}