Skip to content

Commit

Permalink
refactor(neon_framework): Always use a real Account for testing
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Aug 16, 2024
1 parent 3eed63e commit 755b28d
Show file tree
Hide file tree
Showing 17 changed files with 32 additions and 110 deletions.
17 changes: 12 additions & 5 deletions packages/neon_framework/lib/src/testing/mocks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import 'package:flutter/widgets.dart';
import 'package:go_router/go_router.dart';
import 'package:meta/meta.dart';
// ignore: depend_on_referenced_packages
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/blocs.dart';
Expand All @@ -23,10 +22,18 @@ import 'package:shared_preferences_platform_interface/shared_preferences_platfor
// ignore: depend_on_referenced_packages
import 'package:url_launcher_platform_interface/url_launcher_platform_interface.dart';

class MockAccount extends Mock implements Account {
@internal
@override
Uri completeUri(Uri uri) => uri;
// ignore: non_constant_identifier_names
Account MockAccount({
String serverURL = 'https://cloud.example.com:8443/nextcloud',
String username = 'username',
String password = 'password',
}) {
return Account(
(b) => b
..serverURL = Uri.parse(serverURL)
..username = username
..password = password,
);
}

class MockAccountCache<T extends Disposable> extends Mock implements AccountCache<T> {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:notifications_app/l10n/localizations.dart';
import 'package:notifications_app/src/blocs/notifications.dart';
import 'package:notifications_app/src/pages/main.dart';
Expand Down Expand Up @@ -36,7 +35,6 @@ void main() {
when(() => bloc.notifications).thenAnswer((_) => BehaviorSubject.seeded(Result.success(BuiltList())));

account = MockAccount();
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));
});

testWidgets('Errors', (tester) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/notifications.dart' as notifications;
import 'package:notifications_app/l10n/localizations.dart';
import 'package:notifications_app/src/widgets/action.dart';
Expand Down Expand Up @@ -47,7 +46,6 @@ void main() {
callback = MockCallbackFunction<void>().call;

account = MockAccount();
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));
});

