Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Jan 19, 2024
1 parent b86a8e9 commit 169d919
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import 'dart:convert';
import 'dart:io';
import 'dart:typed_data';

import 'package:mocktail/mocktail.dart';

Expand Down Expand Up @@ -40,10 +38,13 @@ import 'package:mocktail/mocktail.dart';
/// }
/// ```
/// {@endtemplate}
T mockNetworkImages<T>(T Function() body, {Uint8List? imageData}) {
T mockNetworkImages<T>(
T Function() body, {
List<int> imageBytes = _transparentPixelPng,
}) {
return HttpOverrides.runZoned(
body,
createHttpClient: (_) => _createHttpClient(imageData),
createHttpClient: (_) => _createHttpClient(data: imageBytes),
);
}

Expand All @@ -60,7 +61,7 @@ class _MockHttpClientResponse extends Mock implements HttpClientResponse {}

class _MockHttpHeaders extends Mock implements HttpHeaders {}

HttpClient _createHttpClient([Uint8List? imageData]) {
HttpClient _createHttpClient({required List<int> data}) {
final client = _MockHttpClient();
final request = _MockHttpClientRequest();
final response = _MockHttpClientResponse();
Expand All @@ -81,7 +82,7 @@ HttpClient _createHttpClient([Uint8List? imageData]) {
invocation.positionalArguments[0] as void Function(List<int>);
final onDone = invocation.namedArguments[#onDone] as void Function()?;
return Stream<List<int>>.fromIterable(
<List<int>>[imageData ?? _transparentPixelPng],
<List<int>>[data],
).listen(onData, onDone: onDone);
});
when(() => request.headers).thenReturn(headers);
Expand All @@ -90,6 +91,75 @@ HttpClient _createHttpClient([Uint8List? imageData]) {
return client;
}

final _transparentPixelPng = base64Decode(
'''iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==''',
);
const _transparentPixelPng = [
137,
80,
78,
71,
13,
10,
26,
10,
0,
0,
0,
13,
73,
72,
68,
82,
0,
0,
0,
1,
0,
0,
0,
1,
8,
6,
0,
0,
0,
31,
21,
196,
137,
0,
0,
0,
13,
73,
68,
65,
84,
120,
218,
99,
252,
207,
192,
80,
15,
0,
4,
133,
1,
128,
132,
169,
140,
33,
0,
0,
0,
0,
73,
69,
78,
68,
174,
66,
96,
130,
];
Original file line number Diff line number Diff line change
Expand Up @@ -45,30 +45,26 @@ void main() {
});
});

test(
'should use custom imageData',
() async {
// Mock green pixel generated with https://png-pixel.com/
final greenPixel = base64Decode(
'''iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg==''',
);
await mockNetworkImages(
() async {
final client = HttpClient()..autoUncompress = false;
final request = await client.getUrl(Uri.https(''));
final response = await request.close();
final data = <int>[];
test('should properly use custom imageBytes', () async {
final greenPixel = base64Decode(
'''iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg==''',
);
await mockNetworkImages(
() async {
final client = HttpClient()..autoUncompress = false;
final request = await client.getUrl(Uri.https(''));
final response = await request.close();
final data = <int>[];

response.listen(data.addAll);
response.listen(data.addAll);

// Wait for all microtasks to run
await Future<void>.delayed(Duration.zero);
// Wait for all microtasks to run
await Future<void>.delayed(Duration.zero);

expect(data, equals(greenPixel));
},
imageData: greenPixel,
);
},
);
expect(data, equals(greenPixel));
},
imageBytes: greenPixel,
);
});
});
}

0 comments on commit 169d919

Please sign in to comment.