> For the complete documentation index, see [llms.txt](https://gdgaddis.gitbook.io/mobile-development-reference-tce-c01/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gdgaddis.gitbook.io/mobile-development-reference-tce-c01/testing/widget.md).

# Widget Testing

&#x20;Widget testing is otherwise called **component testing**. As its name proposes, it is utilized for testing a single widget, and the objective of this test is to check whether the widget works and looks true to form.

Widget testing proves to be useful when we are trying the particular widgets. On the off chance that the Text widget doesn’t contain the text, at that point, Widget testing throws an error expressing that the Text widget doesn’t have a text inside it. We likewise don’t host to install any third-party dependencies for widget testing in Flutter.

Implementation:

> *Steps to Implement Widget Testing*

**Step 1: Add the dependencies**

> *Add the `flutter_test` dependency to pubspec — yaml file.*

```dart
dev_dependencies:
  flutter_test:
    sdk: flutter
```

&#x20;**Step 2:** Create a file "product\_screen\_test.dart" in the directory **test/product/view/products/** with the following code.

```dart
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:techamp_flutter_shopping_app/app.dart';

void main() {
  group('ProductsScreen', () {
    ProductsCubit productsCubit;

    setUp(() {
      productsCubit = _MockProductsCubit();
      getIt.registerFactory(() => productsCubit);
    });

    tearDown(() {
      productsCubit?.close();
      getIt.reset();
    });

    testWidgets('should show progress indicator on initial view',
        (tester) async {
      when(productsCubit.state).thenReturn(const InitialProductsState());
      await tester.pumpWidget(
        const MaterialApp(
          home: ProductsScreen(),
        ),
      );

      expect(find.byType(CircularProgressIndicator), findsOneWidget);
    });

    testWidgets('should show products when loaded successfully',
        (tester) async {
      const products = [
        Product(
          id: 1,
          title: 'Fjallraven',
          image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg',
          price: 109.95,
          description: 'Your perfect pack for everyday use.',
          category: MenClothingProductCategory(),
        ),
        Product(
          id: 2,
          title: 'Mens Casual Premium T-Shirts',
          image:
              'https://fakestoreapi.com/img/71-3HjGNDUL._AC_SY879._SX._UX._SY._UY_.jpg',
          price: 22.5,
          description: 'Slim-fitting style.',
          category: MenClothingProductCategory(),
        )
      ];
      when(productsCubit.state)
          .thenReturn(const ProductsLoadedState(products: products));

      // Fixes Image.network network exception
      HttpOverrides.global = null;

      await tester.pumpWidget(
        const MaterialApp(
          home: ProductsScreen(),
        ),
      );

      expect(find.text(products.first.title), findsOneWidget);
      expect(find.text('${products.first.price} ETB'), findsOneWidget);

      expect(find.text(products.last.title), findsOneWidget);
      expect(find.text('${products.last.price} ETB'), findsOneWidget);
    });
  });
}

class _MockProductsCubit extends Mock implements ProductsCubit {}

```
