Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(neon_framework): Deduplicate mock server implementation in tests #1582

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions packages/neon_framework/lib/src/testing/mock_server.dart
Original file line number Diff line number Diff line change
@@ -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<RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> 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);
}),
);
1 change: 1 addition & 0 deletions packages/neon_framework/lib/testing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
24 changes: 2 additions & 22 deletions packages/neon_framework/test/user_status_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -69,7 +68,7 @@ Account mockUserStatusAccount() {
200,
);

final requests = <RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> queryParameters)>>{
return mockServer({
RegExp(r'/ocs/v2\.php/apps/user_status/api/v1/predefined_statuses'): {
'get': (match, queryParameters) => predefinedStatusesResponse(),
},
Expand Down Expand Up @@ -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() {
Expand Down
24 changes: 2 additions & 22 deletions packages/neon_framework/test/weather_status_bloc_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -31,7 +30,7 @@ Account mockWeatherStatusAccount() {
200,
);

final requests = <RegExp, Map<String, Response Function(RegExpMatch match, Map<String, String> queryParameters)>>{
return mockServer({
RegExp(r'/ocs/v2\.php/apps/weather_status/api/v1/location'): {
'get': (match, queryParameters) => locationResponse(),
'put': (match, queryParameters) {
Expand Down Expand Up @@ -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}) =>
Expand Down
Loading