Skip to content

Commit

Permalink
Feat: Add method handleError for emulating a call of onErrorHandle in…
Browse files Browse the repository at this point in the history
… WM tests. (#119)

* Feat: add method for handling error emulation.
Docs: update docs of WMTester.

* Feat: WMTester.handleError calls WidgetModel.onErrorHandle.

* Chore: add information to changelog.
  • Loading branch information
MbIXjkee authored Nov 3, 2024
1 parent 4a8ac13 commit 3142935
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
5 changes: 4 additions & 1 deletion packages/elementary_test/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# Changelog

## Unreleased
# Added
### Added
* Mocked context is mounted by default.
* Method handleError for emulating a call of onErrorHandle in WM tests.
### Changed
* WMTester documentation updated.

## 2.0.1
### Changed
Expand Down
47 changes: 40 additions & 7 deletions packages/elementary_test/lib/src/widget_model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,53 @@ void testWidgetModel<WM extends WidgetModel, W extends ElementaryWidget>(
/// Interface for emulating BuildContext behaviour.
class WMContext extends Mock implements BuildContext {}

/// Interface for control wm stage in test.
/// Interface for controlling WidgetModel's stage during tests.
/// Every method in this interface represents possible changes with Widget Model
/// and allows to emulate this happened.
///
/// For additional information check the lifecycle of the [WidgetModel].
abstract class WMTester<WM extends WidgetModel, W extends ElementaryWidget> {
/// Initialize widget model to work.
/// Emulates initializing WidgetModel to work.
///
/// Represents processes happen with WidgetModel when it is inseted into the
/// tree, before the first build of the subtree.
void init({W? initWidget});

/// Change the widget used to configure this element.
/// Emulates changing a widget instance associated
/// with the according Elementary.
void update(W newWidget);

/// Called when a dependency of this element changes.
/// Emulates changing of dependencies WidgetModel has subscribed
/// by BuildContext.
void didChangeDependencies();

/// Transition from the "inactive" to the "active" lifecycle state.
/// Emulates the transition from the "inactive"
/// to the "active" lifecycle state.
///
/// In real work happens when the WidgetModel is reinserted into
/// the tree after having been removed via [deactivate].
void activate();

/// Transition from the "active" to the "inactive" lifecycle state.
/// Emulates the transition from the "active"
/// to the "inactive" lifecycle state.
///
/// In real work happens when the WidgetModel is removed from the tree.
void deactivate();

/// Transition from the "inactive" to the "defunct" lifecycle state.
/// Emulates the transition from the "inactive"
/// to the "defunct" lifecycle state.
///
/// In real work happens when the WidgetModel is removed from the
/// tree permanently.
/// See also:
/// [WidgetModel.dispose];
void unmount();

/// Emulates notification WidgetModel about an error from the Model.
///
/// See also:
/// [WidgetModel.onErrorHandle];
void handleError(Object error);
}

class _WMTestableElement<WM extends WidgetModel, W extends ElementaryWidget>
Expand Down Expand Up @@ -141,4 +169,9 @@ class _WMTestableElement<WM extends WidgetModel, W extends ElementaryWidget>
..setupTestElement(null)
..setupTestWidget(null);
}

@override
void handleError(Object error) {
_wm.handleTestError(error);
}
}
2 changes: 1 addition & 1 deletion packages/elementary_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ dependencies:
sdk: flutter
flutter_test:
sdk: flutter
elementary: ^3.0.0
elementary: ^3.1.2
mocktail: ^1.0.0
meta: ^1.8.0

Expand Down

0 comments on commit 3142935

Please sign in to comment.