-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(neon_dashboard): Add tests for the DashboardBloc
Signed-off-by: provokateurin <kate@provokateurin.de>
- Loading branch information
1 parent
fba6934
commit 5ec2b50
Showing
2 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
} |