Skip to content

Commit

Permalink
test(Extensions): add unit tests for context media query extensions
Browse files Browse the repository at this point in the history
Signed-off-by: arafaysaleem <[email protected]>
  • Loading branch information
arafaysaleem committed Aug 1, 2021
1 parent 0503264 commit 0cf4f37
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.0"
mockito:
dependency: "direct main"
description:
name: mockito
url: "https://pub.dartlang.org"
source: hosted
version: "5.0.13"
octo_image:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ dependencies:
flutter_spinkit: 5.0.0
flutter_secure_storage: ^4.2.1
clock: ^1.1.0
mockito: ^5.0.13
dev_dependencies:
flutter_test:
sdk: flutter
Expand Down
75 changes: 75 additions & 0 deletions test/helper/extensions/context_extension_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import 'package:ez_ticketz_app/helper/extensions/context_extensions.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/annotations.dart';
import 'package:mockito/mockito.dart';

import 'context_extension_test.mocks.dart';

@GenerateMocks([BuildContext])
void main() {
group('ContextUtils', () {
group('extensions related to MediaQuery', () {
BuildContext _mockContextWithMediaQuery(Size size) {
final context = MockBuildContext();
final mediaQuery = MediaQuery(
data: MediaQueryData(size: size),
child: const SizedBox.shrink(),
);
when(context.widget).thenReturn(const SizedBox.shrink());
when(context.findAncestorWidgetOfExactType()).thenReturn(mediaQuery);
when(context.dependOnInheritedWidgetOfExactType<MediaQuery>())
.thenReturn(mediaQuery);
return context;
}

group('screenWidth', () {
test(
'GIVEN a Size created with a screen width '
'AND a BuildContext with MediaQuery '
'AND that MediaQuery has that Size '
'WHEN extension method `.screenWidth` is called '
'THEN a double is returned '
'AND is equal to that screen width',
() {
//given
const screenWidth = 500.0;
const size = Size.fromWidth(screenWidth);
final context = _mockContextWithMediaQuery(size);

//when
final actual = context.screenWidth;

//then
expect(actual, isA<double>());
expect(actual, screenWidth);
},
);
});

group('screenHeight', () {
test(
'GIVEN a Size created with a screen height '
'AND a BuildContext with MediaQuery '
'AND that MediaQuery has that Size '
'WHEN extension method `.screenHeight` is called '
'THEN a double is returned '
'AND is equal to that screen height',
() {
//given
const screenHeight = 650.0;
const size = Size.fromHeight(screenHeight);
final context = _mockContextWithMediaQuery(size);

//when
final actual = context.screenHeight;

//then
expect(actual, isA<double>());
expect(actual, screenHeight);
},
);
});
});
});
}

0 comments on commit 0cf4f37

Please sign in to comment.