Showing
11 changed files
with
134 additions
and
40 deletions
@@ -1097,6 +1097,74 @@ The only way to actually delete a `GetxService`, is with `Get.reset()` which is | @@ -1097,6 +1097,74 @@ The only way to actually delete a `GetxService`, is with `Get.reset()` which is | ||
1097 | "Hot Reboot" of your app. So remember, if you need absolute persistence of a class instance during the | 1097 | "Hot Reboot" of your app. So remember, if you need absolute persistence of a class instance during the |
1098 | lifetime of your app, use `GetxService`. | 1098 | lifetime of your app, use `GetxService`. |
1099 | 1099 | ||
1100 | + | ||
1101 | +### Tests | ||
1102 | + | ||
1103 | +You can test your controllers like any other class, including their lifecycles: | ||
1104 | + | ||
1105 | +```dart | ||
1106 | +class Controller extends GetxController { | ||
1107 | + @override | ||
1108 | + void onInit() { | ||
1109 | + super.onInit(); | ||
1110 | + //Change value to name2 | ||
1111 | + name.value = 'name2'; | ||
1112 | + } | ||
1113 | + | ||
1114 | + @override | ||
1115 | + void onClose() { | ||
1116 | + name.value = ''; | ||
1117 | + super.onClose(); | ||
1118 | + } | ||
1119 | + | ||
1120 | + final name = 'name1'.obs; | ||
1121 | + | ||
1122 | + void changeName() => name.value = 'name3'; | ||
1123 | +} | ||
1124 | + | ||
1125 | +void main() { | ||
1126 | + test(''' | ||
1127 | +Test the state of the reactive variable "name" across all of its lifecycles''', | ||
1128 | + () { | ||
1129 | + /// You can test the controller without the lifecycle, | ||
1130 | + /// but it's not recommended unless you're not using | ||
1131 | + /// GetX dependency injection | ||
1132 | + final controller = Controller(); | ||
1133 | + expect(controller.name.value, 'name1'); | ||
1134 | + | ||
1135 | + /// If you are using it, you can test everything, | ||
1136 | + /// including the state of the application after each lifecycle. | ||
1137 | + Get.put(controller); // onInit was called | ||
1138 | + expect(controller.name.value, 'name2'); | ||
1139 | + | ||
1140 | + /// Test your functions | ||
1141 | + controller.changeName(); | ||
1142 | + expect(controller.name.value, 'name3'); | ||
1143 | + | ||
1144 | + /// onClose was called | ||
1145 | + Get.delete<Controller>(); | ||
1146 | + | ||
1147 | + expect(controller.name.value, ''); | ||
1148 | + }); | ||
1149 | +} | ||
1150 | +``` | ||
1151 | + | ||
1152 | +#### Tips | ||
1153 | + | ||
1154 | +##### Mockito or mocktail | ||
1155 | +If you need to mock your GetxController/GetxService, you should extend GetxController, and mixin it with Mock, that way | ||
1156 | + | ||
1157 | +```dart | ||
1158 | +class NotificationServiceMock extends GetxService with Mock implements NotificationService {} | ||
1159 | +``` | ||
1160 | + | ||
1161 | +##### Using Get.reset() | ||
1162 | +If you are testing widgets, or test groups, use Get.reset at the end of your test or in tearDown to reset all settings from your previous test. | ||
1163 | + | ||
1164 | +##### Get.testMode | ||
1165 | +if you are using your navigation in your controllers, use `Get.testMode = true` at the beginning of your main. | ||
1166 | + | ||
1167 | + | ||
1100 | # Breaking changes from 2.0 | 1168 | # Breaking changes from 2.0 |
1101 | 1169 | ||
1102 | 1- Rx types: | 1170 | 1- Rx types: |
@@ -3,6 +3,7 @@ | @@ -3,6 +3,7 @@ | ||
3 | /// injection, and route management in a quick and practical way. | 3 | /// injection, and route management in a quick and practical way. |
4 | library get; | 4 | library get; |
5 | 5 | ||
6 | +export 'get_common/get_reset.dart'; | ||
6 | export 'get_connect/connect.dart'; | 7 | export 'get_connect/connect.dart'; |
7 | export 'get_core/get_core.dart'; | 8 | export 'get_core/get_core.dart'; |
8 | export 'get_instance/get_instance.dart'; | 9 | export 'get_instance/get_instance.dart'; |
lib/get_common/get_reset.dart
0 → 100644
1 | +import '../get.dart'; | ||
2 | + | ||
3 | +extension GetResetExt on GetInterface { | ||
4 | + void reset( | ||
5 | + {@deprecated bool clearFactory = true, bool clearRouteBindings = true}) { | ||
6 | + GetInstance().resetInstance(clearRouteBindings: clearRouteBindings); | ||
7 | + Get.clearRouteTree(); | ||
8 | + Get.clearTranslations(); | ||
9 | + Get.resetRootNavigator(); | ||
10 | + } | ||
11 | +} |
@@ -95,14 +95,14 @@ extension Inst on GetInterface { | @@ -95,14 +95,14 @@ extension Inst on GetInterface { | ||
95 | /// - [clearFactory] clears the callbacks registered by `Get.lazyPut()` | 95 | /// - [clearFactory] clears the callbacks registered by `Get.lazyPut()` |
96 | /// - [clearRouteBindings] clears Instances associated with Routes when using | 96 | /// - [clearRouteBindings] clears Instances associated with Routes when using |
97 | /// [GetMaterialApp]. | 97 | /// [GetMaterialApp]. |
98 | - bool reset( | ||
99 | - {@deprecated bool clearFactory = true, | ||
100 | - @deprecated bool clearRouteBindings = true}) => | ||
101 | - GetInstance().reset( | ||
102 | - // ignore: deprecated_member_use_from_same_package | ||
103 | - clearFactory: clearFactory, | ||
104 | - // ignore: deprecated_member_use_from_same_package | ||
105 | - clearRouteBindings: clearRouteBindings); | 98 | + // bool reset( |
99 | + // {@deprecated bool clearFactory = true, | ||
100 | + // @deprecated bool clearRouteBindings = true}) => | ||
101 | + // GetInstance().reset( | ||
102 | + // // ignore: deprecated_member_use_from_same_package | ||
103 | + // clearFactory: clearFactory, | ||
104 | + // // ignore: deprecated_member_use_from_same_package | ||
105 | + // clearRouteBindings: clearRouteBindings); | ||
106 | 106 | ||
107 | /// Deletes the `Instance<S>`, cleaning the memory and closes any open | 107 | /// Deletes the `Instance<S>`, cleaning the memory and closes any open |
108 | /// controllers (`DisposableInterface`). | 108 | /// controllers (`DisposableInterface`). |
@@ -290,9 +290,8 @@ class GetInstance { | @@ -290,9 +290,8 @@ class GetInstance { | ||
290 | /// [clearFactory] clears the callbacks registered by [lazyPut] | 290 | /// [clearFactory] clears the callbacks registered by [lazyPut] |
291 | /// [clearRouteBindings] clears Instances associated with routes. | 291 | /// [clearRouteBindings] clears Instances associated with routes. |
292 | /// | 292 | /// |
293 | - bool reset( | ||
294 | - {@deprecated bool clearFactory = true, | ||
295 | - @deprecated bool clearRouteBindings = true}) { | 293 | + bool resetInstance( |
294 | + {@deprecated bool clearFactory = true, bool clearRouteBindings = true}) { | ||
296 | // if (clearFactory) _factory.clear(); | 295 | // if (clearFactory) _factory.clear(); |
297 | // deleteAll(force: true); | 296 | // deleteAll(force: true); |
298 | if (clearRouteBindings) RouterReportManager.clearRouteKeys(); | 297 | if (clearRouteBindings) RouterReportManager.clearRouteKeys(); |
@@ -1014,17 +1014,17 @@ you can only use widgets and widget functions here'''; | @@ -1014,17 +1014,17 @@ you can only use widgets and widget functions here'''; | ||
1014 | Get.log = logWriterCallback; | 1014 | Get.log = logWriterCallback; |
1015 | } | 1015 | } |
1016 | if (defaultPopGesture != null) { | 1016 | if (defaultPopGesture != null) { |
1017 | - getxController.defaultPopGesture = defaultPopGesture; | 1017 | + _getxController.defaultPopGesture = defaultPopGesture; |
1018 | } | 1018 | } |
1019 | if (defaultOpaqueRoute != null) { | 1019 | if (defaultOpaqueRoute != null) { |
1020 | - getxController.defaultOpaqueRoute = defaultOpaqueRoute; | 1020 | + _getxController.defaultOpaqueRoute = defaultOpaqueRoute; |
1021 | } | 1021 | } |
1022 | if (defaultTransition != null) { | 1022 | if (defaultTransition != null) { |
1023 | - getxController.defaultTransition = defaultTransition; | 1023 | + _getxController.defaultTransition = defaultTransition; |
1024 | } | 1024 | } |
1025 | 1025 | ||
1026 | if (defaultDurationTransition != null) { | 1026 | if (defaultDurationTransition != null) { |
1027 | - getxController.defaultTransitionDuration = defaultDurationTransition; | 1027 | + _getxController.defaultTransitionDuration = defaultDurationTransition; |
1028 | } | 1028 | } |
1029 | } | 1029 | } |
1030 | 1030 | ||
@@ -1050,18 +1050,18 @@ you can only use widgets and widget functions here'''; | @@ -1050,18 +1050,18 @@ you can only use widgets and widget functions here'''; | ||
1050 | engine!.performReassemble(); | 1050 | engine!.performReassemble(); |
1051 | } | 1051 | } |
1052 | 1052 | ||
1053 | - void appUpdate() => getxController.update(); | 1053 | + void appUpdate() => _getxController.update(); |
1054 | 1054 | ||
1055 | void changeTheme(ThemeData theme) { | 1055 | void changeTheme(ThemeData theme) { |
1056 | - getxController.setTheme(theme); | 1056 | + _getxController.setTheme(theme); |
1057 | } | 1057 | } |
1058 | 1058 | ||
1059 | void changeThemeMode(ThemeMode themeMode) { | 1059 | void changeThemeMode(ThemeMode themeMode) { |
1060 | - getxController.setThemeMode(themeMode); | 1060 | + _getxController.setThemeMode(themeMode); |
1061 | } | 1061 | } |
1062 | 1062 | ||
1063 | GlobalKey<NavigatorState>? addKey(GlobalKey<NavigatorState> newKey) { | 1063 | GlobalKey<NavigatorState>? addKey(GlobalKey<NavigatorState> newKey) { |
1064 | - return getxController.addKey(newKey); | 1064 | + return _getxController.addKey(newKey); |
1065 | } | 1065 | } |
1066 | 1066 | ||
1067 | GlobalKey<NavigatorState>? nestedKey(dynamic key) { | 1067 | GlobalKey<NavigatorState>? nestedKey(dynamic key) { |
@@ -1206,45 +1206,49 @@ you can only use widgets and widget functions here'''; | @@ -1206,45 +1206,49 @@ you can only use widgets and widget functions here'''; | ||
1206 | // /// give access to Immutable MediaQuery.of(context).size.width | 1206 | // /// give access to Immutable MediaQuery.of(context).size.width |
1207 | // double get width => MediaQuery.of(context).size.width; | 1207 | // double get width => MediaQuery.of(context).size.width; |
1208 | 1208 | ||
1209 | - GlobalKey<NavigatorState> get key => getxController.key; | 1209 | + GlobalKey<NavigatorState> get key => _getxController.key; |
1210 | 1210 | ||
1211 | - Map<dynamic, GlobalKey<NavigatorState>> get keys => getxController.keys; | 1211 | + Map<dynamic, GlobalKey<NavigatorState>> get keys => _getxController.keys; |
1212 | 1212 | ||
1213 | - GetMaterialController get rootController => getxController; | 1213 | + GetMaterialController get rootController => _getxController; |
1214 | 1214 | ||
1215 | - bool get defaultPopGesture => getxController.defaultPopGesture; | ||
1216 | - bool get defaultOpaqueRoute => getxController.defaultOpaqueRoute; | 1215 | + bool get defaultPopGesture => _getxController.defaultPopGesture; |
1216 | + bool get defaultOpaqueRoute => _getxController.defaultOpaqueRoute; | ||
1217 | 1217 | ||
1218 | - Transition? get defaultTransition => getxController.defaultTransition; | 1218 | + Transition? get defaultTransition => _getxController.defaultTransition; |
1219 | 1219 | ||
1220 | Duration get defaultTransitionDuration { | 1220 | Duration get defaultTransitionDuration { |
1221 | - return getxController.defaultTransitionDuration; | 1221 | + return _getxController.defaultTransitionDuration; |
1222 | } | 1222 | } |
1223 | 1223 | ||
1224 | - Curve get defaultTransitionCurve => getxController.defaultTransitionCurve; | 1224 | + Curve get defaultTransitionCurve => _getxController.defaultTransitionCurve; |
1225 | 1225 | ||
1226 | Curve get defaultDialogTransitionCurve { | 1226 | Curve get defaultDialogTransitionCurve { |
1227 | - return getxController.defaultDialogTransitionCurve; | 1227 | + return _getxController.defaultDialogTransitionCurve; |
1228 | } | 1228 | } |
1229 | 1229 | ||
1230 | Duration get defaultDialogTransitionDuration { | 1230 | Duration get defaultDialogTransitionDuration { |
1231 | - return getxController.defaultDialogTransitionDuration; | 1231 | + return _getxController.defaultDialogTransitionDuration; |
1232 | } | 1232 | } |
1233 | 1233 | ||
1234 | - Routing get routing => getxController.routing; | 1234 | + Routing get routing => _getxController.routing; |
1235 | 1235 | ||
1236 | - Map<String, String?> get parameters => getxController.parameters; | 1236 | + Map<String, String?> get parameters => _getxController.parameters; |
1237 | set parameters(Map<String, String?> newParameters) => | 1237 | set parameters(Map<String, String?> newParameters) => |
1238 | - getxController.parameters = newParameters; | 1238 | + _getxController.parameters = newParameters; |
1239 | 1239 | ||
1240 | - CustomTransition? get customTransition => getxController.customTransition; | 1240 | + CustomTransition? get customTransition => _getxController.customTransition; |
1241 | set customTransition(CustomTransition? newTransition) => | 1241 | set customTransition(CustomTransition? newTransition) => |
1242 | - getxController.customTransition = newTransition; | 1242 | + _getxController.customTransition = newTransition; |
1243 | 1243 | ||
1244 | - bool get testMode => getxController.testMode; | ||
1245 | - set testMode(bool isTest) => getxController.testMode = isTest; | 1244 | + bool get testMode => _getxController.testMode; |
1245 | + set testMode(bool isTest) => _getxController.testMode = isTest; | ||
1246 | 1246 | ||
1247 | - static GetMaterialController getxController = GetMaterialController(); | 1247 | + void resetRootNavigator() { |
1248 | + _getxController = GetMaterialController(); | ||
1249 | + } | ||
1250 | + | ||
1251 | + static GetMaterialController _getxController = GetMaterialController(); | ||
1248 | } | 1252 | } |
1249 | 1253 | ||
1250 | extension NavTwoExt on GetInterface { | 1254 | extension NavTwoExt on GetInterface { |
@@ -1252,6 +1256,10 @@ extension NavTwoExt on GetInterface { | @@ -1252,6 +1256,10 @@ extension NavTwoExt on GetInterface { | ||
1252 | routeTree.addRoutes(getPages); | 1256 | routeTree.addRoutes(getPages); |
1253 | } | 1257 | } |
1254 | 1258 | ||
1259 | + void clearRouteTree() { | ||
1260 | + _routeTree.routes.clear(); | ||
1261 | + } | ||
1262 | + | ||
1255 | static late final _routeTree = ParseRouteTree(routes: []); | 1263 | static late final _routeTree = ParseRouteTree(routes: []); |
1256 | 1264 | ||
1257 | ParseRouteTree get routeTree => _routeTree; | 1265 | ParseRouteTree get routeTree => _routeTree; |
@@ -57,8 +57,7 @@ class GetDelegate extends RouterDelegate<GetNavConfig> | @@ -57,8 +57,7 @@ class GetDelegate extends RouterDelegate<GetNavConfig> | ||
57 | final List<NavigatorObserver>? navigatorObservers; | 57 | final List<NavigatorObserver>? navigatorObservers; |
58 | final TransitionDelegate<dynamic>? transitionDelegate; | 58 | final TransitionDelegate<dynamic>? transitionDelegate; |
59 | 59 | ||
60 | - GlobalKey<NavigatorState> get navigatorKey => | ||
61 | - GetNavigation.getxController.key; | 60 | + GlobalKey<NavigatorState> get navigatorKey => Get.key; |
62 | 61 | ||
63 | GetDelegate({ | 62 | GetDelegate({ |
64 | GetPage? notFoundRoute, | 63 | GetPage? notFoundRoute, |
@@ -34,6 +34,7 @@ class RouterReportManager<T> { | @@ -34,6 +34,7 @@ class RouterReportManager<T> { | ||
34 | 34 | ||
35 | static void clearRouteKeys() { | 35 | static void clearRouteKeys() { |
36 | _routesKey.clear(); | 36 | _routesKey.clear(); |
37 | + _routesByCreate.clear(); | ||
37 | } | 38 | } |
38 | 39 | ||
39 | static void appendRouteByCreate(GetLifeCycleBase i) { | 40 | static void appendRouteByCreate(GetLifeCycleBase i) { |
@@ -96,6 +96,10 @@ extension LocalesIntl on GetInterface { | @@ -96,6 +96,10 @@ extension LocalesIntl on GetInterface { | ||
96 | translations.addAll(tr); | 96 | translations.addAll(tr); |
97 | } | 97 | } |
98 | 98 | ||
99 | + void clearTranslations() { | ||
100 | + translations.clear(); | ||
101 | + } | ||
102 | + | ||
99 | void appendTranslations(Map<String, Map<String, String>> tr) { | 103 | void appendTranslations(Map<String, Map<String, String>> tr) { |
100 | tr.forEach((key, map) { | 104 | tr.forEach((key, map) { |
101 | if (translations.containsKey(key)) { | 105 | if (translations.containsKey(key)) { |
1 | name: get | 1 | name: get |
2 | description: Open screens/snackbars/dialogs without context, manage states and inject dependencies easily with GetX. | 2 | description: Open screens/snackbars/dialogs without context, manage states and inject dependencies easily with GetX. |
3 | -version: 4.3.3 | 3 | +version: 4.3.4 |
4 | homepage: https://github.com/jonataslaw/getx | 4 | homepage: https://github.com/jonataslaw/getx |
5 | 5 | ||
6 | environment: | 6 | environment: |
-
Please register or login to post a comment