// ignore_for_file: avoid_classes_with_only_static_members import 'package:flutter_test/flutter_test.dart'; import 'package:get/get.dart'; import 'util/matcher.dart' as m; class Mock { static Future test() async { await Future.delayed(Duration.zero); return 'test'; } } abstract class MyController extends GetLifeCycle {} class DisposableController extends MyController {} // 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(Mock.test); expect('test', Get.find()); Get.reset(); }); test('Get.put test', () async { final instance = Get.put(Controller()); expect(instance, Get.find()); Get.reset(); }); test('Get start and delete called just one time', () async { Get ..put(Controller()) ..put(Controller()); final controller = Get.find(); expect(controller.init, 1); Get ..delete() ..delete(); expect(controller.close, 1); Get.reset(); }); test('Get.put tag test', () async { final instance = Get.put(Controller(), tag: 'one'); final instance2 = Get.put(Controller(), tag: 'two'); expect(instance == instance2, false); expect(Get.find(tag: 'one') == Get.find(tag: 'two'), false); expect(Get.find(tag: 'one') == Get.find(tag: 'one'), true); expect(Get.find(tag: 'two') == Get.find(tag: 'two'), true); Get.reset(); }); test('Get.lazyPut tag test', () async { Get.lazyPut(Controller.new, tag: 'one'); Get.lazyPut(Controller.new, tag: 'two'); expect(Get.find(tag: 'one') == Get.find(tag: 'two'), false); expect(Get.find(tag: 'one') == Get.find(tag: 'one'), true); expect(Get.find(tag: 'two') == Get.find(tag: 'two'), true); Get.reset(); }); test('Get.lazyPut test', () async { final controller = Controller(); Get.lazyPut(() => controller); final ct1 = Get.find(); expect(ct1, controller); Get.reset(); }); test('Get.lazyPut fenix test', () async { Get.lazyPut(Controller.new, fenix: true); Get.find().increment(); expect(Get.find().count, 1); Get.delete(); expect(Get.find().count, 0); Get.reset(); }); test('Get.lazyPut without fenix', () async { Get.lazyPut(Controller.new); Get.find().increment(); expect(Get.find().count, 1); Get.delete(); expect( () => Get.find(), throwsA(const m.TypeMatcher())); Get.reset(); }); test('Get.reloadInstance test', () async { Get.lazyPut(Controller.new); var ct1 = Get.find(); ct1.increment(); expect(ct1.count, 1); ct1 = Get.find(); expect(ct1.count, 1); GetInstance().reload(); ct1 = Get.find(); expect(ct1.count, 0); Get.reset(); }); test('GetxService test', () async { Get.lazyPut(PermanentService.new); var sv1 = Get.find(); var sv2 = Get.find(); expect(sv1, sv2); expect(Get.isRegistered(), true); Get.delete(); expect(Get.isRegistered(), true); Get.delete(force: true); expect(Get.isRegistered(), false); Get.reset(); }); test('Get.lazyPut with abstract class test', () async { final api = Api(); Get.lazyPut(() => api); final ct1 = Get.find(); expect(ct1, api); Get.reset(); }); test('Get.create with abstract class test', () async { Get.create(Api.new); final ct1 = Get.find(); final ct2 = Get.find(); // 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()); expect(instance.initialized, true); }); test('Get.delete test with disposable controller', () async { // Get.put(DisposableController()); expect(await Get.delete(), true); expect(() => Get.find(), throwsA(const m.TypeMatcher())); }); test('Get.put test after delete with disposable controller and init check', () async { final instance = Get.put(DisposableController()); expect(instance, Get.find()); expect(instance.initialized, true); }); }); group('Get.replace test for replacing parent instance that is', () { tearDown(Get.reset); test('temporary', () async { Get.put(DisposableController()); Get.replace(Controller()); final instance = Get.find(); expect(instance is Controller, isTrue); expect((instance as Controller).init, greaterThan(0)); }); test('permanent', () async { Get.put(DisposableController(), permanent: true); Get.replace(Controller()); final instance = Get.find(); expect(instance is Controller, isTrue); expect((instance as Controller).init, greaterThan(0)); }); test('tagged temporary', () async { const tag = 'tag'; Get.put(DisposableController(), tag: tag); Get.replace(Controller(), tag: tag); final instance = Get.find(tag: tag); expect(instance is Controller, isTrue); expect((instance as Controller).init, greaterThan(0)); }); test('tagged permanent', () async { const tag = 'tag'; Get.put(DisposableController(), permanent: true, tag: tag); Get.replace(Controller(), tag: tag); final instance = Get.find(tag: tag); expect(instance is Controller, isTrue); expect((instance as Controller).init, greaterThan(0)); }); test('a generic parent type', () async { const tag = 'tag'; Get.put(DisposableController(), permanent: true, tag: tag); Get.replace(Controller(), tag: tag); final instance = Get.find(tag: tag); expect(instance is Controller, isTrue); expect((instance as Controller).init, greaterThan(0)); }); }); group('Get.lazyReplace replaces parent instance', () { tearDown(Get.reset); test('without fenix', () async { Get.put(DisposableController()); Get.lazyReplace(Controller.new); final instance = Get.find(); expect(instance, isA()); expect((instance as Controller).init, greaterThan(0)); }); test('with fenix', () async { Get.put(DisposableController()); Get.lazyReplace(Controller.new, fenix: true); expect(Get.find(), isA()); (Get.find() as Controller).increment(); expect((Get.find() as Controller).count, 1); Get.delete(); expect((Get.find() as Controller).count, 0); }); test('with fenix when parent is permanent', () async { Get.put(DisposableController(), permanent: true); Get.lazyReplace(Controller.new); final instance = Get.find(); expect(instance, isA()); (instance as Controller).increment(); expect((Get.find() as Controller).count, 1); Get.delete(); expect((Get.find() as Controller).count, 0); }); }); } class PermanentService extends GetxService {} 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++; } }