-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Allow overriding the route filtering using a ctor param `route…
…Filter` (#95)
- Loading branch information
1 parent
f5b8658
commit 8e3a6f3
Showing
7 changed files
with
185 additions
and
15 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
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
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,13 @@ | ||
import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; | ||
|
||
class PosthogFlutterPlatformFake extends PosthogFlutterPlatformInterface { | ||
String? screenName; | ||
|
||
@override | ||
Future<void> screen({ | ||
required String screenName, | ||
Map<String, Object>? properties, | ||
}) async { | ||
this.screenName = screenName; | ||
} | ||
} |
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,115 @@ | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:posthog_flutter/src/posthog_flutter_io.dart'; | ||
import 'package:posthog_flutter/src/posthog_flutter_platform_interface.dart'; | ||
import 'package:posthog_flutter/src/posthog_observer.dart'; | ||
|
||
import 'posthog_flutter_platform_interface_fake.dart'; | ||
|
||
void main() { | ||
PageRoute<dynamic> route(RouteSettings? settings) => PageRouteBuilder<void>( | ||
pageBuilder: (_, __, ___) => Container(), | ||
settings: settings, | ||
); | ||
|
||
final fake = PosthogFlutterPlatformFake(); | ||
|
||
setUp(() { | ||
TestWidgetsFlutterBinding.ensureInitialized(); | ||
PosthogFlutterPlatformInterface.instance = fake; | ||
}); | ||
|
||
tearDown(() { | ||
fake.screenName = null; | ||
PosthogFlutterPlatformInterface.instance = PosthogFlutterIO(); | ||
}); | ||
|
||
PosthogObserver getSut( | ||
{ScreenNameExtractor nameExtractor = defaultNameExtractor, | ||
PostHogRouteFilter routeFilter = defaultPostHogRouteFilter}) { | ||
return PosthogObserver( | ||
nameExtractor: nameExtractor, routeFilter: routeFilter); | ||
} | ||
|
||
test('returns current route name', () { | ||
final currentRoute = route(const RouteSettings(name: 'Current Route')); | ||
|
||
final sut = getSut(); | ||
sut.didPush(currentRoute, null); | ||
|
||
expect(fake.screenName, 'Current Route'); | ||
}); | ||
|
||
test('returns overriden route name', () { | ||
final currentRoute = route(const RouteSettings(name: 'Current Route')); | ||
|
||
String? nameExtractor(RouteSettings settings) => 'overriden'; | ||
|
||
final sut = getSut(nameExtractor: nameExtractor); | ||
sut.didPush(currentRoute, null); | ||
|
||
expect(fake.screenName, 'overriden'); | ||
}); | ||
|
||
test('returns overriden root route name', () { | ||
final currentRoute = route(const RouteSettings(name: '/')); | ||
|
||
final sut = getSut(); | ||
sut.didPush(currentRoute, null); | ||
|
||
expect(fake.screenName, 'root (\'/\')'); | ||
}); | ||
|
||
test('does not capture not named routes', () { | ||
final currentRoute = route(const RouteSettings(name: null)); | ||
|
||
final sut = getSut(); | ||
sut.didPush(currentRoute, null); | ||
|
||
expect(fake.screenName, null); | ||
}); | ||
|
||
test('does not capture blank routes', () { | ||
final currentRoute = route(const RouteSettings(name: ' ')); | ||
|
||
final sut = getSut(); | ||
sut.didPush(currentRoute, null); | ||
|
||
expect(fake.screenName, null); | ||
}); | ||
|
||
test('does not capture filtered routes', () { | ||
// CustomOverlawRoute isn't a PageRoute | ||
final overlayRoute = CustomOverlawRoute( | ||
settings: const RouteSettings(name: 'Overlay Route'), | ||
); | ||
|
||
final sut = getSut(); | ||
sut.didPush(overlayRoute, null); | ||
|
||
expect(fake.screenName, null); | ||
}); | ||
|
||
test('allows overriding the route filter', () { | ||
final overlayRoute = CustomOverlawRoute( | ||
settings: const RouteSettings(name: 'Overlay Route'), | ||
); | ||
|
||
bool defaultPostHogRouteFilter(Route<dynamic>? route) => | ||
route is PageRoute || route is OverlayRoute; | ||
|
||
final sut = getSut(routeFilter: defaultPostHogRouteFilter); | ||
sut.didPush(overlayRoute, null); | ||
|
||
expect(fake.screenName, 'Overlay Route'); | ||
}); | ||
} | ||
|
||
class CustomOverlawRoute extends OverlayRoute { | ||
CustomOverlawRoute({super.settings}); | ||
|
||
@override | ||
Iterable<OverlayEntry> createOverlayEntries() { | ||
return []; | ||
} | ||
} |