-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated unit tests for all AsyncNotifier subclasses
# Conflicts: # ecommerce_app/test/src/features/authentication/presentation/account/account_screen_controller_test.dart # Conflicts: # ecommerce_app/test/src/features/cart/presentation/add_to_cart/add_to_cart_controller_test.dart
- Loading branch information
Showing
7 changed files
with
410 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,10 @@ import 'package:mocktail/mocktail.dart'; | |
import '../../../../mocks.dart'; | ||
|
||
void main() { | ||
const testEmail = '[email protected]'; | ||
const testPassword = '1234'; | ||
const testFormType = EmailPasswordSignInFormType.signIn; | ||
|
||
ProviderContainer makeProviderContainer(MockAuthRepository authRepository) { | ||
final container = ProviderContainer( | ||
overrides: [ | ||
|
@@ -19,9 +23,9 @@ void main() { | |
return container; | ||
} | ||
|
||
const testEmail = '[email protected]'; | ||
const testPassword = '1234'; | ||
const testFormType = EmailPasswordSignInFormType.signIn; | ||
setUpAll(() { | ||
registerFallbackValue(const AsyncLoading<int>()); | ||
}); | ||
|
||
group('EmailPasswordSignInController', () { | ||
test('sign in success', () async { | ||
|
@@ -32,24 +36,32 @@ void main() { | |
testPassword, | ||
)).thenAnswer((_) => Future.value()); | ||
final container = makeProviderContainer(authRepository); | ||
final controller = | ||
container.read(emailPasswordSignInControllerProvider.notifier); | ||
// expect later | ||
expectLater( | ||
controller.stream, | ||
emitsInOrder([ | ||
const AsyncLoading<void>(), | ||
const AsyncData<void>(null), | ||
]), | ||
final listener = Listener<AsyncValue<void>>(); | ||
container.listen( | ||
emailPasswordSignInControllerProvider, | ||
listener.call, | ||
fireImmediately: true, | ||
); | ||
const data = AsyncData<void>(null); | ||
// verify initial value from build method | ||
verify(() => listener(null, data)); | ||
// run | ||
final controller = | ||
container.read(emailPasswordSignInControllerProvider.notifier); | ||
final result = await controller.submit( | ||
email: testEmail, | ||
password: testPassword, | ||
formType: testFormType, | ||
); | ||
// verify | ||
expect(result, true); | ||
verifyInOrder([ | ||
// set loading state | ||
() => listener(data, any(that: isA<AsyncLoading>())), | ||
// data when complete | ||
() => listener(any(that: isA<AsyncLoading>()), data), | ||
]); | ||
verifyNoMoreInteractions(listener); | ||
}); | ||
test('sign in failure', () async { | ||
// setup | ||
|
@@ -60,27 +72,32 @@ void main() { | |
testPassword, | ||
)).thenThrow(exception); | ||
final container = makeProviderContainer(authRepository); | ||
final controller = | ||
container.read(emailPasswordSignInControllerProvider.notifier); | ||
// expect later | ||
expectLater( | ||
controller.stream, | ||
emitsInOrder([ | ||
const AsyncLoading<void>(), | ||
predicate<AsyncValue<void>>((state) { | ||
expect(state.hasError, true); | ||
return true; | ||
}), | ||
]), | ||
final listener = Listener<AsyncValue<void>>(); | ||
container.listen( | ||
emailPasswordSignInControllerProvider, | ||
listener.call, | ||
fireImmediately: true, | ||
); | ||
// verify initial value from build method | ||
verify(() => listener(null, const AsyncData<void>(null))); | ||
// run | ||
final controller = | ||
container.read(emailPasswordSignInControllerProvider.notifier); | ||
final result = await controller.submit( | ||
email: testEmail, | ||
password: testPassword, | ||
formType: testFormType, | ||
); | ||
// verify | ||
expect(result, false); | ||
verifyInOrder([ | ||
// set loading state | ||
() => listener( | ||
const AsyncData<void>(null), any(that: isA<AsyncLoading>())), | ||
// error when complete | ||
() => listener( | ||
any(that: isA<AsyncLoading>()), any(that: isA<AsyncError>())), | ||
]); | ||
}); | ||
}); | ||
} |
Oops, something went wrong.