-
-
Notifications
You must be signed in to change notification settings - Fork 119
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 #66 from Blazored/65-fix-serialisation
Fix for deserialisation issue when overwriting existing value
- Loading branch information
Showing
9 changed files
with
307 additions
and
35 deletions.
There are no files selected for viewing
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
103 changes: 103 additions & 0 deletions
103
tests/Blazored.LocalStorage.Tests/LocalStorageServiceTests/GetItem.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,103 @@ | ||
using Blazored.LocalStorage.JsonConverters; | ||
using Blazored.LocalStorage.Tests.Mocks; | ||
using Blazored.LocalStorage.Tests.TestAssets; | ||
using FluentAssertions; | ||
using Moq; | ||
using System.Text.Json; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace Blazored.LocalStorage.Tests.LocalStorageServiceTests | ||
{ | ||
public class GetItem | ||
{ | ||
private JsonSerializerOptions _jsonOptions; | ||
private Mock<JSRuntimeWrapper> _mockJSRuntime; | ||
private LocalStorageService _sut; | ||
|
||
private static string _key = "testKey"; | ||
|
||
public GetItem() | ||
{ | ||
_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 void 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.Invoke<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => serialisedData); | ||
|
||
// Act | ||
var result = _sut.GetItem<T>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
_mockJSRuntime.Verify(); | ||
} | ||
|
||
[Fact] | ||
public void Should_DeserialiseValueToNullableInt() | ||
{ | ||
// Arrange | ||
int? value = 6; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.Invoke<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => serialisedData); | ||
|
||
// Act | ||
var result = _sut.GetItem<int?>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
} | ||
|
||
[Fact] | ||
public void Should_DeserialiseValueToDecimal() | ||
{ | ||
// Arrange | ||
decimal value = 6.00m; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.Invoke<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => serialisedData); | ||
|
||
// Act | ||
var result = _sut.GetItem<decimal>(_key); | ||
|
||
// Assert | ||
Assert.Equal(value, result); | ||
} | ||
|
||
[Fact] | ||
public void Should_DeserialiseValueToComplexType() | ||
{ | ||
// Arrange | ||
TestObject value = new TestObject { Id = 1, Name = "John Smith" }; | ||
var serialisedData = JsonSerializer.Serialize(value, _jsonOptions); | ||
|
||
_mockJSRuntime.Setup(x => x.Invoke<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => serialisedData); | ||
|
||
// Act | ||
var result = _sut.GetItem<TestObject>(_key); | ||
|
||
// Assert | ||
result.Should().BeEquivalentTo(value); | ||
} | ||
} | ||
} |
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
44 changes: 44 additions & 0 deletions
44
tests/Blazored.LocalStorage.Tests/LocalStorageServiceTests/SetItem.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,44 @@ | ||
using Blazored.LocalStorage.JsonConverters; | ||
using Blazored.LocalStorage.Tests.Mocks; | ||
using FluentAssertions; | ||
using Moq; | ||
using System.Text.Json; | ||
using Xunit; | ||
|
||
namespace Blazored.LocalStorage.Tests.LocalStorageServiceTests | ||
{ | ||
public class SetItem | ||
{ | ||
private JsonSerializerOptions _jsonOptions; | ||
private Mock<JSRuntimeWrapper> _mockJSRuntime; | ||
private LocalStorageService _sut; | ||
|
||
private static string _key = "testKey"; | ||
|
||
public SetItem() | ||
{ | ||
_mockJSRuntime = new Mock<JSRuntimeWrapper>(); | ||
_jsonOptions = new JsonSerializerOptions(); | ||
_jsonOptions.Converters.Add(new TimespanJsonConverter()); | ||
_sut = new LocalStorageService(_mockJSRuntime.Object); | ||
} | ||
|
||
[Fact] | ||
public void Should_OverwriteExistingValue() | ||
{ | ||
// Arrange | ||
string existingValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJBZG1pbmlzdHJhdG9yIiwiZXhwIjoxNTg1NjYwNzEyLCJpc3MiOiJDb2RlUmVkQm9va2luZy5TZXJ2ZXIiLCJhdWQiOiJDb2RlUmVkQm9va2luZy5DbGllbnRzIn0.JhK1M1H7NLCFexujJYCDjTn9La0HloGYADMHXGCFksU"; | ||
string newValue = "6QLE0LL7iw7tHPAwold31qUENt3lVTUZxDGqeXQFx38="; | ||
|
||
_mockJSRuntime.Setup(x => x.Invoke<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => existingValue); | ||
_mockJSRuntime.Setup(x => x.InvokeVoid("localStorage.setItem", new[] { _key, newValue })); | ||
|
||
// Act | ||
_sut.SetItem(_key, newValue); | ||
|
||
// Assert | ||
_mockJSRuntime.Verify(); | ||
} | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
tests/Blazored.LocalStorage.Tests/LocalStorageServiceTests/SetItemAsync.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,45 @@ | ||
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 SetItemAsync | ||
{ | ||
private JsonSerializerOptions _jsonOptions; | ||
private Mock<JSRuntimeWrapperAsync> _mockJSRuntime; | ||
private LocalStorageService _sut; | ||
|
||
private static string _key = "testKey"; | ||
|
||
public SetItemAsync() | ||
{ | ||
_mockJSRuntime = new Mock<JSRuntimeWrapperAsync>(); | ||
_jsonOptions = new JsonSerializerOptions(); | ||
_jsonOptions.Converters.Add(new TimespanJsonConverter()); | ||
_sut = new LocalStorageService(_mockJSRuntime.Object); | ||
} | ||
|
||
[Fact] | ||
public async Task Should_OverwriteExistingValue() | ||
{ | ||
// Arrange | ||
string existingValue = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiYWRtaW4iLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL3JvbGUiOiJBZG1pbmlzdHJhdG9yIiwiZXhwIjoxNTg1NjYwNzEyLCJpc3MiOiJDb2RlUmVkQm9va2luZy5TZXJ2ZXIiLCJhdWQiOiJDb2RlUmVkQm9va2luZy5DbGllbnRzIn0.JhK1M1H7NLCFexujJYCDjTn9La0HloGYADMHXGCFksU"; | ||
string newValue = "6QLE0LL7iw7tHPAwold31qUENt3lVTUZxDGqeXQFx38="; | ||
|
||
_mockJSRuntime.Setup(x => x.InvokeAsync<string>("localStorage.getItem", new[] { _key })) | ||
.Returns(() => new ValueTask<string>(existingValue)); | ||
_mockJSRuntime.Setup(x => x.InvokeVoidAsync("localStorage.setItem", new[] { _key, newValue })); | ||
|
||
// Act | ||
await _sut.SetItemAsync(_key, newValue); | ||
|
||
// Assert | ||
_mockJSRuntime.Verify(); | ||
} | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
tests/Blazored.LocalStorage.Tests/Mocks/JSRuntimeWrapper.cs
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.