Skip to content

Commit

Permalink
Testing Navigation servcie
Browse files Browse the repository at this point in the history
  • Loading branch information
Dante291 committed Nov 21, 2023
1 parent e8129ce commit cc385f3
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
7 changes: 7 additions & 0 deletions test/helpers/test_helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,13 @@ NavigationService getAndRegisterNavigationService() {
when(service.pushScreen(any, arguments: anyNamed('arguments')))
.thenAnswer((_) async {});
when(service.popAndPushScreen(any, arguments: '-1')).thenAnswer((_) async {});
when(service.pushReplacementScreen(any, arguments: anyNamed('arguments')))
.thenAnswer((_) async {});
when(service.pushDialog(any)).thenReturn(null);
when(service.showSnackBar(any)).thenReturn(null);
when(service.showTalawaErrorSnackBar(any, any)).thenReturn(null);
when(service.showTalawaErrorDialog(any, any)).thenReturn(null);
when(service.pop()).thenReturn(null);
locator.registerSingleton<NavigationService>(service);
return service;
}
Expand Down
123 changes: 123 additions & 0 deletions test/service_tests/naviagtion_service_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:talawa/enums/enums.dart';
import '../helpers/test_helpers.dart';
import '../helpers/test_locator.dart';

/// Test for navigation_service.dart.
///
/// more_info_if_required
///
/// **params**:
/// None
///
/// **returns**:
/// None
void main() {
testSetupLocator();

setUp(() {
registerServices();
});

tearDown(() {
unregisterServices();
});

group('Testing all functions in NavigationService', () {
test('NavigationService should call pushScreen with correct route',
() async {
await navigationService.pushScreen('testRoute', arguments: 'testArgs');
verify(navigationService.pushScreen('testRoute', arguments: 'testArgs'))
.called(1);
});

test('NavigationService should call popAndPushScreen with correct route',
() async {
await navigationService.popAndPushScreen(
'newRoute',
arguments: 'newArgs',
);
verify(
navigationService.popAndPushScreen(
'newRoute',
arguments: 'newArgs',
),
).called(1);
});

test(
'NavigationService should call pushReplacementScreen with correct route',
() async {
await navigationService.pushReplacementScreen(
'replacementRoute',
arguments: 'replacementArgs',
);
verify(
navigationService.pushReplacementScreen(
'replacementRoute',
arguments: 'replacementArgs',
),
).called(1);
});

test('NavigationService should call removeAllAndPush with correct routes',
() async {
await navigationService.removeAllAndPush(
'routeName',
'/tillRoute',
arguments: 'args',
);
verify(
navigationService.removeAllAndPush(
'routeName',
'/tillRoute',
arguments: 'args',
),
).called(1);
});

test('NavigationService should call pushDialog with correct widget',
() async {
const testDialog = Text('Test Dialog');
navigationService.pushDialog(testDialog);
verify(navigationService.pushDialog(testDialog)).called(1);
});

test('NavigationService should call showSnackBar with correct message', () {
const testMessage = 'Test Message';
navigationService.showSnackBar(testMessage);
verify(navigationService.showSnackBar(testMessage)).called(1);
});

test(
'NavigationService should call showTalawaErrorSnackBar with correct message and type',
() {
const errorMessage = 'Error Message';
const messageType = MessageType.error;
navigationService.showTalawaErrorSnackBar(errorMessage, messageType);
verify(
navigationService.showTalawaErrorSnackBar(
errorMessage,
messageType,
),
).called(1);
});

test(
'NavigationService should call showTalawaErrorDialog with correct message and type',
() {
const errorMessage = 'Error Dialog Message';
const messageType = MessageType.error;
navigationService.showTalawaErrorDialog(errorMessage, messageType);
verify(navigationService.showTalawaErrorDialog(errorMessage, messageType))
.called(1);
});

test('NavigationService should call pop', () {
navigationService.pop();
verify(navigationService.pop()).called(1);
});
});
}

0 comments on commit cc385f3

Please sign in to comment.