-
-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #64 from Blazored/63-regression-int-serialisation
Fixes bug in deserialising logic
- Loading branch information
Showing
10 changed files
with
181 additions
and
79 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/Blazored.LocalStorage.Tests/Blazored.LocalStorage.Tests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp3.1</TargetFramework> | ||
|
||
<IsPackable>false</IsPackable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="FluentAssertions" Version="5.10.3" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" /> | ||
<PackageReference Include="Moq" Version="4.13.1" /> | ||
<PackageReference Include="System.Text.Json" Version="4.7.1" /> | ||
<PackageReference Include="xunit" Version="2.4.1" /> | ||
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.1"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="coverlet.collector" Version="1.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Blazored.LocalStorage\Blazored.LocalStorage.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
108 changes: 108 additions & 0 deletions
108
tests/Blazored.LocalStorage.Tests/LocalStorageServiceTests/GetItemAsync.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
using Blazored.LocalStorage.JsonConverters; | ||
using Blazored.LocalStorage.Tests.Mocks; | ||
using FluentAssertions; | ||
using Moq; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Blazored.LocalStorage.Tests.LocalStorageServiceTests | ||
{ | ||
public class GetItemAsync | ||
{ | ||
private JsonSerializerOptions _jsonOptions; | ||
private Mock<JSRuntimeWrapper> _mockJSRuntime; | ||
private LocalStorageService _sut; | ||
|
||
private static string _key = "testKey"; | ||
|
||
public GetItemAsync() | ||
{ | ||
_mockJSRuntime = new Mock<JSRuntimeWrapper>(); | ||
_jsonOptions = new JsonSerializerOptions(); | ||
_jsonOptions.Converters.Add(new TimespanJsonConverter()); | ||
_sut = new LocalStorageService(_mockJSRuntime.Object); | ||
} | ||
|
||
[Theory] | ||
[InlineData("stringTest")] | ||
[InlineData(11)] | ||
[InlineData(11.11)] | ||
public async Task Should_DeserialiseToCorrectType<T>(T value) | ||
{ | ||
// Arrange | ||
var serialisedData = ""; | ||
if (typeof(T) == typeof(string)) | ||
serialisedData = value.ToString(); | ||
else | ||
serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => new ValueTask<string>(serialisedData)); | ||
|
||
// Act | ||
var result = await _sut.GetItemAsync<T>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
_mockJSRuntime.Verify(); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_DeserialiseValueToNullableInt() | ||
{ | ||
// Arrange | ||
int? value = 6; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => new ValueTask<string>(serialisedData)); | ||
|
||
// Act | ||
var result = await _sut.GetItemAsync<int?>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_DeserialiseValueToDecimal() | ||
{ | ||
// Arrange | ||
decimal value = 6.00m; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => new ValueTask<string>(serialisedData)); | ||
|
||
// Act | ||
var result = await _sut.GetItemAsync<decimal>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_DeserialiseValueToComplexType() | ||
{ | ||
// Arrange | ||
TestObject value = new TestObject { Id = 1, Name = "John Smith" }; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => new ValueTask<string>(serialisedData)); | ||
|
||
// Act | ||
var result = await _sut.GetItemAsync<TestObject>(_key); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(value); | ||
} | ||
} | ||
|
||
public class TestObject | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
tests/Blazored.LocalStorage.Tests/Mocks/JSRuntimeWrapper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
using Microsoft.JSInterop; | ||
using Microsoft.JSInterop.Infrastructure; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Blazored.LocalStorage.Tests.Mocks | ||
{ | ||
public class JSRuntimeWrapper : IJSRuntime | ||
{ | ||
public virtual ValueTask<TValue> InvokeAsync<TValue>(string identifier, object[] args) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public virtual ValueTask<TValue> InvokeAsync<TValue>(string identifier, CancellationToken cancellationToken, object[] args) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |