Skip to content

Commit fe68842

Browse files
committed
Test JSRuntime for value type handling
1 parent 8b02460 commit fe68842

File tree

3 files changed

+142
-1
lines changed

3 files changed

+142
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using BasicTestApp;
5+
using Microsoft.AspNetCore.Components.E2ETest;
6+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
7+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
8+
using Microsoft.AspNetCore.E2ETesting;
9+
using Microsoft.JSInterop;
10+
using OpenQA.Selenium;
11+
using Xunit.Abstractions;
12+
13+
namespace Microsoft.AspNetCore.Components.E2ETests.Tests;
14+
15+
public class InteropValueTypesTest: ServerTestBase<BlazorWasmTestAppFixture<Program>>
16+
{
17+
public InteropValueTypesTest(
18+
BrowserFixture browserFixture,
19+
BlazorWasmTestAppFixture<Program> serverFixture,
20+
ITestOutputHelper output)
21+
: base(browserFixture, serverFixture, output)
22+
{
23+
}
24+
25+
protected override void InitializeAsyncCore()
26+
{
27+
Navigate(ServerPathBase);
28+
Browser.MountTestComponent<InteropValueTypesComponent>();
29+
}
30+
31+
[Fact]
32+
public void CanRetrieveStoredGuidAsString()
33+
{
34+
Browser.Exists(By.Id("done-with-interop"));
35+
36+
var stringGuidElement = Browser.Exists(By.Id("string-get-by-interop"));
37+
38+
Browser.NotEqual(string.Empty, () => stringGuidElement.Text);
39+
Browser.NotEqual(default, () => Guid.Parse(stringGuidElement.Text));
40+
}
41+
42+
[Fact]
43+
public void CanRetrieveStoredGuidAsGuid()
44+
{
45+
Browser.Exists(By.Id("done-with-interop"));
46+
47+
var guidElement = Browser.Exists(By.Id("guid-get-by-interop"));
48+
49+
Browser.NotEqual(default, () => Guid.Parse(guidElement.Text));
50+
}
51+
52+
[Fact]
53+
public void CanRetrieveStoredGuidAsNullableGuid()
54+
{
55+
Browser.Exists(By.Id("done-with-interop"));
56+
57+
var nullableGuidElement = Browser.Exists(By.Id("nullable-guid-get-by-interop"));
58+
59+
Browser.NotEqual(default, () => Guid.Parse(nullableGuidElement.Text));
60+
}
61+
62+
[Fact]
63+
public void CanRetrieveNullAsNullableGuid()
64+
{
65+
Browser.Exists(By.Id("done-with-interop"));
66+
67+
var nullableGuidElement = Browser.Exists(By.Id("null-loaded-into-nullable"));
68+
69+
Browser.Equal(false.ToString(), () => nullableGuidElement.Text);
70+
}
71+
}

src/Components/test/E2ETest/Tests/StartupErrorNotificationTest.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public StartupErrorNotificationTest(
2727
public void DisplaysNotificationForStartupException(bool errorIsAsync)
2828
{
2929
var url = $"{ServerPathBase}?error={(errorIsAsync ? "async" : "sync")}";
30-
30+
3131
Navigate(url);
3232
var errorUiElem = Browser.Exists(By.Id("blazor-error-ui"), TimeSpan.FromSeconds(10));
3333
Assert.NotNull(errorUiElem);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
@using Microsoft.JSInterop
2+
@inject IJSRuntime JSRuntime
3+
4+
<p>
5+
This component shows it's possible to save and load value types (in this case, a guid)
6+
to and from localstorage,
7+
and also that a null value can be loaded into a nullable value type, i.e. a Guid?.
8+
</p>
9+
10+
<p>
11+
String value from guid:
12+
<strong id="string-get-by-interop">@(stringFromInterop ?? "No value yet")</strong>
13+
</p>
14+
15+
<p>
16+
Guid value from guid is default(Guid) if it wasn't loaded correctly:
17+
<strong id="guid-get-by-interop">@guidFromInterop</strong>
18+
</p>
19+
20+
<p>
21+
Nullable Guid value from guid is null if it wasn't loaded correctly:
22+
<strong id="nullable-guid-get-by-interop">@nullableGuidFromInterop</strong>
23+
</p>
24+
25+
<p>
26+
Nullable load result is false if the null value wasn't loaded into the nullable guid correctly:
27+
<strong id="null-loaded-into-nullable">@nullableLoadResult</strong>
28+
</p>
29+
30+
@if (doneWithInterop)
31+
{
32+
<p id="done-with-interop">
33+
Done with interop!
34+
</p>
35+
}
36+
37+
@code {
38+
readonly Guid guid = Guid.NewGuid();
39+
string stringFromInterop;
40+
Guid guidFromInterop;
41+
Guid? nullableGuidFromInterop;
42+
bool nullableLoadResult;
43+
44+
bool doneWithInterop;
45+
46+
protected override async Task OnInitializedAsync()
47+
{
48+
// Store guid in localStorage
49+
await JSRuntime.InvokeVoidAsync("localStorage.setItem", "guid", guid);
50+
51+
// Retrieve guid from localStorage as string because it's a simple conversion
52+
stringFromInterop = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "guid");
53+
54+
// Retrieve guid from localStorage as Guid
55+
guidFromInterop = await JSRuntime.InvokeAsync<Guid>("localStorage.getItem", "guid");
56+
57+
// Retrieve guid from localStorage as Guid?
58+
nullableGuidFromInterop = await JSRuntime.InvokeAsync<Guid?>("localStorage.getItem", "guid");
59+
60+
// Retrieve null from localStorage as Guid?
61+
var nullableGuid = await JSRuntime.InvokeAsync<Guid?>("localStorage.getItem", "nothingHere");
62+
63+
// If the retrieval crashes, the bool stays false
64+
nullableLoadResult = !nullableGuid.HasValue;
65+
66+
doneWithInterop = true;
67+
68+
StateHasChanged();
69+
}
70+
}

0 commit comments

Comments
 (0)