Skip to content

Commit

Permalink
fix: improve locationPrefix impl (#10719)
Browse files Browse the repository at this point in the history
fixes #4730
  • Loading branch information
Denis committed Apr 20, 2021
1 parent 2ebd653 commit a1663b6
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ public static HighlightCondition<RouterLink> sameLocation() {
* @return the highlight condition
*/
public static HighlightCondition<RouterLink> locationPrefix() {
return (link, event) -> event.getLocation().getPath()
.startsWith(link.getHref());
return (link, event) -> link.getHref().isEmpty()
? event.getLocation().getPath().isEmpty()
: event.getLocation().getPath().startsWith(link.getHref());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright 2000-2021 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.router;

import org.junit.Assert;
import org.junit.Test;
import org.mockito.Mockito;

public class HighlightConditionsTest {

@Test
public void locationPrefix_defaultRoute_emptyLocationMatches() {
HighlightCondition<RouterLink> condition = HighlightConditions
.locationPrefix();
RouterLink link = Mockito.mock(RouterLink.class);
AfterNavigationEvent event = Mockito.mock(AfterNavigationEvent.class);

Mockito.when(link.getHref()).thenReturn("");
Location location = new Location("");
Mockito.when(event.getLocation()).thenReturn(location);
Assert.assertTrue(condition.shouldHighlight(link, event));
}

@Test
public void locationPrefix_defaultRoute_nonEmptyLocationDoesNotMatch() {
HighlightCondition<RouterLink> condition = HighlightConditions
.locationPrefix();
RouterLink link = Mockito.mock(RouterLink.class);
AfterNavigationEvent event = Mockito.mock(AfterNavigationEvent.class);

Mockito.when(link.getHref()).thenReturn("");
Location location = new Location("foo");
Mockito.when(event.getLocation()).thenReturn(location);
Assert.assertFalse(condition.shouldHighlight(link, event));
}

@Test
public void locationPrefix_notDefaultRoute_prefixMatches() {
HighlightCondition<RouterLink> condition = HighlightConditions
.locationPrefix();
RouterLink link = Mockito.mock(RouterLink.class);
AfterNavigationEvent event = Mockito.mock(AfterNavigationEvent.class);

Mockito.when(link.getHref()).thenReturn("foo");
Location location = new Location("foobar");
Mockito.when(event.getLocation()).thenReturn(location);
Assert.assertTrue(condition.shouldHighlight(link, event));
}

@Test
public void locationPrefix_notDefaultRoute_nonPrefixDoesNotMatch() {
HighlightCondition<RouterLink> condition = HighlightConditions
.locationPrefix();
RouterLink link = Mockito.mock(RouterLink.class);
AfterNavigationEvent event = Mockito.mock(AfterNavigationEvent.class);

Mockito.when(link.getHref()).thenReturn("foo");
Location location = new Location("bar");
Mockito.when(event.getLocation()).thenReturn(location);
Assert.assertFalse(condition.shouldHighlight(link, event));
}
}

0 comments on commit a1663b6

Please sign in to comment.