Skip to content

Commit

Permalink
test(home): add widget tests (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsutsu3 authored Feb 1, 2025
1 parent 762980f commit b305ab8
Show file tree
Hide file tree
Showing 2 changed files with 237 additions and 0 deletions.
36 changes: 36 additions & 0 deletions test/widgets/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,15 @@ final serverV5 = Server(
alias: 'test v5',
defaultServer: false,
apiVersion: 'v5',
enabled: false,
);

final serverV6 = Server(
address: 'http://localhost:8081',
alias: 'test v6',
defaultServer: false,
apiVersion: 'v6',
enabled: true,
);

final domains = [
Expand Down Expand Up @@ -950,6 +952,10 @@ class TestSetupHelper {
sid: 'sid123',
),
);

when(mockApiGatewayV6.enableServerRequest()).thenAnswer((_) async {
return EnableServerResponse(result: APiResponseType.success);
});
}

void _initApiGatewayV6Mock() {
Expand Down Expand Up @@ -986,5 +992,35 @@ class TestSetupHelper {
data: DomainResult(success: true, message: 'Added white.example.com'),
);
});

when(mockApiGatewayV6.enableServerRequest()).thenAnswer((_) async {
return EnableServerResponse(result: APiResponseType.success);
});

when(mockApiGatewayV6.disableServerRequest(any)).thenAnswer((_) async {
return DisableServerResponse(result: APiResponseType.success);
});

when(mockApiGatewayV6.loginQuery()).thenAnswer(
(_) async => LoginQueryResponse(
result: APiResponseType.success,
status: 'enabled',
sid: 'sid123',
),
);

when(mockApiGatewayV6.realtimeStatus()).thenAnswer(
(_) async => RealtimeStatusResponse(
result: APiResponseType.success,
data: realtimeStatus,
),
);

when(mockApiGatewayV6.fetchOverTimeData()).thenAnswer(
(_) async => FetchOverTimeDataResponse(
result: APiResponseType.success,
data: overtimeData,
),
);
}
}
201 changes: 201 additions & 0 deletions test/widgets/home/home_test.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:pi_hole_client/constants/enums.dart';
import 'package:pi_hole_client/models/server.dart';
import 'package:pi_hole_client/screens/home/disable_modal.dart';
import 'package:pi_hole_client/screens/home/home.dart';
import 'package:pi_hole_client/screens/home/home_appbar.dart';
import 'package:pi_hole_client/screens/home/switch_server_modal.dart';

import '../helpers.dart';

Expand Down Expand Up @@ -63,6 +69,201 @@ void main() async {
expect(find.text('Client activity last 24 hours'), findsOneWidget);
},
);

testWidgets(
'should home screen be rendered (loading state)',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

when(testSetup.mockStatusProvider.getStatusLoading).thenAnswer(
(_) => LoadStatus.loading,
);

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
await tester.pump();

expect(find.text('Loading stats...'), findsOneWidget);
},
);

testWidgets(
'should home screen be rendered (loading error state)',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

when(testSetup.mockStatusProvider.getStatusLoading).thenAnswer(
(_) => LoadStatus.error,
);

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
await tester.pump();

expect(find.text('Stats could not be loaded'), findsOneWidget);
},
);

testWidgets(
'should show disable modal and snackbar when disable button is pressed',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
await tester.pump();
expect(find.byType(FloatingActionButton), findsOneWidget);

await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle(const Duration(seconds: 3));

expect(find.byType(DisableModal), findsOneWidget);
await tester.tap(find.text('30 seconds').last);
await tester.pump();
await tester.tap(find.text('Accept').last);
await tester.pump();
await tester.pumpAndSettle(const Duration(seconds: 1));

expect(find.byType(SnackBar), findsOneWidget);
expect(find.text('Server disabled successfully.'), findsOneWidget);
},
);

testWidgets(
'should show enabled snackbar when enable button is pressed',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

final serverV6 = Server(
address: 'http://localhost:8081',
alias: 'test v6',
defaultServer: false,
apiVersion: 'v6',
enabled: false,
);
when(testSetup.mockServersProvider.selectedServer)
.thenReturn(serverV6);

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
await tester.pump();
expect(find.byType(FloatingActionButton), findsOneWidget);

await tester.tap(find.byType(FloatingActionButton));
await tester.pumpAndSettle(const Duration(seconds: 3));

expect(find.byType(DisableModal), findsNothing);
expect(find.byType(SnackBar), findsOneWidget);
expect(find.text('Server enabled successfully.'), findsOneWidget);
},
);

testWidgets(
'should show switch server modal',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
expect(find.byType(HomeAppBar), findsOneWidget);
await tester.pump();

// show switch server modal
expect(find.text('test v6'), findsWidgets); // TODO: find One
await tester.tap(find.text('test v6').last);
await tester.pumpAndSettle();
expect(find.byType(SwitchServerModal), findsOneWidget);
await tester.pump();

// tap and swith server
await tester.tap(find.text('test v6').last);
},
);

testWidgets(
'should show popup menu',
(WidgetTester tester) async {
tester.view.physicalSize = const Size(1080, 2400);
tester.view.devicePixelRatio = 2.0;

addTearDown(() {
tester.view.resetPhysicalSize();
tester.view.resetDevicePixelRatio();
});

await tester.pumpWidget(
testSetup.buildTestWidget(
const Home(),
),
);

expect(find.byType(Home), findsOneWidget);
expect(find.byType(HomeAppBar), findsOneWidget);
expect(find.byType(PopupMenuButton), findsOneWidget);
await tester.pump();

await tester.tap(find.byType(PopupMenuButton));
await tester.pumpAndSettle();
expect(find.text('Refresh'), findsOneWidget);
expect(find.text('Open web panel'), findsOneWidget);
expect(find.text('Change server'), findsOneWidget);
},
);
},
);
}

0 comments on commit b305ab8

Please sign in to comment.