Skip to content

Commit

Permalink
test(neon_dashboard): Add tests for the DashboardBloc
Browse files Browse the repository at this point in the history
Signed-off-by: provokateurin <kate@provokateurin.de>
  • Loading branch information
provokateurin committed Feb 8, 2024
1 parent fba6934 commit 5ec2b50
Show file tree
Hide file tree
Showing 2 changed files with 173 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/neon/neon_dashboard/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ dev_dependencies:
flutter_test:
sdk: flutter
go_router_builder: ^2.4.1
http: ^1.2.0
mocktail: ^1.0.3
neon_lints:
git:
Expand Down
172 changes: 172 additions & 0 deletions packages/neon/neon_dashboard/test/bloc_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
import 'dart:convert';

import 'package:built_collection/built_collection.dart';
import 'package:collection/collection.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:http/http.dart';
import 'package:mocktail/mocktail.dart';
import 'package:neon_dashboard/src/blocs/dashboard.dart';
import 'package:neon_framework/blocs.dart';
import 'package:neon_framework/models.dart';
import 'package:neon_framework/testing.dart';

Account mockDashboardAccount() => mockServer({
RegExp(r'/ocs/v2\.php/apps/dashboard/api/v1/widgets'): {
'get': (match, queryParameters) => Response(
json.encode({
'ocs': {
'meta': {'status': '', 'statuscode': 0},
'data': {
for (final entry in {
'null': null,
'empty': <dynamic>[],
'v1': [1],
'v2': [2],
'v1v2': [1, 2],
}.entries)
entry.key: {
'id': entry.key,
'title': '',
'order': 0,
'icon_class': '',
'icon_url': '',
'item_icons_round': false,
'item_api_versions': entry.value,
},
},
},
}),
200,
),
},
RegExp(r'/ocs/v2\.php/apps/dashboard/api/v1/widget-items'): {
'get': (match, queryParameters) => Response(
json.encode({
'ocs': {
'meta': {'status': '', 'statuscode': 0},
'data': {
for (final key in queryParameters['widgets[]']!)
key: [
{
'subtitle': '',
'title': key,
'link': '',
'iconUrl': '',
'sinceId': '',
},
],
'tooMany1': [
for (var i = 0; i < 8; i++)
{
'subtitle': '',
'title': '$i',
'link': '',
'iconUrl': '',
'sinceId': '',
},
],
},
},
}),
200,
),
},
RegExp(r'/ocs/v2\.php/apps/dashboard/api/v2/widget-items'): {
'get': (match, queryParameters) => Response(
json.encode({
'ocs': {
'meta': {'status': '', 'statuscode': 0},
'data': {
for (final key in queryParameters['widgets[]']!)
key: {
'items': [
{
'subtitle': '',
'title': key,
'link': '',
'iconUrl': '',
'sinceId': '',
},
],
'emptyContentMessage': '',
'halfEmptyContentMessage': '',
},
'tooMany2': {
'items': [
for (var i = 0; i < 8; i++)
{
'subtitle': '',
'title': '$i',
'link': '',
'iconUrl': '',
'sinceId': '',
},
],
'emptyContentMessage': '',
'halfEmptyContentMessage': '',
},
},
},
}),
200,
),
},
});

void main() {
late Account account;
late DashboardBloc bloc;

setUpAll(() {
final storage = MockNeonStorage();
when(() => storage.requestCache).thenReturn(null);
});

setUp(() {
account = mockDashboardAccount();
bloc = DashboardBloc(account);
});

test('refresh', () async {
final widgets = BuiltList<String>([
'null',
'v1',
'v2',
'v1v2',
]);
expect(
bloc.widgets.transformResult((e) => BuiltList<String>(e.map((w) => w.id))),
emitsInOrder([
Result<BuiltList<String>>.loading(),
Result.success(widgets),
Result.success(widgets).asLoading(),
Result.success(widgets),
]),
);

final items = BuiltList<String>([
'null',
'v1',
for (var i = 0; i < 7; i++) '$i',
'v2',
'v1v2',
for (var i = 0; i < 7; i++) '$i',
]);
expect(
bloc.items
.transformResult(
(e) => BuiltList<String>(e.values.map((items) => items.items.map((item) => item.title)).flattened),
)
.distinct(),
emitsInOrder([
Result(BuiltList<String>(), null, isLoading: true, isCached: false),
Result.success(items),
Result.success(items).asLoading(),
Result.success(items),
]),
);
// The delay is necessary to avoid a race condition with loading twice at the same time
await Future<void>.delayed(const Duration(milliseconds: 1));
await bloc.refresh();
});
}

0 comments on commit 5ec2b50

Please sign in to comment.