Skip to content

Commit 3e90e77

Browse files
committed
Merge branch 'feat-flutter-service-test' into feat-flutter-dart-helpers-models-tests
2 parents b6ca936 + 31610b9 commit 3e90e77

File tree

9 files changed

+247
-3
lines changed

9 files changed

+247
-3
lines changed

src/SDK/Language/Dart.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,11 @@ public function getFiles(): array
436436
'destination' => '/test/src/enums_test.dart',
437437
'template' => 'dart/test/src/enums_test.dart.twig',
438438
],
439+
[
440+
'scope' => 'default',
441+
'destination' => '/test/src/exception_test.dart',
442+
'template' => 'dart/test/src/exception_test.dart.twig',
443+
],
439444
[
440445
'scope' => 'default',
441446
'destination' => '/test/src/response_test.dart',

src/SDK/Language/Flutter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,11 +275,36 @@ public function getFiles(): array
275275
'destination' => '/test/src/cookie_manager_test.dart',
276276
'template' => 'flutter/test/src/cookie_manager_test.dart.twig',
277277
],
278+
[
279+
'scope' => 'default',
280+
'destination' => '/test/src/interceptor_test.dart',
281+
'template' => 'flutter/test/src/interceptor_test.dart.twig',
282+
],
283+
[
284+
'scope' => 'default',
285+
'destination' => '/test/src/realtime_response_test.dart',
286+
'template' => 'flutter/test/src/realtime_response_test.dart.twig',
287+
],
288+
[
289+
'scope' => 'default',
290+
'destination' => '/test/src/realtime_response_connected_test.dart',
291+
'template' => 'flutter/test/src/realtime_response_connected_test.dart.twig',
292+
],
293+
[
294+
'scope' => 'default',
295+
'destination' => '/test/src/realtime_subscription_test.dart',
296+
'template' => 'flutter/test/src/realtime_subscription_test.dart.twig',
297+
],
278298
[
279299
'scope' => 'default',
280300
'destination' => '/test/src/enums_test.dart',
281301
'template' => 'dart/test/src/enums_test.dart.twig',
282302
],
303+
[
304+
'scope' => 'default',
305+
'destination' => '/test/src/exception_test.dart',
306+
'template' => 'dart/test/src/exception_test.dart.twig',
307+
],
283308
[
284309
'scope' => 'default',
285310
'destination' => '/test/src/response_test.dart',

templates/dart/lib/src/exception.dart.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class {{spec.title | caseUcfirst}}Exception implements Exception {
88

99
@override
1010
String toString() {
11-
if (message == null) return "{{spec.title | caseUcfirst}}Exception";
12-
return "{{spec.title | caseUcfirst}}Exception: $type, $message (${code ?? 0})";
11+
if (message == null || message == "") return "{{spec.title | caseUcfirst}}Exception";
12+
return "{{spec.title | caseUcfirst}}Exception: ${type ?? ''}, $message (${code ?? 0})";
1313
}
1414
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import 'package:{{lang.params.packageName}}/src/exception.dart';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('{{spec.title | caseUcfirst}}Exception', () {
6+
test('toString should return correct string representation', () {
7+
final exception1 = {{spec.title | caseUcfirst}}Exception();
8+
expect(exception1.toString(), equals('{{spec.title | caseUcfirst}}Exception'));
9+
10+
final exception2 = {{spec.title | caseUcfirst}}Exception('Some error message');
11+
expect(exception2.toString(), equals('{{spec.title | caseUcfirst}}Exception: , Some error message (0)'));
12+
13+
final exception3 = {{spec.title | caseUcfirst}}Exception('Invalid request', 400, 'ValidationError');
14+
expect(exception3.toString(), equals('{{spec.title | caseUcfirst}}Exception: ValidationError, Invalid request (400)'));
15+
});
16+
});
17+
}

templates/flutter/.travis.yml.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,6 @@ install:
2424
deploy:
2525
provider: script
2626
skip_cleanup: true
27-
script: cd $TRAVIS_BUILD_DIR && dart format ./lib/ && flutter pub publish -f
27+
script: cd $TRAVIS_BUILD_DIR && dart format ./lib/ && dart format ./test/ && flutter pub publish -f
2828
on:
2929
tags: true
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import 'dart:async';
2+
import 'package:http/http.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:mockito/mockito.dart';
5+
import 'package:{{lang.params.packageName}}/src/interceptor.dart';
6+
7+
class MockRequest extends Mock implements BaseRequest {
8+
final Map<String, String> headers = {};
9+
10+
@override
11+
Future<StreamedResponse> send() async {
12+
final response = StreamedResponse(ByteStream.fromBytes([]), 200);
13+
response.headers.addAll(headers);
14+
return response;
15+
}
16+
}
17+
18+
void main() {
19+
group('HeadersInterceptor', () {
20+
test('onRequest should add headers to the request', () async {
21+
final headers = {'Authorization': 'Bearer token123'};
22+
final interceptor = HeadersInterceptor(headers);
23+
final request = MockRequest();
24+
25+
final interceptedRequest = await interceptor.onRequest(request);
26+
27+
expect(interceptedRequest.headers, equals(headers));
28+
});
29+
30+
test('onResponse should return the same response', () async {
31+
final response = Response('', 200);
32+
final interceptor = HeadersInterceptor({});
33+
34+
final interceptedResponse = await interceptor.onResponse(response);
35+
36+
expect(interceptedResponse, equals(response));
37+
});
38+
});
39+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import 'dart:convert';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:{{lang.params.packageName}}/src/realtime_response_connected.dart';
5+
6+
void main() {
7+
group('RealtimeResponseConnected', () {
8+
final channels = ['channel1', 'channel2'];
9+
final user = {'id': 123, 'name': 'John Doe'};
10+
final response1 = RealtimeResponseConnected(channels: channels, user: user);
11+
12+
test('copyWith should create a new instance with updated properties', () {
13+
final newChannels = ['channel3', 'channel4'];
14+
final newUser = {'id': 456, 'name': 'Jane Smith'};
15+
16+
final updatedResponse = response1.copyWith(channels: newChannels, user: newUser);
17+
18+
expect(updatedResponse.channels, equals(newChannels));
19+
expect(updatedResponse.user, equals(newUser));
20+
});
21+
22+
test('toMap should return a map representation of the response', () {
23+
final responseMap = response1.toMap();
24+
25+
expect(responseMap['channels'], equals(channels));
26+
expect(responseMap['user'], equals(user));
27+
});
28+
29+
test('fromMap should create an instance from a map', () {
30+
final responseMap = {'channels': channels, 'user': user};
31+
32+
final response2 = RealtimeResponseConnected.fromMap(responseMap);
33+
34+
expect(response2.channels, equals(channels));
35+
expect(response2.user, equals(user));
36+
});
37+
38+
test('toJson and fromJson should convert to/from JSON', () {
39+
final jsonString = response1.toJson();
40+
41+
final response3 = RealtimeResponseConnected.fromJson(jsonString);
42+
43+
expect(response3.channels, equals(channels));
44+
expect(response3.user, equals(user));
45+
});
46+
47+
test('toString should return a string representation of the response', () {
48+
final responseString = response1.toString();
49+
50+
expect(responseString, equals('RealtimeResponseConnected(channels: $channels, user: $user)'));
51+
});
52+
53+
test('equality operator should compare two instances', () {
54+
final response2 = RealtimeResponseConnected(channels: channels, user: user);
55+
56+
expect(response1 == response2, isTrue);
57+
});
58+
59+
test('hashCode should return a unique hash value', () {
60+
final hashCode1 = response1.hashCode;
61+
62+
final response2 = RealtimeResponseConnected(channels: channels, user: user);
63+
final hashCode2 = response2.hashCode;
64+
65+
expect(hashCode1, equals(hashCode2));
66+
});
67+
});
68+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import 'dart:convert';
2+
import 'package:flutter/foundation.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
import 'package:{{lang.params.packageName}}/src/realtime_response.dart';
5+
6+
void main() {
7+
group('RealtimeResponse', () {
8+
final type = 'event';
9+
final data = {'event': 'message', 'payload': 'Hello, world!'};
10+
final response1 = RealtimeResponse(type: type, data: data);
11+
12+
test('copyWith should create a new instance with updated properties', () {
13+
final newType = 'response';
14+
final newData = {'result': true};
15+
16+
final updatedResponse = response1.copyWith(type: newType, data: newData);
17+
18+
expect(updatedResponse.type, equals(newType));
19+
expect(updatedResponse.data, equals(newData));
20+
});
21+
22+
test('toMap should return a map representation of the response', () {
23+
final responseMap = response1.toMap();
24+
25+
expect(responseMap['type'], equals(type));
26+
expect(responseMap['data'], equals(data));
27+
});
28+
29+
test('fromMap should create an instance from a map', () {
30+
final responseMap = {'type': type, 'data': data};
31+
32+
final response2 = RealtimeResponse.fromMap(responseMap);
33+
34+
expect(response2.type, equals(type));
35+
expect(response2.data, equals(data));
36+
});
37+
38+
test('toJson and fromJson should convert to/from JSON', () {
39+
final jsonString = response1.toJson();
40+
41+
final response3 = RealtimeResponse.fromJson(jsonString);
42+
43+
expect(response3.type, equals(type));
44+
expect(response3.data, equals(data));
45+
});
46+
47+
test('toString should return a string representation of the response', () {
48+
final responseString = response1.toString();
49+
50+
expect(
51+
responseString, equals('RealtimeResponse(type: $type, data: $data)'));
52+
});
53+
54+
test('equality operator should compare two instances', () {
55+
final response2 = RealtimeResponse(type: type, data: data);
56+
57+
expect(response1 == response2, isTrue);
58+
});
59+
60+
test('hashCode should return a unique hash value', () {
61+
final hashCode1 = response1.hashCode;
62+
63+
final response2 = RealtimeResponse(type: type, data: data);
64+
final hashCode2 = response2.hashCode;
65+
66+
expect(hashCode1, equals(hashCode2));
67+
});
68+
});
69+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import 'package:mockito/mockito.dart';
2+
import 'package:{{lang.params.packageName}}/src/realtime_message.dart';
3+
import 'package:{{lang.params.packageName}}/src/realtime_subscription.dart';
4+
import 'package:flutter_test/flutter_test.dart';
5+
6+
class MockStream<T> extends Mock implements Stream<T> {}
7+
8+
9+
10+
void main() {
11+
group('RealtimeSubscription', () {
12+
final mockStream = MockStream<RealtimeMessage>();
13+
final mockCloseFunction = () async {};
14+
final subscription = RealtimeSubscription(stream: mockStream, close: mockCloseFunction);
15+
16+
test('should have the correct stream and close function', () {
17+
expect(subscription.stream, equals(mockStream));
18+
expect(subscription.close, equals(mockCloseFunction));
19+
});
20+
});
21+
}

0 commit comments

Comments
 (0)