From dacc15b19777799519b7204e185ec7750614ccf8 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 6 Feb 2024 12:17:33 +0100 Subject: [PATCH] refactor(neon_framework): Deduplicate mock server implementation in tests Signed-off-by: provokateurin --- .../lib/src/testing/mock_server.dart | 28 +++++++++++++++++++ packages/neon_framework/lib/testing.dart | 1 + .../test/user_status_bloc_test.dart | 24 ++-------------- .../test/weather_status_bloc_test.dart | 24 ++-------------- 4 files changed, 33 insertions(+), 44 deletions(-) create mode 100644 packages/neon_framework/lib/src/testing/mock_server.dart diff --git a/packages/neon_framework/lib/src/testing/mock_server.dart b/packages/neon_framework/lib/src/testing/mock_server.dart new file mode 100644 index 00000000000..eac9c498faf --- /dev/null +++ b/packages/neon_framework/lib/src/testing/mock_server.dart @@ -0,0 +1,28 @@ +import 'package:http/http.dart'; +import 'package:http/testing.dart'; +import 'package:neon_framework/src/models/account.dart'; + +/// Creates an [Account] connected to a fake server defined by [requests]. +/// +/// To be used for end-to-end testing `Bloc`s. +Account mockServer( + Map queryParameters)>> requests, +) => + Account( + serverURL: Uri.parse('https://example.com'), + username: 'test', + password: 'test', + httpClient: MockClient((request) async { + for (final entry in requests.entries) { + final match = entry.key.firstMatch(request.url.path); + if (match != null) { + final call = entry.value[request.method]; + if (call != null) { + return call(match, request.url.queryParameters); + } + } + } + + throw Exception(request); + }), + ); diff --git a/packages/neon_framework/lib/testing.dart b/packages/neon_framework/lib/testing.dart index 8e5cd9ed848..17276b119ad 100644 --- a/packages/neon_framework/lib/testing.dart +++ b/packages/neon_framework/lib/testing.dart @@ -7,5 +7,6 @@ library; import 'package:flutter/material.dart'; +export 'package:neon_framework/src/testing/mock_server.dart'; export 'package:neon_framework/src/testing/mocks.dart'; export 'package:neon_framework/src/testing/utils.dart'; diff --git a/packages/neon_framework/test/user_status_bloc_test.dart b/packages/neon_framework/test/user_status_bloc_test.dart index 0f3578af3cf..8d39e4de530 100644 --- a/packages/neon_framework/test/user_status_bloc_test.dart +++ b/packages/neon_framework/test/user_status_bloc_test.dart @@ -2,7 +2,6 @@ import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart'; -import 'package:http/testing.dart'; import 'package:mocktail/mocktail.dart'; import 'package:neon_framework/blocs.dart'; import 'package:neon_framework/platform.dart'; @@ -69,7 +68,7 @@ Account mockUserStatusAccount() { 200, ); - final requests = queryParameters)>>{ + return mockServer({ RegExp(r'/ocs/v2\.php/apps/user_status/api/v1/predefined_statuses'): { 'get': (match, queryParameters) => predefinedStatusesResponse(), }, @@ -125,26 +124,7 @@ Account mockUserStatusAccount() { return statusResponse(); }, }, - }; - - return Account( - serverURL: Uri.parse('https://example.com'), - username: 'test', - password: 'test', - httpClient: MockClient((request) async { - for (final entry in requests.entries) { - final match = entry.key.firstMatch(request.url.path); - if (match != null) { - final call = entry.value[request.method]; - if (call != null) { - return call(match, request.url.queryParameters); - } - } - } - - throw Exception(request); - }), - ); + }); } void main() { diff --git a/packages/neon_framework/test/weather_status_bloc_test.dart b/packages/neon_framework/test/weather_status_bloc_test.dart index 502155097c3..4d54fbb44b6 100644 --- a/packages/neon_framework/test/weather_status_bloc_test.dart +++ b/packages/neon_framework/test/weather_status_bloc_test.dart @@ -2,7 +2,6 @@ import 'dart:convert'; import 'package:flutter_test/flutter_test.dart'; import 'package:http/http.dart'; -import 'package:http/testing.dart'; import 'package:mocktail/mocktail.dart'; import 'package:neon_framework/blocs.dart'; import 'package:neon_framework/src/models/account.dart'; @@ -31,7 +30,7 @@ Account mockWeatherStatusAccount() { 200, ); - final requests = queryParameters)>>{ + return mockServer({ RegExp(r'/ocs/v2\.php/apps/weather_status/api/v1/location'): { 'get': (match, queryParameters) => locationResponse(), 'put': (match, queryParameters) { @@ -92,26 +91,7 @@ Account mockWeatherStatusAccount() { 200, ), }, - }; - - return Account( - serverURL: Uri.parse('https://example.com'), - username: 'test', - password: 'test', - httpClient: MockClient((request) async { - for (final entry in requests.entries) { - final match = entry.key.firstMatch(request.url.path); - if (match != null) { - final call = entry.value[request.method]; - if (call != null) { - return call(match, request.url.queryParameters); - } - } - } - - throw Exception(request); - }), - ); + }); } core.OcsGetCapabilitiesResponseApplicationJson_Ocs_Data buildCapabilities({required bool enabled}) =>