Skip to content

Commit

Permalink
chore: add test data (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethan-tbd authored Jun 14, 2024
1 parent e5b8507 commit 63f82c7
Show file tree
Hide file tree
Showing 21 changed files with 324 additions and 351 deletions.
2 changes: 1 addition & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -812,7 +812,7 @@ packages:
source: hosted
version: "1.3.2"
typeid:
dependency: transitive
dependency: "direct dev"
description:
name: typeid
sha256: "1e4beac12e6cdf65299bdc458a44e3a2a4b2b0bd88ca93bee81011ab4e1133cd"
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dev_dependencies:
mocktail: ^1.0.2
flutter_launcher_icons: ^0.13.1
json_schema: ^5.1.7
typeid: ^1.0.1

flutter:
uses-material-design: true
Expand Down
7 changes: 3 additions & 4 deletions test/features/account/account_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';

void main() async {
const dap = '[email protected]';

void main() {
late MockPfisNotifier mockPfisNotifier;
late MockVcsNotifier mockVcsNotifier;
late MockFeatureFlagsNotifier mockFeatureFlagsNotifier;
Expand All @@ -36,7 +35,7 @@ void main() async {
testWidgets('should show DAP', (tester) async {
await tester.pumpWidget(accountPageTestWidget());

expect(find.text(dap), findsOneWidget);
expect(find.text(TestData.dap), findsOneWidget);
});

testWidgets('should show linked pfis', (tester) async {
Expand Down
22 changes: 10 additions & 12 deletions test/features/app/app_tabs_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import 'package:didpay/features/account/account_balance.dart';
import 'package:didpay/features/account/account_balance_notifier.dart';
import 'package:didpay/features/account/account_page.dart';
import 'package:didpay/features/app/app_tabs.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/feature_flags/feature_flags_notifier.dart';
import 'package:didpay/features/home/home_page.dart';
import 'package:didpay/features/pfis/pfi.dart';
import 'package:didpay/features/pfis/pfis_notifier.dart';
import 'package:didpay/features/send/send_page.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
Expand All @@ -14,16 +12,17 @@ import 'package:didpay/features/vcs/vcs_notifier.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:web5/web5.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';

void main() async {
final did = await DidDht.create();
final accountBalance =
AccountBalance(total: '101', currencyCode: 'USD', balancesMap: {});
const pfi = Pfi(did: 'did:web:x%3A8892:ingress');
await TestData.initializeDids();

final did = TestData.aliceDid;
final pfis = TestData.getPfis();
final accountBalance = TestData.getAccountBalance();

late MockTbdexService mockTbdexService;
late MockPfisNotifier mockPfisNotifier;
Expand All @@ -32,19 +31,18 @@ void main() async {

setUp(() {
mockTbdexService = MockTbdexService();
mockPfisNotifier = MockPfisNotifier([pfi]);
mockPfisNotifier = MockPfisNotifier(pfis);
mockVcsNotifier = MockVcsNotifier([]);
mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]);

when(
() => mockTbdexService.getExchanges(did, [pfi]),
() => mockTbdexService.getExchanges(did, pfis),
).thenAnswer((_) async => {});

when(
() => mockTbdexService.getAccountBalance([pfi]),
() => mockTbdexService.getAccountBalance(pfis),
).thenAnswer(
(_) async =>
AccountBalance(total: '0', currencyCode: 'USD', balancesMap: {}),
(_) async => accountBalance,
);
});

Expand Down
19 changes: 9 additions & 10 deletions test/features/app/app_test.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import 'package:didpay/features/account/account_balance.dart';
import 'package:didpay/features/app/app_tabs.dart';
import 'package:didpay/features/did/did_provider.dart';
import 'package:didpay/features/feature_flags/feature_flags_notifier.dart';
import 'package:didpay/features/pfis/pfi.dart';
import 'package:didpay/features/pfis/pfis_notifier.dart';
import 'package:didpay/features/tbdex/tbdex_service.dart';
import 'package:didpay/features/transaction/transaction_notifier.dart';
import 'package:didpay/features/vcs/vcs_notifier.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:web5/web5.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';

void main() async {
final did = await DidDht.create();
const pfi = Pfi(did: 'did:web:x%3A8892:ingress');
await TestData.initializeDids();

final did = TestData.aliceDid;
final pfis = TestData.getPfis();

late MockTbdexService mockTbdexService;
late MockPfisNotifier mockPfisNotifier;
Expand All @@ -25,19 +25,18 @@ void main() async {

setUp(() {
mockTbdexService = MockTbdexService();
mockPfisNotifier = MockPfisNotifier([pfi]);
mockPfisNotifier = MockPfisNotifier(pfis);
mockVcsNotifier = MockVcsNotifier([]);
mockFeatureFlagsNotifier = MockFeatureFlagsNotifier([]);

when(
() => mockTbdexService.getExchanges(did, [pfi]),
() => mockTbdexService.getExchanges(did, pfis),
).thenAnswer((_) async => {});

when(
() => mockTbdexService.getAccountBalance([pfi]),
() => mockTbdexService.getAccountBalance(pfis),
).thenAnswer(
(_) async =>
AccountBalance(total: '0', currencyCode: 'USD', balancesMap: {}),
(_) async => TestData.getAccountBalance(),
);
});

Expand Down
29 changes: 15 additions & 14 deletions test/features/countries/countries_notifier_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:didpay/features/countries/countries.dart';
import 'package:didpay/features/countries/countries_notifier.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';

void main() {
final australia = TestData.getCountry('Australia', 'AU');
final us = TestData.getCountry('United States', 'US');
final initialCountries = [australia];

late MockBox mockBox;

setUp(() {
Expand All @@ -14,22 +18,19 @@ void main() {

group('CountriesNotifier', () {
test('should create and load initial list', () async {
final initialCountries = [const Country(name: 'Mexico', code: 'MXN')];

when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn(
initialCountries.map((country) => country.toJson()).toList(),);
initialCountries.map((country) => country.toJson()).toList(),
);
final notifier = await CountriesNotifier.create(mockBox);

expect(notifier.state, initialCountries);
verify(() => mockBox.get(CountriesNotifier.storageKey)).called(1);
});

test('should add a new Country', () async {
final initialCountries = [const Country(name: 'Mexico', code: 'MXN')];
const newCountry = Country(name: 'United States', code: 'USD');

when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn(
initialCountries.map((country) => country.toJson()).toList(),);
initialCountries.map((country) => country.toJson()).toList(),
);
when(
() => mockBox.put(
CountriesNotifier.storageKey,
Expand All @@ -39,28 +40,28 @@ void main() {

final notifier = await CountriesNotifier.create(mockBox);

final addedCountry = await notifier.add(newCountry);
final addedCountry = await notifier.add(us);

expect(notifier.state, [...initialCountries, newCountry]);
expect(addedCountry, newCountry);
expect(notifier.state, [...initialCountries, us]);
expect(addedCountry, us);

verify(
() => mockBox.put(
CountriesNotifier.storageKey,
[
...initialCountries.map((country) => country.toJson()),
newCountry.toJson(),
us.toJson(),
],
),
).called(1);
});

test('should remove a Country', () async {
final initialCountries = [const Country(name: 'Mexico', code: 'MXN')];
final countryToRemove = initialCountries.first;

when(() => mockBox.get(CountriesNotifier.storageKey)).thenReturn(
initialCountries.map((country) => country.toJson()).toList(),);
initialCountries.map((country) => country.toJson()).toList(),
);
when(() => mockBox.put(CountriesNotifier.storageKey, any()))
.thenAnswer((_) async {});

Expand Down
37 changes: 17 additions & 20 deletions test/features/countries/countries_page_test.dart
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
import 'package:didpay/features/countries/countries.dart';
import 'package:didpay/features/countries/countries_notifier.dart';
import 'package:didpay/features/countries/countries_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';
import '../../helpers/widget_helpers.dart';

void main() {
Widget testableWidget({List<Country> initialCountries = const []}) {
final countriesNotifier = MockCountriesNotifier(initialCountries);

return WidgetHelpers.testableWidget(
child: const CountriesPage(),
overrides: [
countriesProvider.overrideWith((ref) => countriesNotifier),
],
);
}
final australia = TestData.getCountry('Australia', 'AU');
final us = TestData.getCountry('United States', 'US');
final initialCountries = [australia, us];

group('CountriesPage', () {
testWidgets('should show countries', (tester) async {
await tester.pumpWidget(
testableWidget(
initialCountries: const [
Country(name: 'Mexico', code: 'MXN'),
Country(name: 'United States', code: 'USD'),
],
),
Widget countriesPageTestWidget() {
final countriesNotifier = MockCountriesNotifier(initialCountries);

return WidgetHelpers.testableWidget(
child: const CountriesPage(),
overrides: [
countriesProvider.overrideWith((ref) => countriesNotifier),
],
);
}

testWidgets('should show countries', (tester) async {
await tester.pumpWidget(countriesPageTestWidget());

await tester.pumpAndSettle();

expect(find.text('Mexico'), findsOneWidget);
expect(find.text('Australia'), findsOneWidget);
expect(find.text('United States'), findsOneWidget);
});
});
Expand Down
30 changes: 11 additions & 19 deletions test/features/feature_flags/feature_flags_notifier_test.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import 'package:didpay/features/feature_flags/feature_flag.dart';
import 'package:didpay/features/feature_flags/feature_flags_notifier.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';

import '../../helpers/mocks.dart';
import '../../helpers/test_data.dart';

void main() {
final testFlag = TestData.getFeatureFlag('test', 'test description');
final newFlag = TestData.getFeatureFlag('new test', 'new test description');
final initialFeatureFlags = [testFlag];

late MockBox mockBox;

setUp(() {
Expand All @@ -14,10 +18,6 @@ void main() {

group('FeatureFlagsNotifier', () {
test('should create and load initial list', () async {
final initialFeatureFlags = [
const FeatureFlag(name: 'test', description: 'test flag'),
];

when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn(
initialFeatureFlags.map((flag) => flag.toJson()).toList(),
);
Expand All @@ -28,14 +28,9 @@ void main() {
});

test('should add a new FeatureFlag', () async {
final initialFeatureFlags = [
const FeatureFlag(name: 'test', description: 'test flag'),
];
const newFeatureFlag =
FeatureFlag(name: 'new test', description: 'new test flag');

when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn(
initialFeatureFlags.map((flag) => flag.toJson()).toList(),);
initialFeatureFlags.map((flag) => flag.toJson()).toList(),
);
when(
() => mockBox.put(
FeatureFlagsNotifier.storageKey,
Expand All @@ -45,26 +40,23 @@ void main() {

final notifier = await FeatureFlagsNotifier.create(mockBox);

final addedFeatureFlag = await notifier.add(newFeatureFlag);
final addedFeatureFlag = await notifier.add(newFlag);

expect(notifier.state, [...initialFeatureFlags, newFeatureFlag]);
expect(addedFeatureFlag, newFeatureFlag);
expect(notifier.state, [...initialFeatureFlags, newFlag]);
expect(addedFeatureFlag, newFlag);

verify(
() => mockBox.put(
FeatureFlagsNotifier.storageKey,
[
...initialFeatureFlags.map((flag) => flag.toJson()),
newFeatureFlag.toJson(),
newFlag.toJson(),
],
),
).called(1);
});

test('should remove a FeatureFlag', () async {
final initialFeatureFlags = [
const FeatureFlag(name: 'test', description: 'test flag'),
];
final featureFlagToRemove = initialFeatureFlags.first;

when(() => mockBox.get(FeatureFlagsNotifier.storageKey)).thenReturn(
Expand Down
Loading

0 comments on commit 63f82c7

Please sign in to comment.