Skip to content

Commit

Permalink
fix: Make test provider properly reflect state and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rlperez committed Apr 20, 2022
1 parent 5509ded commit cc3c63c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 44 deletions.
21 changes: 15 additions & 6 deletions lib/traits/color_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,26 @@ class ColorWidget extends StatelessWidget {
child: SizedBox(
width: 100,
height: 100,
child: _colorProvider.isPerformingAction
? CircularProgressIndicator()
: UnknownItemIcon(
size: 100,
color: _iconColor,
))),
child: _mainIcon())),
SizedBox(height: 10),
_stateRow(context),
]);
}

Widget _mainIcon() {
if (_colorProvider.isInErrorState) {
return Icon(
Icons.error,
size: 100.0,
color: _iconColor,
);
} else if (_colorProvider.isPerformingAction) {
return CircularProgressIndicator();
} else {
return UnknownItemIcon(size: 100, color: _iconColor);
}
}

Widget _stateRow(BuildContext context) {
final state = _colorProvider.getColorState;
if (state == null) {
Expand Down
32 changes: 19 additions & 13 deletions lib/traits/slim/color_slim_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,26 @@ class ColorSlimWidget extends BaseSlimWidget {
colorModel: ColorModel.hsv,
);
},
child: Row(
children: [
CircleAvatar(
backgroundColor:
_hsbToColor(_colorTraitProvider.getColorState),
radius: 14,
child: SizedBox(
child: SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Row(
children: [
CircleAvatar(
backgroundColor:
_hsbToColor(_colorTraitProvider.getColorState),
radius: 14,
),
Icon(
BootstrapIcons.pencil,
color: WidgetStyleConstants.globalSuccessColor,
size: 14.0,
)
],
),
),
Icon(
BootstrapIcons.pencil,
color: WidgetStyleConstants.globalSuccessColor,
size: 14.0,
)
],
)));
width: 28,
height: 28)));

static Color _hsbToColor(GHSBColorValueInput? hsbColor) {
if (hsbColor == null) {
Expand Down
27 changes: 22 additions & 5 deletions test/mixins/color_testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,32 @@ import 'color_testing.mocks.dart';

@GenerateMocks([ColorProvider])
mixin ColorTesting {
MockColorProvider mockColorProvider(TestColorDevice device) {
MockColorProvider mockColorProvider(TestColorDevice device,
{bool isBusy = false,
bool isLoading = false,
bool isInErrorState = false,
bool isPerformingAction = false,
String errorMessage = ''}) {
final mockColorProvider = MockColorProvider();
when(mockColorProvider.displayName).thenReturn(device.displayName);
when(mockColorProvider.isLoading).thenReturn(isLoading);
when(mockColorProvider.isBusy).thenReturn(isBusy);
when(mockColorProvider.isInErrorState).thenReturn(isInErrorState);
when(mockColorProvider.getErrorMessage).thenReturn(errorMessage);
when(mockColorProvider.isPerformingAction).thenReturn(isPerformingAction);

when(mockColorProvider.deviceDetail).thenReturn(device);
when(mockColorProvider.getColorState).thenReturn(device.colorState?.value);
when(mockColorProvider.setColorAction(any))
.thenAnswer((_) => Future.value(null));
return mockColorProvider;
}
}

class TestColorDevice extends Device {
TestColorDevice(Device device)
final HSBColor? colorState;

TestColorDevice(Device device, {this.colorState})
: super(
device.id,
device.displayName,
Expand All @@ -26,10 +42,11 @@ class TestColorDevice extends Device {
device.serialNumber,
device.createdAt,
device.updatedAt, [
...device.traits // .where((t) => t.runtimeType != ColorTrait)
ColorTrait(colorState ?? HSBColor(130, 50, 50)),
...device.traits.where((t) => t.runtimeType != ColorTrait)
]);

TestColorDevice withColor(param0) {
return this;
TestColorDevice withColor(HSBColor? colorState) {
return TestColorDevice(this, colorState: colorState);
}
}
18 changes: 6 additions & 12 deletions test/traits/color_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,25 @@ main() {

testWidgets('When loading, should show CircularProgressIndicator ',
(WidgetTester tester) async {
final mockColorProvider = test.mockColorProvider(colorDevice);
when(mockColorProvider.isLoading).thenReturn(true);

final mockColorProvider =
test.mockColorProvider(colorDevice, isLoading: true, isBusy: true);
await tester.pumpWidget(test.createMaterialApp(mockColorProvider));

expect(find.byType(CircularProgressIndicator), findsOneWidget);
});

testWidgets('When performing action, should show CircularProgressIndicator ',
(WidgetTester tester) async {
final mockColorProvider = test.mockColorProvider(colorDevice);
when(mockColorProvider.isPerformingAction).thenReturn(true);

final mockColorProvider =
test.mockColorProvider(colorDevice, isPerformingAction: true);
await tester.pumpWidget(test.createMaterialApp(mockColorProvider));

expect(find.byType(CircularProgressIndicator), findsOneWidget);
});

testWidgets('When color widget is in error, should show error icon',
(WidgetTester tester) async {
final mockColorProvider = test.mockColorProvider(colorDevice);
when(mockColorProvider.isInErrorState).thenReturn(true);

final mockColorProvider =
test.mockColorProvider(colorDevice, isInErrorState: true);
await tester.pumpWidget(test.createMaterialApp(mockColorProvider));

expect(find.byIcon(Icons.error), findsOneWidget);
});
}
12 changes: 4 additions & 8 deletions test/traits/slim/color_slim_widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ColorSlimWidgetTest with DeviceTesting, ColorTesting {
MaterialApp createMaterialApp(ColorProvider mockColorProvider) {
return MaterialApp(
home: new Scaffold(
body: Scaffold(body: ColorSlimWidget(mockColorProvider))),
body: Column(children: [ColorSlimWidget(mockColorProvider)]))
);
}
}
Expand All @@ -22,11 +22,9 @@ void main() {

testWidgets('When loading, should show CircularProgressIndicator ',
(WidgetTester tester) async {
final mockColorProvider = test.mockColorProvider(colorDevice);
when(mockColorProvider.isLoading).thenReturn(true);

final mockColorProvider =
test.mockColorProvider(colorDevice, isLoading: true);
await tester.pumpWidget(test.createMaterialApp(mockColorProvider));

expect(find.byType(CircularProgressIndicator), findsOneWidget);
});

Expand All @@ -35,9 +33,7 @@ void main() {
(WidgetTester tester) async {
final mockColorProvider =
test.mockColorProvider(colorDevice.withColor(null));

await tester.pumpWidget(test.createMaterialApp(mockColorProvider));

verify(mockColorProvider.getColorState).called(2);
verify(mockColorProvider.getColorState).called(1);
});
}

0 comments on commit cc3c63c

Please sign in to comment.