|
| 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.Infrastructure; |
| 6 | +using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures; |
| 7 | +using Microsoft.AspNetCore.E2ETesting; |
| 8 | +using Microsoft.JSInterop; |
| 9 | +using OpenQA.Selenium; |
| 10 | +using Xunit.Abstractions; |
| 11 | + |
| 12 | +namespace Microsoft.AspNetCore.Components.E2ETests.Tests; |
| 13 | + |
| 14 | +public class JSInteropValueTypesTest: ServerTestBase<BlazorWasmTestAppFixture<Program>> |
| 15 | +{ |
| 16 | + private IJSRuntime _jsRuntime; |
| 17 | + public JSInteropValueTypesTest( |
| 18 | + BrowserFixture browserFixture, |
| 19 | + BlazorWasmTestAppFixture<Program> serverFixture, |
| 20 | + ITestOutputHelper output, |
| 21 | + IJSRuntime jsRuntime) |
| 22 | + : base(browserFixture, serverFixture, output) |
| 23 | + { |
| 24 | + _serverFixture.PathBase = ServerPathBase; |
| 25 | + _jsRuntime = jsRuntime; |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public async Task CanStoreGuid() |
| 30 | + { |
| 31 | + var guid = Guid.NewGuid(); |
| 32 | + |
| 33 | + // Store guid in localStorage |
| 34 | + await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "guid", guid); |
| 35 | + |
| 36 | + // Retrieve guid from localStorage as string because that "conversion" always works |
| 37 | + var storedGuid = await _jsRuntime.InvokeAsync<string>("localStorage.getItem", "guid"); |
| 38 | + |
| 39 | + Assert.Equal(guid.ToString(), storedGuid); |
| 40 | + } |
| 41 | + |
| 42 | + [Fact] |
| 43 | + public async Task CanRetrieveStoredGuid() |
| 44 | + { |
| 45 | + var guid = Guid.NewGuid(); |
| 46 | + |
| 47 | + // Store guid in localStorage |
| 48 | + await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "guid", guid); |
| 49 | + |
| 50 | + // Retrieve guid from localStorage |
| 51 | + var storedGuid = await _jsRuntime.InvokeAsync<Guid>("localStorage.getItem", "guid"); |
| 52 | + |
| 53 | + Assert.Equal(guid, storedGuid); |
| 54 | + } |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public async Task CanRetrieveNullAsNullable() |
| 58 | + { |
| 59 | + // Store null in localStorage |
| 60 | + await _jsRuntime.InvokeVoidAsync("localStorage.setItem", "nullHere", null); |
| 61 | + |
| 62 | + // Retrieve null from localStorage |
| 63 | + var storedNull = await _jsRuntime.InvokeAsync<Guid?>("localStorage.getItem", "nullHere"); |
| 64 | + |
| 65 | + Assert.Null(storedNull); |
| 66 | + } |
| 67 | +} |
0 commit comments