

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:fotodocumentation/utils/global_router.dart';
import 'package:fotodocumentation/utils/login_credentials.dart';
import 'package:mockito/mockito.dart';

import 'package:fotodocumentation/controller/customer_controller.dart';
import 'package:fotodocumentation/dto/customer_dto.dart';
import 'package:fotodocumentation/utils/di_container.dart';

import '../testing/test_utils.dart';
import '../testing/test_utils.mocks.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();
  DiContainer.instance.initState();
  DiContainer.instance.put(LoginCredentials, getDefaultLoginCredentials());
  group('Customer List Test', () {
    testWidgets('Customer list screen search test', (WidgetTester tester) async {
      await _searchtest(tester);
    });
  });
}

List<CustomerListDto> _list = [CustomerListDto(id: 1, customerNumber: "CODE1", name: "Customer 1"), CustomerListDto(id: 2, customerNumber: "CODE2", name: "Customer 2")];

Future<void> _searchtest(WidgetTester tester) async {
  setScreenSize(tester, 1024, 1024);
  String searchText = 'Henk';

  var controller = MockCustomerController();
  DiContainer.instance.put(CustomerController, controller);

  when(controller.getAll("", "")).thenAnswer((_) async => _list);
  when(controller.getAll(searchText, "")).thenAnswer((_) async => [_list.first]);

  await pumpAppConfig(tester, GlobalRouter.pathHome);
  verify(controller.getAll(argThat(equals("")), argThat(equals("")))).called(1);

  await tester.enterText(find.byKey(Key("Search_text_field")), searchText);

  await tester.testTextInput.receiveAction(TextInputAction.done);

  await tester.pumpAndSettle();
  verify(controller.getAll(searchText, "")).called(1);
}