Skip to content

Commit

Permalink
Merge pull request #5421 from jay-hodgson/SWC-6869
Browse files Browse the repository at this point in the history
  • Loading branch information
jay-hodgson committed Jun 14, 2024
2 parents 7d8c30e + b13897a commit cf167df
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@ public void setAppPlaceHistoryMapper(
void back();

boolean isShowingVersionAlert();

boolean handleRelativePathClick(String href);
}
Original file line number Diff line number Diff line change
Expand Up @@ -570,19 +570,32 @@ public void onPreviewNativeEvent(NativePreviewEvent event) {
);
// SWC-5812: SRC has components that apply this special CSS class, and expect the app to look for these events and handle appropriately.
// When the class name is undefined, targetElement.getClassName() still returns an object where isEmpty is false and is not equal to null.
boolean isSRCSignInClass = false;
try {
if (
isSRCSignInClass =
targetElement.hasClassName("SRC-SIGN-IN-CLASS") &&
!ginInjector.getAuthenticationController().isLoggedIn()
) {
getPlaceChanger().goTo(new LoginPlace(LoginPlace.LOGIN_TOKEN));
}
!ginInjector.getAuthenticationController().isLoggedIn();
} catch (Exception e) {
// SWC-6243: log the error if it is not the known problem of finding the className
if (!e.getMessage().contains("indexOf")) {
synapseJSNIUtils.consoleError(e.getMessage());
}
}
try {
if (isSRCSignInClass) {
getPlaceChanger().goTo(new LoginPlace(LoginPlace.LOGIN_TOKEN));
} else {
if (targetElement.hasAttribute("href")) {
String href = targetElement.getAttribute("href");
boolean handled = handleRelativePathClick(href);
if (handled) {
event.cancel();
}
}
}
} catch (Exception e) {
synapseJSNIUtils.consoleError(e.getMessage());
}
}
}
}
Expand All @@ -596,6 +609,20 @@ public void onPreviewNativeEvent(NativePreviewEvent event) {
}
}

@Override
public boolean handleRelativePathClick(String href) {
String placeToken = StringUtils.getGWTPlaceTokenFromURL(href);
if (placeToken != null) {
AppPlaceHistoryMapper appPlaceHistoryMapper = getAppPlaceHistoryMapper();
Place newPlace = appPlaceHistoryMapper.getPlace(placeToken);
if (newPlace != null) {
getPlaceChanger().goTo(newPlace);
return true;
}
}
return false;
}

