Implement Blazor.navigateTo for triggering navigation from JS code, and use it in E2E tests

This commit is contained in:
Steve Sanderson 2018-03-16 16:55:46 +00:00
parent 0cfb25e2ad
commit 2150820efe
3 changed files with 13 additions and 7 deletions

View File

@ -1,5 +1,6 @@
import { platform } from './Environment'
import { registerFunction } from './Interop/RegisteredFunction';
import { navigateTo } from './Services/UriHelper';
if (typeof window !== 'undefined') {
// When the library is loaded in a browser via a <script> element, make the
@ -7,5 +8,6 @@ if (typeof window !== 'undefined') {
window['Blazor'] = {
platform,
registerFunction,
navigateTo,
};
}

View File

@ -33,14 +33,17 @@ registerFunction(`${registeredFunctionPrefix}.enableNavigationInteception`, () =
});
registerFunction(`${registeredFunctionPrefix}.navigateTo`, (uriDotNetString: System_String) => {
const href = platform.toJavaScriptString(uriDotNetString);
if (isWithinBaseUriSpace(toAbsoluteUri(href))) {
performInternalNavigation(href);
} else {
location.href = href;
}
navigateTo(platform.toJavaScriptString(uriDotNetString));
});
export function navigateTo(uri: string) {
if (isWithinBaseUriSpace(toAbsoluteUri(uri))) {
performInternalNavigation(uri);
} else {
location.href = uri;
}
}
function performInternalNavigation(href: string) {
history.pushState(null, /* ignored title */ '', href);
handleInternalNavigation();

View File

@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
{
_server = serverFixture;
Navigate(ServerPathBase, noReload: true);
WaitUntilDotNetRunningInBrowser();
}
[Fact]
@ -131,7 +132,7 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
{
var jsExecutor = (IJavaScriptExecutor)Browser;
var absoluteUri = new Uri(_server.RootUri, relativeUri);
jsExecutor.ExecuteScript($"history.pushState(null, '', '{absoluteUri.ToString()}')");
jsExecutor.ExecuteScript($"Blazor.navigateTo('{absoluteUri.ToString()}')");
}
}
}