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 { platform } from './Environment'
|
||||||
import { registerFunction } from './Interop/RegisteredFunction';
|
import { registerFunction } from './Interop/RegisteredFunction';
|
||||||
|
import { navigateTo } from './Services/UriHelper';
|
||||||
|
|
||||||
if (typeof window !== 'undefined') {
|
if (typeof window !== 'undefined') {
|
||||||
// When the library is loaded in a browser via a <script> element, make the
|
// When the library is loaded in a browser via a <script> element, make the
|
||||||
|
|
@ -7,5 +8,6 @@ if (typeof window !== 'undefined') {
|
||||||
window['Blazor'] = {
|
window['Blazor'] = {
|
||||||
platform,
|
platform,
|
||||||
registerFunction,
|
registerFunction,
|
||||||
|
navigateTo,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -33,14 +33,17 @@ registerFunction(`${registeredFunctionPrefix}.enableNavigationInteception`, () =
|
||||||
});
|
});
|
||||||
|
|
||||||
registerFunction(`${registeredFunctionPrefix}.navigateTo`, (uriDotNetString: System_String) => {
|
registerFunction(`${registeredFunctionPrefix}.navigateTo`, (uriDotNetString: System_String) => {
|
||||||
const href = platform.toJavaScriptString(uriDotNetString);
|
navigateTo(platform.toJavaScriptString(uriDotNetString));
|
||||||
if (isWithinBaseUriSpace(toAbsoluteUri(href))) {
|
|
||||||
performInternalNavigation(href);
|
|
||||||
} else {
|
|
||||||
location.href = href;
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export function navigateTo(uri: string) {
|
||||||
|
if (isWithinBaseUriSpace(toAbsoluteUri(uri))) {
|
||||||
|
performInternalNavigation(uri);
|
||||||
|
} else {
|
||||||
|
location.href = uri;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function performInternalNavigation(href: string) {
|
function performInternalNavigation(href: string) {
|
||||||
history.pushState(null, /* ignored title */ '', href);
|
history.pushState(null, /* ignored title */ '', href);
|
||||||
handleInternalNavigation();
|
handleInternalNavigation();
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
|
||||||
{
|
{
|
||||||
_server = serverFixture;
|
_server = serverFixture;
|
||||||
Navigate(ServerPathBase, noReload: true);
|
Navigate(ServerPathBase, noReload: true);
|
||||||
|
WaitUntilDotNetRunningInBrowser();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
|
|
@ -131,7 +132,7 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
|
||||||
{
|
{
|
||||||
var jsExecutor = (IJavaScriptExecutor)Browser;
|
var jsExecutor = (IJavaScriptExecutor)Browser;
|
||||||
var absoluteUri = new Uri(_server.RootUri, relativeUri);
|
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