@Override
public void back() {
view.back();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@ public interface Callback {
void run();
}

@JsFunction
public interface Callback2 {
void run(String targetHref);
}

public Callback signoutCallback;
public Callback2 gotoPlace;

@JsOverlay
public static SynapseNavDrawerProps create(Callback signoutCallback) {
public static SynapseNavDrawerProps create(
Callback signoutCallback,
Callback2 gotoPlace
) {
SynapseNavDrawerProps props = new SynapseNavDrawerProps();
props.signoutCallback = signoutCallback;
props.gotoPlace = gotoPlace;
return props;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,16 @@ public HeaderViewImpl(
public void clear() {}

public void rerenderNavBar() {
SynapseNavDrawerProps props = SynapseNavDrawerProps.create(() -> {
ginInjector.getAuthenticationController().logoutUser();
});
SynapseNavDrawerProps props = SynapseNavDrawerProps.create(
() -> {
ginInjector.getAuthenticationController().logoutUser();
},
href -> {
GlobalApplicationState globalAppState =
ginInjector.getGlobalApplicationState();
globalAppState.handleRelativePathClick(href);
}
);
ReactNode component = React.createElementWithSynapseContext(
SRC.SynapseComponents.SynapseNavDrawer,
props,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.sagebionetworks.web.client.DateTimeUtils;
import org.sagebionetworks.web.client.GWTWrapper;
Expand All @@ -50,6 +49,7 @@
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.mvp.AppActivityMapper;
import org.sagebionetworks.web.client.mvp.AppPlaceHistoryMapper;
import org.sagebionetworks.web.client.place.Home;
import org.sagebionetworks.web.client.place.Synapse;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.widget.footer.VersionState;
Expand All @@ -61,12 +61,24 @@ public class GlobalApplicationStateImplTest {
@Mock
StackConfigServiceAsync mockStackConfigService;

@Mock
CookieProvider mockCookieProvider;

@Mock
PlaceController mockPlaceController;

@Mock
EventBus mockEventBus;

GlobalApplicationStateImpl globalApplicationState;

@Mock
AppPlaceHistoryMapper mockAppPlaceHistoryMapper;

@Mock
SynapseJSNIUtils mockSynapseJSNIUtils;

@Mock
GlobalApplicationStateView mockView;

@Mock
Expand Down Expand Up @@ -96,19 +108,16 @@ public class GlobalApplicationStateImplTest {
@Mock
SessionStorage mockSessionStorage;

@Captor
ArgumentCaptor<Place> placeCaptor;

public static final String REPO_ENDPOINT =
"https://repo-staging.prod.sagebase.org/";
public static final String SWC_ENDPOINT = "https://staging.synapse.org/";

@Before
public void before() {
MockitoAnnotations.initMocks(this);
mockCookieProvider = Mockito.mock(CookieProvider.class);
mockPlaceController = Mockito.mock(PlaceController.class);
mockEventBus = Mockito.mock(EventBus.class);
mockAppPlaceHistoryMapper = mock(AppPlaceHistoryMapper.class);
mockSynapseJSNIUtils = mock(SynapseJSNIUtils.class);
mockView = mock(GlobalApplicationStateView.class);
AsyncMockStubber
.callSuccessWith("v1")
.when(mockJsClient)
Expand Down Expand Up @@ -512,4 +521,55 @@ public void testSetShowUTCTime() {
any(Date.class)
);
}

@Test
public void testHandleClickSynapsePlace() {
when(mockPlaceController.getWhere()).thenReturn(mock(Home.class));
Synapse mockSynapsePlace = mock(Synapse.class);
when(mockAppPlaceHistoryMapper.getPlace("Synapse:syn123"))
.thenReturn(mockSynapsePlace);
String href = "http://www.synapse.org/Synapse:syn123";

boolean isHandled = globalApplicationState.handleRelativePathClick(href);

assertTrue(isHandled);
verify(mockPlaceController).goTo(mockSynapsePlace);
}

@Test
public void testHandleRelativePathClickSynapsePlace() {
when(mockPlaceController.getWhere()).thenReturn(mock(Home.class));
Synapse mockSynapsePlace = mock(Synapse.class);
when(mockAppPlaceHistoryMapper.getPlace("Synapse:syn123"))
.thenReturn(mockSynapsePlace);
String href = "/Synapse:syn123";

boolean isHandled = globalApplicationState.handleRelativePathClick(href);

assertTrue(isHandled);
verify(mockPlaceController).goTo(mockSynapsePlace);
}

@Test
public void testHandleRelativePathClickInvalidFormat() {
when(mockPlaceController.getWhere()).thenReturn(mock(Home.class));
String href = "https://www.synapse.org/notaplace";

boolean isHandled = globalApplicationState.handleRelativePathClick(href);

assertFalse(isHandled);
verify(mockPlaceController, never()).goTo(any(Place.class));
}

@Test
public void testHandleRelativePathNotAGWTPlace() {
when(mockPlaceController.getWhere()).thenReturn(mock(Home.class));
String href =
"https://www.synapse.org/AppearsToBeCorrectFormatButNotAPlace:syn1234";

boolean isHandled = globalApplicationState.handleRelativePathClick(href);

assertFalse(isHandled);
verify(mockPlaceController, never()).goTo(any(Place.class));
}
}

0 comments on commit cf167df

Please sign in to comment.