|
7 | 7 | library;
|
8 | 8 |
|
9 | 9 | import 'dart:async';
|
| 10 | +import 'dart:js_interop' |
| 11 | + show JSExportedDartFunction, JSExportedDartFunctionToFunction; |
10 | 12 |
|
11 | 13 | import 'package:quiver/testing/async.dart';
|
12 | 14 | import 'package:test/bootstrap/browser.dart';
|
13 | 15 | import 'package:test/test.dart';
|
14 |
| -import 'package:ui/src/engine.dart' show DomEventListener, window; |
| 16 | +import 'package:ui/src/engine.dart' show window; |
15 | 17 | import 'package:ui/src/engine/browser_detection.dart';
|
| 18 | +import 'package:ui/src/engine/dom.dart' |
| 19 | + show DomEvent, DomEventListener, createDomPopStateEvent; |
16 | 20 | import 'package:ui/src/engine/navigation.dart';
|
17 | 21 | import 'package:ui/src/engine/services.dart';
|
18 | 22 | import 'package:ui/src/engine/test_embedding.dart';
|
@@ -647,6 +651,31 @@ void testMain() {
|
647 | 651 | location.hash = '#';
|
648 | 652 | expect(strategy.getPath(), '/');
|
649 | 653 | });
|
| 654 | + |
| 655 | + test('addPopStateListener fn unwraps DomPopStateEvent state', () { |
| 656 | + final HashUrlStrategy strategy = HashUrlStrategy(location); |
| 657 | + const String expected = 'expected value'; |
| 658 | + final List<Object?> states = <Object?>[]; |
| 659 | + |
| 660 | + // Put the popStates received from the `location` in a list |
| 661 | + strategy.addPopStateListener(states.add); |
| 662 | + |
| 663 | + // Simulate a popstate with a null state: |
| 664 | + location.debugTriggerPopState(null); |
| 665 | + |
| 666 | + expect(states, hasLength(1)); |
| 667 | + expect(states[0], isNull); |
| 668 | + |
| 669 | + // Simulate a popstate event with `expected` as its 'state'. |
| 670 | + location.debugTriggerPopState(expected); |
| 671 | + |
| 672 | + expect(states, hasLength(2)); |
| 673 | + final Object? state = states[1]; |
| 674 | + expect(state, isNotNull); |
| 675 | + // flutter/flutter#125228 |
| 676 | + expect(state, isNot(isA<DomEvent>())); |
| 677 | + expect(state, expected); |
| 678 | + }); |
650 | 679 | });
|
651 | 680 | }
|
652 | 681 |
|
@@ -694,15 +723,32 @@ class TestPlatformLocation extends PlatformLocation {
|
694 | 723 | @override
|
695 | 724 | dynamic state;
|
696 | 725 |
|
| 726 | + List<DomEventListener> popStateListeners = <DomEventListener>[]; |
| 727 | + |
697 | 728 | @override
|
698 | 729 | String get pathname => throw UnimplementedError();
|
699 | 730 |
|
700 | 731 | @override
|
701 | 732 | String get search => throw UnimplementedError();
|
702 | 733 |
|
| 734 | + /// Calls all the registered `popStateListeners` with a 'popstate' |
| 735 | + /// event with value `state` |
| 736 | + void debugTriggerPopState(Object? state) { |
| 737 | + final DomEvent event = createDomPopStateEvent( |
| 738 | + 'popstate', |
| 739 | + <Object, Object>{ |
| 740 | + if (state != null) 'state': state, |
| 741 | + }, |
| 742 | + ); |
| 743 | + for (final DomEventListener listener in popStateListeners) { |
| 744 | + final Function fn = (listener as JSExportedDartFunction).toDart; |
| 745 | + fn(event); |
| 746 | + } |
| 747 | + } |
| 748 | + |
703 | 749 | @override
|
704 | 750 | void addPopStateListener(DomEventListener fn) {
|
705 |
| - throw UnimplementedError(); |
| 751 | + popStateListeners.add(fn); |
706 | 752 | }
|
707 | 753 |
|
708 | 754 | @override
|
|
0 commit comments