This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: state not updated on post change
- Loading branch information
1 parent
0a1fe7c
commit aee96db
Showing
2 changed files
with
124 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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,83 @@ | ||
import "package:flutter/material.dart"; | ||
import "package:flutter_test/flutter_test.dart"; | ||
import "package:kaiteki/model/auth/account.dart"; | ||
import "package:kaiteki/model/auth/account_key.dart"; | ||
import "package:kaiteki/ui/shared/posts/post_widget.dart"; | ||
import "package:kaiteki_core/kaiteki_core.dart"; | ||
|
||
import "utils/bootstrap.dart"; | ||
import "utils/dummy_adapter.dart"; | ||
|
||
void main() { | ||
testWidgets("Test post widget state updating", (tester) async { | ||
final boot = await Bootstrapper.getInstance( | ||
initialAccounts: [ | ||
Account( | ||
key: const AccountKey(ApiType.mastodon, "example.com", "alice"), | ||
adapter: DummyAdapter(), | ||
accountSecret: null, | ||
clientSecret: null, | ||
user: exampleUser, | ||
), | ||
], | ||
); | ||
|
||
final post = Post( | ||
id: "1", | ||
content: "Hello world", | ||
postedAt: DateTime.now(), | ||
author: exampleUser, | ||
); | ||
|
||
final key = GlobalKey<_TestPostWidgetState>(); | ||
|
||
await tester.pumpWidget( | ||
boot.wrap(_TestPostWidget(post: post, key: key)), | ||
); | ||
|
||
final contentFinder = find.text(post.content!); | ||
|
||
expect(contentFinder, findsOneWidget); | ||
|
||
final newPost = post.copyWith(content: "Hello world 2"); | ||
|
||
key.currentState!.post = newPost; | ||
|
||
await tester.pumpAndSettle(); | ||
|
||
expect(contentFinder, findsNothing); | ||
}); | ||
} | ||
|
||
class _TestPostWidget extends StatefulWidget { | ||
final Post post; | ||
|
||
const _TestPostWidget({required this.post, super.key}); | ||
|
||
@override | ||
State<_TestPostWidget> createState() => _TestPostWidgetState(); | ||
} | ||
|
||
class _TestPostWidgetState extends State<_TestPostWidget> { | ||
late Post _post; | ||
|
||
set post(Post post) => setState(() => _post = post); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
_post = widget.post; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return Scaffold( | ||
body: Center( | ||
child: ConstrainedBox( | ||
constraints: const BoxConstraints(maxWidth: 600), | ||
child: PostWidget(_post), | ||
), | ||
), | ||
); | ||
} | ||
} |