Last active
January 21, 2021 10:02
-
-
Save normancarcamo/1ba181ce88cbecf458cbf5737cc17d99 to your computer and use it in GitHub Desktop.
Redux example for dart & flutter with testing
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
import 'package:equatable/equatable.dart'; | |
import 'package:flutter/material.dart'; | |
import 'package:flutter_redux/flutter_redux.dart'; | |
import 'package:flutter_test/flutter_test.dart'; | |
import 'package:meta/meta.dart'; | |
import 'package:redux/redux.dart'; | |
/// STATE: | |
@immutable | |
class AppState extends Equatable { | |
final bool isInternetAvailable; | |
AppState({this.isInternetAvailable}); | |
factory AppState.initial() { | |
return AppState(isInternetAvailable: false); | |
} | |
AppState copyWith({bool isInternetAvailable}) { | |
return AppState( | |
isInternetAvailable: isInternetAvailable ?? this.isInternetAvailable, | |
); | |
} | |
@override | |
List<Object> get props => [this.isInternetAvailable]; | |
@override | |
bool get stringify => true; | |
} | |
/// ACTIONS: | |
enum InternetAction { IS_ONLINE, IS_OFFLINE } | |
/// REDUCERS: | |
bool internetReducer(bool state, InternetAction action) { | |
if (action is InternetAction) { | |
return action == InternetAction.IS_ONLINE; | |
} else { | |
return state; | |
} | |
} | |
AppState appReducer(AppState state, action) { | |
return state.copyWith( | |
isInternetAvailable: internetReducer(state.isInternetAvailable, action), | |
); | |
} | |
/// UI: | |
class InternetCheckerWidget extends StatelessWidget { | |
@override | |
Widget build(BuildContext context) { | |
return Scaffold( | |
appBar: AppBar(), | |
body: StoreConnector<AppState, bool>( | |
converter: (store) => store.state.isInternetAvailable, | |
builder: (context, isInternetAvailable) { | |
final String status = isInternetAvailable ? 'ONLINE' : 'OFFLINE'; | |
return Text('Status: $status'); | |
}, | |
), | |
); | |
} | |
} | |
/// WIDGET TESTING: | |
void main() { | |
final store = Store<AppState>(appReducer, initialState: AppState.initial()); | |
Widget buildApp() { | |
return MaterialApp( | |
home: StoreProvider<AppState>( | |
store: store, | |
child: InternetCheckerWidget(), | |
), | |
); | |
} | |
testWidgets('builds successfully', (WidgetTester tester) async { | |
await tester.pumpWidget(buildApp()); | |
expect(find.byType(Text), findsOneWidget); | |
}); | |
testWidgets('"Status: ONLINE" when online', (WidgetTester tester) async { | |
store.dispatch(InternetAction.IS_ONLINE); | |
await tester.pumpWidget(buildApp()); | |
expect(find.text('Status: ONLINE'), findsOneWidget); | |
}); | |
testWidgets('"Status: OFFLINE" when offline', (WidgetTester tester) async { | |
store.dispatch(InternetAction.IS_OFFLINE); | |
await tester.pumpWidget(buildApp()); | |
expect(find.text('Status: OFFLINE'), findsOneWidget); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment