Line data Source code
1 : import 'package:flutter/material.dart';
2 : import 'package:flutter_redux/flutter_redux.dart';
3 : import 'package:redfire/types.dart';
4 : import 'package:redux/redux.dart';
5 :
6 : import '../test-doubles/redux/fake_store.dart';
7 :
8 : /// A test harness to wrap a widget under test and provide all the functionality
9 : /// that a test may want in order to interact with the widget or check for
10 : /// expected values and behaviour.
11 : ///
12 : /// Currently, calling .receivedActions when [_store] is not a [FakeStore] is
13 : /// a runtime error - I think we can do better.
14 : class WidgetTestHarness<T extends RedFireState> {
15 : final Store<T> _store;
16 : final Widget _widgetUnderTest;
17 :
18 1 : WidgetTestHarness.withFakeStore(
19 : {required T initialState, required Widget widgetUnderTest})
20 1 : : _store = FakeStore(initialState),
21 : _widgetUnderTest = widgetUnderTest;
22 :
23 0 : WidgetTestHarness.withStore(
24 : {required Store<T> initializedStore, required Widget widgetUnderTest})
25 : : _store = initializedStore,
26 : _widgetUnderTest = widgetUnderTest;
27 :
28 0 : Widget get widget => StoreProvider<T>(
29 0 : store: _store,
30 0 : child: MaterialApp(home: Scaffold(body: _widgetUnderTest)));
31 :
32 0 : T get state => _store.state;
33 :
34 1 : List<ReduxAction> get receivedActions {
35 2 : if (_store is! FakeStore) {
36 0 : throw Exception(
37 : 'You tried to access `dispatched` on a real `Store`. \n\n Use `WidgetTestHarness.withFakeStore` if you want to access `dispatchedActions`.');
38 : }
39 2 : return (_store as FakeStore).dispatched;
40 : }
41 : }
|