Skip to content

Commit c170342

Browse files
committed
feat: implement isActive method for route matching
1 parent c635c59 commit c170342

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/src/infra/services/routing_kit_routing_service.dart

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,16 @@ class RoutingKitRoutingService<T, Config extends Object>
4343

4444
@override
4545
bool isActive(String path, {bool exact = true, bool ignoreParams = false}) {
46-
// TODO: implement isActive
47-
throw UnimplementedError();
46+
final context = _context;
47+
if (context == null) return false;
48+
49+
final currentUri = context.uri;
50+
final targetUri = Uri.parse(path);
51+
52+
final current = ignoreParams ? currentUri.path : currentUri.toString();
53+
final target = ignoreParams ? targetUri.path : targetUri.toString();
54+
55+
return exact ? current == target : current.startsWith(target);
4856
}
4957

5058
@override

test/routing_test.dart

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,38 @@ void main() {
190190
expect(root.featureModule.activateCalls, 1);
191191
},
192192
);
193+
194+
test('isActive returns false when no route is active', () {
195+
expect(routing.isActive('/feature/child'), isFalse);
196+
expect(routing.isActive('/feature', exact: false), isFalse);
197+
});
198+
199+
test('isActive supports exact and partial matches', () async {
200+
await routing.navigate('/feature/child');
201+
202+
expect(routing.isActive('/feature/child'), isTrue);
203+
expect(routing.isActive('/feature'), isFalse);
204+
expect(routing.isActive('/feature', exact: false), isTrue);
205+
expect(routing.isActive('/feature/sub', exact: false), isFalse);
206+
});
207+
208+
test('isActive can ignore query params and fragments', () async {
209+
await routing.navigate('/feature/child?tab=profile#details');
210+
211+
expect(
212+
routing.isActive('/feature/child?tab=profile#details'),
213+
isTrue,
214+
);
215+
expect(routing.isActive('/feature/child'), isFalse);
216+
expect(
217+
routing.isActive('/feature/child', ignoreParams: true),
218+
isTrue,
219+
);
220+
expect(
221+
routing.isActive('/feature', exact: false, ignoreParams: true),
222+
isTrue,
223+
);
224+
});
193225
});
194226
}
195227

0 commit comments

Comments
 (0)