Implement Blazor.navigateTo for triggering navigation from JS code, and use it in E2E tests
This commit is contained in:
parent
0cfb25e2ad
commit
2150820efe
|
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
|
|
@ -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()}')");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue