-
-
Notifications
You must be signed in to change notification settings - Fork 186
Add useOnListenableChange
#438
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
Merged
rrousselGit
merged 10 commits into
rrousselGit:master
from
whynotmake-it:feat/use-on-listenable-changed
Jul 30, 2024
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
b2b270b
Add `useOnListenableChange`
timcreatedit 5781221
removed generic from `useOnListenableChange`
timcreatedit ff2619d
removed generic also from test
timcreatedit 4128dc7
remove keys
timcreatedit e22132e
support listener change
timcreatedit 31ec140
remove accidental dart:math import
timcreatedit 2b0ca3e
better performance on listener change
timcreatedit b9bbbf8
fixed removeListener
timcreatedit e11b548
test that all listeners are removed on unmount
timcreatedit f2812a1
applied review changes
timcreatedit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
165 changes: 165 additions & 0 deletions
165
packages/flutter_hooks/test/use_on_listenable_change_test.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_hooks/flutter_hooks.dart'; | ||
|
||
import 'mock.dart'; | ||
|
||
void main() { | ||
testWidgets('debugFillProperties', (tester) async { | ||
final listenable = ValueNotifier(42); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useOnListenableChange(listenable, () {}); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
await tester.pump(); | ||
|
||
final element = tester.element(find.byType(HookBuilder)); | ||
|
||
expect( | ||
element | ||
.toDiagnosticsNode(style: DiagnosticsTreeStyle.offstage) | ||
.toStringDeep(), | ||
equalsIgnoringHashCodes( | ||
'HookBuilder\n' | ||
' │ useOnListenableChange: ValueNotifier<int>#00000(42)\n' | ||
' └SizedBox(renderObject: RenderConstrainedBox#00000)\n', | ||
), | ||
); | ||
}); | ||
|
||
testWidgets('calls listener when Listenable updates', (tester) async { | ||
final listenable = ValueNotifier(42); | ||
|
||
int? value; | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useOnListenableChange( | ||
listenable, | ||
() => value = listenable.value, | ||
); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
expect(value, isNull); | ||
listenable.value++; | ||
expect(value, 43); | ||
}); | ||
|
||
testWidgets( | ||
'listens new Listenable when Listenable is changed', | ||
(tester) async { | ||
final listenable1 = ValueNotifier(42); | ||
final listenable2 = ValueNotifier(42); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
useOnListenableChange(listenable1, () {}); | ||
return const SizedBox(); | ||
}, | ||
), | ||
); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
useOnListenableChange(listenable2, () {}); | ||
return const SizedBox(); | ||
}, | ||
), | ||
); | ||
|
||
// ignore: invalid_use_of_protected_member | ||
expect(listenable1.hasListeners, isFalse); | ||
// ignore: invalid_use_of_protected_member | ||
expect(listenable2.hasListeners, isTrue); | ||
}, | ||
); | ||
|
||
testWidgets( | ||
'listens new listener when listener is changed', | ||
(tester) async { | ||
final listenable = ValueNotifier(42); | ||
late final int value; | ||
|
||
void listener1() { | ||
throw StateError('listener1 should not have been called'); | ||
} | ||
|
||
void listener2() { | ||
value = listenable.value; | ||
} | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
useOnListenableChange(listenable, listener1); | ||
return const SizedBox(); | ||
}, | ||
), | ||
); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder( | ||
builder: (context) { | ||
useOnListenableChange(listenable, listener2); | ||
return const SizedBox(); | ||
}, | ||
), | ||
); | ||
|
||
listenable.value++; | ||
// By now, we should have subscribed to listener2, which sets the value | ||
expect(value, 43); | ||
}, | ||
); | ||
|
||
testWidgets('unsubscribes when listenable becomes null', (tester) async { | ||
final listenable = ValueNotifier(42); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useOnListenableChange(listenable, () {}); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
// ignore: invalid_use_of_protected_member | ||
expect(listenable.hasListeners, isTrue); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useOnListenableChange(null, () {}); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
// ignore: invalid_use_of_protected_member | ||
expect(listenable.hasListeners, isFalse); | ||
}); | ||
|
||
testWidgets('unsubscribes when disposed', (tester) async { | ||
final listenable = ValueNotifier(42); | ||
|
||
await tester.pumpWidget( | ||
HookBuilder(builder: (context) { | ||
useOnListenableChange(listenable, () {}); | ||
return const SizedBox(); | ||
}), | ||
); | ||
|
||
// ignore: invalid_use_of_protected_member | ||
expect(listenable.hasListeners, isTrue); | ||
|
||
await tester.pumpWidget(Container()); | ||
|
||
// ignore: invalid_use_of_protected_member | ||
expect(listenable.hasListeners, isFalse); | ||
}); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.