Showing
2 changed files
with
44 additions
and
35 deletions
@@ -132,7 +132,7 @@ extension Inst on GetInterface { | @@ -132,7 +132,7 @@ extension Inst on GetInterface { | ||
132 | /// Replace a parent instance of a class in dependency management | 132 | /// Replace a parent instance of a class in dependency management |
133 | /// with a [child] instance | 133 | /// with a [child] instance |
134 | /// - [tag] optional, if you use a [tag] to register the Instance. | 134 | /// - [tag] optional, if you use a [tag] to register the Instance. |
135 | - void replace<P, C extends P>(C child, {String? tag}) { | 135 | + void replace<P>(P child, {String? tag}) { |
136 | final info = GetInstance().getInstanceInfo<P>(tag: tag); | 136 | final info = GetInstance().getInstanceInfo<P>(tag: tag); |
137 | final permanent = (info.isPermanent ?? false); | 137 | final permanent = (info.isPermanent ?? false); |
138 | delete<P>(tag: tag, force: permanent); | 138 | delete<P>(tag: tag, force: permanent); |
@@ -10,7 +10,9 @@ class Mock { | @@ -10,7 +10,9 @@ class Mock { | ||
10 | } | 10 | } |
11 | } | 11 | } |
12 | 12 | ||
13 | -class DisposableController extends GetLifeCycle {} | 13 | +abstract class MyController extends GetLifeCycle {} |
14 | + | ||
15 | +class DisposableController extends MyController {} | ||
14 | 16 | ||
15 | // ignore: one_member_abstracts | 17 | // ignore: one_member_abstracts |
16 | abstract class Service { | 18 | abstract class Service { |
@@ -157,42 +159,49 @@ void main() { | @@ -157,42 +159,49 @@ void main() { | ||
157 | }); | 159 | }); |
158 | }); | 160 | }); |
159 | 161 | ||
160 | - test('Get.replace test for replacing temporary parent instance with child', | ||
161 | - () async { | ||
162 | - Get.put(DisposableController()); | ||
163 | - Get.replace<DisposableController, Controller>(Controller()); | ||
164 | - final instance = Get.find<DisposableController>(); | ||
165 | - expect(instance is Controller, isTrue); | ||
166 | - expect((instance as Controller).init, greaterThan(0)); | ||
167 | - }); | 162 | + group('Get.replace test for replacing parent instance that is', () { |
163 | + test('temporary', () async { | ||
164 | + Get.put(DisposableController()); | ||
165 | + Get.replace<DisposableController>(Controller()); | ||
166 | + final instance = Get.find<DisposableController>(); | ||
167 | + expect(instance is Controller, isTrue); | ||
168 | + expect((instance as Controller).init, greaterThan(0)); | ||
169 | + }); | ||
168 | 170 | ||
169 | - test('Get.replace test for replacing permanent parent instance with child', | ||
170 | - () async { | ||
171 | - Get.put(DisposableController(), permanent: true); | ||
172 | - Get.replace<DisposableController, Controller>(Controller()); | ||
173 | - final instance = Get.find<DisposableController>(); | ||
174 | - expect(instance is Controller, isTrue); | ||
175 | - expect((instance as Controller).init, greaterThan(0)); | ||
176 | - }); | 171 | + test('permanent', () async { |
172 | + Get.put(DisposableController(), permanent: true); | ||
173 | + Get.replace<DisposableController>(Controller()); | ||
174 | + final instance = Get.find<DisposableController>(); | ||
175 | + expect(instance is Controller, isTrue); | ||
176 | + expect((instance as Controller).init, greaterThan(0)); | ||
177 | + }); | ||
177 | 178 | ||
178 | - test('Get.replace test for replacing tagged temporary instance with child', | ||
179 | - () async { | ||
180 | - final tag = 'tag'; | ||
181 | - Get.put(DisposableController(), tag: tag); | ||
182 | - Get.replace<DisposableController, Controller>(Controller(), tag: tag); | ||
183 | - final instance = Get.find<DisposableController>(tag: tag); | ||
184 | - expect(instance is Controller, isTrue); | ||
185 | - expect((instance as Controller).init, greaterThan(0)); | ||
186 | - }); | 179 | + test('tagged temporary', () async { |
180 | + final tag = 'tag'; | ||
181 | + Get.put(DisposableController(), tag: tag); | ||
182 | + Get.replace<DisposableController>(Controller(), tag: tag); | ||
183 | + final instance = Get.find<DisposableController>(tag: tag); | ||
184 | + expect(instance is Controller, isTrue); | ||
185 | + expect((instance as Controller).init, greaterThan(0)); | ||
186 | + }); | ||
187 | 187 | ||
188 | - test('Get.replace test for replacing tagged parent instance with child', | ||
189 | - () async { | ||
190 | - final tag = 'tag'; | ||
191 | - Get.put(DisposableController(), permanent: true, tag: tag); | ||
192 | - Get.replace<DisposableController, Controller>(Controller(), tag: tag); | ||
193 | - final instance = Get.find<DisposableController>(tag: tag); | ||
194 | - expect(instance is Controller, isTrue); | ||
195 | - expect((instance as Controller).init, greaterThan(0)); | 188 | + test('tagged permanent', () async { |
189 | + final tag = 'tag'; | ||
190 | + Get.put(DisposableController(), permanent: true, tag: tag); | ||
191 | + Get.replace<DisposableController>(Controller(), tag: tag); | ||
192 | + final instance = Get.find<DisposableController>(tag: tag); | ||
193 | + expect(instance is Controller, isTrue); | ||
194 | + expect((instance as Controller).init, greaterThan(0)); | ||
195 | + }); | ||
196 | + | ||
197 | + test('a generic parent type', () async { | ||
198 | + final tag = 'tag'; | ||
199 | + Get.put<MyController>(DisposableController(), permanent: true, tag: tag); | ||
200 | + Get.replace<MyController>(Controller(), tag: tag); | ||
201 | + final instance = Get.find<MyController>(tag: tag); | ||
202 | + expect(instance is Controller, isTrue); | ||
203 | + expect((instance as Controller).init, greaterThan(0)); | ||
204 | + }); | ||
196 | }); | 205 | }); |
197 | } | 206 | } |
198 | 207 |
-
Please register or login to post a comment