testWidgets('Notification', (tester) async {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:provider/provider.dart';
import 'package:talk_app/src/widgets/actor_avatar.dart';
Expand All @@ -20,8 +18,6 @@ void main() {

testWidgets('$type', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

await tester.pumpWidgetWithAccessibility(
TestApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:nextcloud/utils.dart';
import 'package:provider/provider.dart';
Expand Down Expand Up @@ -54,10 +53,7 @@ void main() {
when(() => bloc.errors).thenAnswer((_) => StreamController<Object>().stream);
when(() => bloc.rooms).thenAnswer((_) => BehaviorSubject.seeded(Result.success(BuiltList([room]))));

account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.username).thenReturn('test');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));
account = MockAccount(username: 'test');
});

testWidgets('Errors', (tester) async {
Expand Down
35 changes: 1 addition & 34 deletions packages/neon_framework/packages/talk_app/test/message_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:neon_framework/testing.dart';
import 'package:neon_framework/theme.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/core.dart' as core;
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';
Expand Down Expand Up @@ -320,8 +319,6 @@ void main() {

testWidgets('Comment', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final chatMessage = MockChatMessage();
when(() => chatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -404,8 +401,6 @@ void main() {

testWidgets('TalkParentMessage', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final chatMessage = MockChatMessage();
when(() => chatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -437,10 +432,7 @@ void main() {

group('TalkCommentMessage', () {
testWidgets('Self', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.username).thenReturn('test');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));
final account = MockAccount(username: 'test');

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -494,9 +486,6 @@ void main() {

testWidgets('Other', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.username).thenReturn('other');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -550,8 +539,6 @@ void main() {

testWidgets('Deleted', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -596,8 +583,6 @@ void main() {

testWidgets('As parent', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final chatMessage = MockChatMessage();
when(() => chatMessage.timestamp).thenReturn(0);
Expand Down Expand Up @@ -636,8 +621,6 @@ void main() {

testWidgets('With parent', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -722,8 +705,6 @@ void main() {
);

final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final chatMessage = MockChatMessageWithParent();
when(() => chatMessage.id).thenReturn(0);
Expand Down Expand Up @@ -778,8 +759,6 @@ void main() {
group('Separate messages', () {
testWidgets('Actor', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -831,8 +810,6 @@ void main() {

testWidgets('Time', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -883,8 +860,6 @@ void main() {

testWidgets('System message', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.system);
Expand Down Expand Up @@ -936,8 +911,6 @@ void main() {

testWidgets('Edited', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final previousChatMessage = MockChatMessage();
when(() => previousChatMessage.messageType).thenReturn(spreed.MessageType.comment);
Expand Down Expand Up @@ -999,9 +972,6 @@ void main() {

setUp(() {
account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.username).thenReturn('test');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

chatMessage = MockChatMessage();
when(() => chatMessage.timestamp).thenReturn(0);
Expand Down Expand Up @@ -1575,9 +1545,6 @@ void main() {
.thenAnswer((_) => BehaviorSubject.seeded(Result.success(userDetails)));

final account = MockAccount();
when(() => account.id).thenReturn('id');
when(() => account.username).thenReturn('username');
when(() => account.client).thenReturn(NextcloudClient(Uri()));

await tester.pumpWidgetWithAccessibility(
TestApp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:mocktail/mocktail.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:provider/provider.dart';
import 'package:talk_app/src/widgets/reference_preview.dart';

Expand Down Expand Up @@ -33,7 +32,6 @@ void main() {

testWidgets('Default', (tester) async {
final account = MockAccount();
when(() => account.client).thenReturn(NextcloudClient(Uri()));

final openGraphObject = MockOpenGraphObject();
when(() => openGraphObject.name).thenReturn('name');
Expand All @@ -60,7 +58,6 @@ void main() {

testWidgets('Opens link', (tester) async {
final account = MockAccount();
when(() => account.client).thenReturn(NextcloudClient(Uri()));

final openGraphObject = MockOpenGraphObject();
when(() => openGraphObject.name).thenReturn('name');
Expand All @@ -87,7 +84,6 @@ void main() {

testWidgets('With thumb', (tester) async {
final account = MockAccount();
when(() => account.client).thenReturn(NextcloudClient(Uri()));

final openGraphObject = MockOpenGraphObject();
when(() => openGraphObject.thumb).thenReturn('');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:neon_framework/testing.dart';
import 'package:neon_framework/theme.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
Expand All @@ -30,16 +29,6 @@ void main() {

setUp(() {
account = MockAccount();
when(() => account.id).thenReturn('id');
when(() => account.username).thenReturn('username');
when(() => account.serverURL).thenReturn(Uri.parse('http://example.com'));
when(() => account.client).thenReturn(
NextcloudClient(
Uri(),
loginName: '',
password: '',
),
);
});

testWidgets('Deck card', (tester) async {
Expand Down Expand Up @@ -434,7 +423,7 @@ void main() {
find.byWidgetPredicate(
(widget) =>
widget is NeonUriImage &&
widget.uri.toString() == 'http://example.com/remote.php/dav/files/username/path',
widget.uri.toString() == 'https://cloud.example.com:8443/nextcloud/remote.php/dav/files/username/path',
),
findsOne,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:neon_framework/testing.dart';
import 'package:neon_framework/theme.dart';
import 'package:neon_framework/utils.dart';
import 'package:neon_framework/widgets.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:provider/provider.dart';
import 'package:rxdart/subjects.dart';
Expand All @@ -22,8 +21,6 @@ void main() {

testWidgets('Custom avatar', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final room = MockRoom();
when(() => room.isCustomAvatar).thenReturn(true);
Expand All @@ -45,8 +42,6 @@ void main() {

testWidgets('One to one', (tester) async {
final account = MockAccount();
when(() => account.id).thenReturn('');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

final userStatusBloc = MockUserStatusBloc();
when(() => userStatusBloc.statuses).thenAnswer((_) => BehaviorSubject.seeded(BuiltMap()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';
import 'package:neon_framework/utils.dart';
import 'package:nextcloud/nextcloud.dart';
import 'package:nextcloud/spreed.dart' as spreed;
import 'package:provider/provider.dart';
import 'package:rxdart/rxdart.dart';
Expand Down Expand Up @@ -164,8 +163,6 @@ void main() {
when(() => bloc.reactions).thenAnswer((_) => BehaviorSubject.seeded(BuiltMap()));

final account = MockAccount();
when(() => account.id).thenReturn('id');
when(() => account.client).thenReturn(NextcloudClient(Uri.parse('')));

await tester.pumpWidgetWithAccessibility(
TestApp(
Expand Down
4 changes: 2 additions & 2 deletions packages/neon_framework/test/account_cache_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import 'package:neon_framework/testing.dart';
void main() {
final disposable0 = MockDisposable();
final disposable1 = MockDisposable();
final account0 = MockAccount();
final account1 = MockAccount();
final account0 = MockAccount(username: '0');
final account1 = MockAccount(username: '1');

group('AccountCache', () {
test('map functionality', () {
Expand Down
3 changes: 0 additions & 3 deletions packages/neon_framework/test/dialog_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,6 @@ void main() {

testWidgets('Without drop_account', (tester) async {
final account = MockAccount();
when(() => account.humanReadableID).thenReturn('');

final capabilitiesBloc = MockCapabilitiesBloc();
when(() => capabilitiesBloc.capabilities).thenAnswer(
Expand All @@ -433,7 +432,6 @@ void main() {
group('With drop_account', () {
testWidgets('Disabled', (tester) async {
final account = MockAccount();
when(() => account.humanReadableID).thenReturn('');

final capabilitiesBloc = MockCapabilitiesBloc();
when(() => capabilitiesBloc.capabilities).thenAnswer(
Expand Down Expand Up @@ -482,7 +480,6 @@ void main() {

testWidgets('Delay', (tester) async {
final account = MockAccount();
when(() => account.humanReadableID).thenReturn('');

final capabilitiesBloc = MockCapabilitiesBloc();
when(() => capabilitiesBloc.capabilities).thenAnswer(
Expand Down
Loading

0 comments on commit 755b28d

Please sign in to comment.