Skip to content

Commit

Permalink
Add comprehensive widget tests for RiffSwitch
Browse files Browse the repository at this point in the history
- Added initial state test to ensure the widget displays the correct initial value.
- Implemented a test for toggling the switch to verify state changes on interaction.
- Added tests for correct rendering of the widget with different switch types.
- Ensured the onChanged callback is triggered and behaves as expected.
- Updated tests to handle both true and false states accurately by tapping the correct side of the switch.

Tests ensure proper functionality and visual correctness of the RiffSwitch widget.
  • Loading branch information
kenresoft committed Aug 7, 2024
1 parent 8947030 commit 9186785
Showing 1 changed file with 137 additions and 6 deletions.
143 changes: 137 additions & 6 deletions test/riff_switch_test.dart
Original file line number Diff line number Diff line change
@@ -1,18 +1,149 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:riff_switch/riff_switch.dart';

void main() {
testWidgets('Widget test for the riff_switch custom widget', (widgetTester) async {
await widgetTester.pumpWidget(RiffSwitch(
value: false,
onChanged: (value) {},
type: RiffSwitchType.simple,
));
await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RiffSwitch(
value: false,
onChanged: (value) {},
type: RiffSwitchType.simple,
),
),
),
);

final Finder finder = find.byWidgetPredicate(
(widget) => widget is RiffSwitch && widget.value == false,
(widget) => widget is RiffSwitch && widget.value == false,
);

expect(finder, findsOneWidget);
});

testWidgets('Tapping the switch changes its value', (widgetTester) async {
bool switchValue = false; // Toggle to test for both cases

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: StatefulBuilder(
builder: (BuildContext context, StateSetter setState) {
return RiffSwitch(
value: switchValue,
onChanged: (value) {
setState(() {
switchValue = value;
});
},
type: RiffSwitchType.simple,
);
},
),
),
),
);

final Finder switchFinder = find.byType(RiffSwitch);

// Get the size and position of the switch
final Rect switchRect = widgetTester.getRect(switchFinder);

// Calculate the tap position on the right side for true
final Offset tapPosition = Offset(switchRect.right - 1, switchRect.center.dy);

// Tap the calculated position
await widgetTester.tapAt(tapPosition);
await widgetTester.pumpAndSettle();

// Verify the switch's value has changed
expect(switchValue, true); // Toggle to test for both cases
});


testWidgets('RiffSwitch has correct initial state', (widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RiffSwitch(
value: true,
onChanged: (value) {},
type: RiffSwitchType.simple,
),
),
),
);

final Finder finder = find.byWidgetPredicate(
(widget) => widget is RiffSwitch && widget.value == true,
);

expect(finder, findsOneWidget);
});

testWidgets('RiffSwitch renders correctly', (widgetTester) async {
await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RiffSwitch(
value: false,
onChanged: (value) {},
type: RiffSwitchType.simple,
),
),
),
);

// Verify the widget renders with the correct initial value
expect(find.byType(RiffSwitch), findsOneWidget);
expect(find.byWidgetPredicate((widget) => widget is RiffSwitch && widget.value == false), findsOneWidget);
});

testWidgets('RiffSwitch onChanged callback is triggered', (widgetTester) async {
bool wasCalled = false;

await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RiffSwitch(
value: false,
onChanged: (value) {
wasCalled = true;
},
type: RiffSwitchType.simple,
),
),
),
);

final Finder switchFinder = find.byType(RiffSwitch);

// Tap the switch
await widgetTester.tap(switchFinder);
await widgetTester.pumpAndSettle();

// Verify the callback was triggered
expect(wasCalled, true);
});

testWidgets('RiffSwitch renders correctly for different types', (widgetTester) async {
for (var type in RiffSwitchType.values) {
await widgetTester.pumpWidget(
MaterialApp(
home: Scaffold(
body: RiffSwitch(
value: false,
onChanged: (value) {},
type: type,
),
),
),
);

// Verify the widget renders correctly for each type
expect(find.byType(RiffSwitch), findsOneWidget);
}
});
}

0 comments on commit 9186785

Please sign in to comment.