Skip to content

Commit

Permalink
Clean up nullabilities in resize sensor tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Nov 20, 2023
1 parent 1ee7435 commit 128567f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 44 deletions.
57 changes: 26 additions & 31 deletions test/over_react/component/_deprecated/resize_sensor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,24 +32,19 @@ void main() {
const int defaultContainerHeight = 100;

late Element domTarget;
Ref<ResizeSensorComponent?>? resizeSensorRef;
Ref<Element?>? containerRef;
late Ref<ResizeSensorComponent?> resizeSensorRef;
late Ref<Element?> containerRef;

setUp(() {
domTarget = document.createElement('div');
document.body!.append(domTarget);
addTearDown(domTarget.remove);
resizeSensorRef = createRef();
containerRef = createRef();
// Perform setup needed for using 'zonedExpect'.
storeZone();
});

tearDown(() {
domTarget.remove();
resizeSensorRef = null;
containerRef = null;
});

void renderSensorIntoContainer({
ResizeSensorHandler? onInitialize,
ResizeSensorHandler? onResize,
Expand Down Expand Up @@ -103,7 +98,7 @@ void main() {
resizes.add(event);
}, width: width, height: height, onInitialize: onInitialize, resizeSensorProps: resizeSensorProps);

if (resizeSensorRef!.current!.props.quickMount!) {
if (resizeSensorRef.current!.props.quickMount!) {
// Quick-mount ResizeSensors can't detect changes until one animation frame and two scroll events
// after mounting. Give them some time to settle.
await window.animationFrame;
Expand Down Expand Up @@ -230,22 +225,22 @@ void main() {
testResizeAmounts.forEach((description, amount) {
test('by a $description amount (${amount}px)', () async {
await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth + amount}px';
containerRef.current!.style.width = '${defaultContainerWidth + amount}px';
}, resizeSensorProps: props);
});
});

test('even when the bounding rect is very small', () async {
await expectResizeAfter(() {
containerRef!.current!.style.width = '4px';
containerRef.current!.style.width = '4px';
}, width: 2, height: 2, resizeSensorProps: props);
});

test('and shrink is true', () async {
props.shrink = true;

await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef.current!.style.width = '${defaultContainerWidth * 2}px';
}, resizeSensorProps: props);
});
});
Expand All @@ -254,22 +249,22 @@ void main() {
testResizeAmounts.forEach((description, amount) {
test('by a $description amount (${amount}px)', () async{
await expectResizeAfter(() {
containerRef!.current!.style.height = '${defaultContainerHeight + amount}px';
containerRef.current!.style.height = '${defaultContainerHeight + amount}px';
}, resizeSensorProps: props);
});
});

test('even when the bounding rect is very small', () async {
await expectResizeAfter(() {
containerRef!.current!.style.height = '4px';
containerRef.current!.style.height = '4px';
}, width: 2, height: 2, resizeSensorProps: props);
});

test('and shrink is true', () async {
props.shrink = true;

await expectResizeAfter(() {
containerRef!.current!.style.height = '${defaultContainerHeight * 2}px';
containerRef.current!.style.height = '${defaultContainerHeight * 2}px';
}, resizeSensorProps: props);
});
});
Expand All @@ -278,22 +273,22 @@ void main() {
testResizeAmounts.forEach((description, amount) {
test('by a $description amount (${amount}px)', () async{
await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth}px';
containerRef.current!.style.width = '${defaultContainerWidth}px';
}, width: defaultContainerWidth + amount, resizeSensorProps: props);
});
});

test('even when the bounding rect is very small', () async {
await expectResizeAfter(() {
containerRef!.current!.style.width = '1px';
containerRef.current!.style.width = '1px';
}, width: 2, height: 2, resizeSensorProps: props);
});

test('and shrink is true', () async{
props.shrink = true;

await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth / 2}px';
containerRef.current!.style.width = '${defaultContainerWidth / 2}px';
}, resizeSensorProps: props);
});
});
Expand All @@ -302,22 +297,22 @@ void main() {
testResizeAmounts.forEach((description, amount) {
test('by a $description amount (${amount}px)', () async{
await expectResizeAfter(() {
containerRef!.current!.style.height = '${defaultContainerHeight}px';
containerRef.current!.style.height = '${defaultContainerHeight}px';
}, height: defaultContainerHeight + amount, resizeSensorProps: props);
});
});

test('even when the bounding rect is very small', () async {
await expectResizeAfter(() {
containerRef!.current!.style.height = '1px';
containerRef.current!.style.height = '1px';
}, width: 2, height: 2, resizeSensorProps: props);
});

test('and shrink is true', () async {
props.shrink = true;

await expectResizeAfter(() {
containerRef!.current!.style.height = '${defaultContainerHeight / 2}px';
containerRef.current!.style.height = '${defaultContainerHeight / 2}px';
}, resizeSensorProps: props);
});
});
Expand All @@ -331,8 +326,8 @@ void main() {
group('false:', () {
test('passes the correct event args on resize', () async {
await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef!.current!.style.height = '${defaultContainerHeight * 2}px';
containerRef.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef.current!.style.height = '${defaultContainerHeight * 2}px';
}, onResize: (event) {
zonedExpect(event.newWidth, equals(defaultContainerWidth * 2));
zonedExpect(event.newHeight, equals(defaultContainerHeight * 2));
Expand Down Expand Up @@ -388,8 +383,8 @@ void main() {
bool onInitializeCalled = false;

await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef!.current!.style.height = '${defaultContainerHeight * 2}px';
containerRef.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef.current!.style.height = '${defaultContainerHeight * 2}px';
}, onInitialize: (_) {
onInitializeCalled = true;
}, resizeSensorProps: props);
Expand All @@ -400,8 +395,8 @@ void main() {

test('does not warn about props.onInitialize when it is not set', () async {
await expectResizeAfter(() {
containerRef!.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef!.current!.style.height = '${defaultContainerHeight * 2}px';
containerRef.current!.style.width = '${defaultContainerWidth * 2}px';
containerRef.current!.style.height = '${defaultContainerHeight * 2}px';
}, resizeSensorProps: props);

rejectValidationWarning(contains('onInitialize'));
Expand All @@ -411,14 +406,14 @@ void main() {
var resizeEvents = <ResizeSensorEvent>[];

await expectResizeAfter(() async {
containerRef!.current!.style.width = '${defaultContainerWidth + 10}px';
containerRef!.current!.style.height = '${defaultContainerHeight + 10}px';
containerRef.current!.style.width = '${defaultContainerWidth + 10}px';
containerRef.current!.style.height = '${defaultContainerHeight + 10}px';

await window.animationFrame;
await window.animationFrame;

containerRef!.current!.style.width = '${defaultContainerWidth + 20}px';
containerRef!.current!.style.height = '${defaultContainerHeight + 20}px';
containerRef.current!.style.width = '${defaultContainerWidth + 20}px';
containerRef.current!.style.height = '${defaultContainerHeight + 20}px';
}, onResize: resizeEvents.add, resizeEventCount: 2, resizeSensorProps: props);

expect(resizeEvents, hasLength(2));
Expand Down
19 changes: 6 additions & 13 deletions test/over_react/component/resize_sensor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ void main() {
setUp(() {
domTarget = document.createElement('div');
document.body!.append(domTarget);
addTearDown(domTarget.remove);
resizeSensorRef = createRef();
containerRef = createRef();
// Perform setup needed for using 'zonedExpect'.
storeZone();
});

// tearDown(() {
// domTarget.remove();
// });

void renderSensorIntoContainer({
ResizeSensorHandler? onInitialize,
ResizeSensorHandler? onResize,
Expand Down Expand Up @@ -440,16 +437,12 @@ void main() {
group('should indicate that the sensor needs to be reset', () {
Element? validTarget;
Element? detachedTarget;
Map<String, bool>? calls;
late Map<String, bool> calls;

setUp(() {
calls = <String, bool>{};
});

tearDown(() {
calls = null;
});

group('when mounted into a node that is not attached to the DOM', () {
setUp(() {
ValidationUtil.WARNINGS_ENABLED = true;
Expand All @@ -458,7 +451,7 @@ void main() {
detachedTarget!.style.width = '200px';
detachedTarget!.style.height = '200px';
mount((ResizeSensor()
..onDetachedMountCheck = (needsReset) { calls!['onDetachedMountCheck'] = needsReset; }
..onDetachedMountCheck = (needsReset) { calls['onDetachedMountCheck'] = needsReset; }
)(), attachedToDocument: false, mountNode: detachedTarget!);
});

Expand All @@ -468,7 +461,7 @@ void main() {
});

test('by providing a `true` argument value to the `props.onDetachedMountCheck` callback', () {
expect(calls!['onDetachedMountCheck'], isTrue);
expect(calls['onDetachedMountCheck'], isTrue);
});

test('and does not emit a validation warning about not needing to set the `onDetachedMountCheck` callback.', () {
Expand All @@ -485,7 +478,7 @@ void main() {
validTarget!.style.height = '200px';
document.body!.append(validTarget!);
mount((ResizeSensor()
..onDetachedMountCheck = (needsReset) { calls!['onDetachedMountCheck'] = needsReset; }
..onDetachedMountCheck = (needsReset) { calls['onDetachedMountCheck'] = needsReset; }
)(), attachedToDocument: true, mountNode: validTarget!);
});

Expand All @@ -496,7 +489,7 @@ void main() {
});

test('- providing a `false` argument value to the `props.onDetachedMountCheck` callback', () {
expect(calls!['onDetachedMountCheck'], isFalse);
expect(calls['onDetachedMountCheck'], isFalse);
});

test('and emits a validation warning about not needing to set the `onDetachedMountCheck` callback.', () {
Expand Down

0 comments on commit 128567f

Please sign in to comment.