From 127427afdbffa7a3ae594b2a9b1c4d5c9d8dbbca Mon Sep 17 00:00:00 2001 From: Mikael Liljedahl Date: Mon, 26 Nov 2018 14:19:14 +0100 Subject: [PATCH] Issue 149 VMSerializer dispatch with arrays failed to serialize --- DotNetifyLib.Core/BaseVM/VMSerializer.cs | 2 +- UnitTests/SimpleListVMTest.cs | 37 ++++++++++++++++++++++-- 2 files changed, 35 insertions(+), 4 deletions(-) diff --git a/DotNetifyLib.Core/BaseVM/VMSerializer.cs b/DotNetifyLib.Core/BaseVM/VMSerializer.cs index 711f8549..59993c6a 100644 --- a/DotNetifyLib.Core/BaseVM/VMSerializer.cs +++ b/DotNetifyLib.Core/BaseVM/VMSerializer.cs @@ -41,7 +41,7 @@ public string Serialize(object viewModel, List ignoredPropertyNames) { try { - if (viewModel is string || !viewModel.GetType().GetTypeInfo().IsClass) + if (viewModel is string || viewModel is JArray || !viewModel.GetType().GetTypeInfo().IsClass) return Convert.ToString(viewModel); var serializer = new JsonSerializer() { ContractResolver = new VMContractResolver(ignoredPropertyNames) }; diff --git a/UnitTests/SimpleListVMTest.cs b/UnitTests/SimpleListVMTest.cs index 2d8493e6..ff2677b5 100644 --- a/UnitTests/SimpleListVMTest.cs +++ b/UnitTests/SimpleListVMTest.cs @@ -48,7 +48,9 @@ public int Add(EmployeeRecord record) public void Update(EmployeeRecord record) => _employeeRecords[record.Id] = record; public void Delete(int id) => _employeeRecords.Remove(id); - } + + public void DeleteMany(List ids) => ids.ForEach(id => _employeeRecords.Remove(id)); + } private class SimpleListVM : BaseVM { @@ -105,7 +107,18 @@ public class EmployeeInfo ShowNotification = true; }; - private bool _showNotification; + + public Action RemoveMany => ids => + { + _employeeService.DeleteMany(ids.ToList()); + foreach(var id in ids) + { + this.RemoveList(nameof(Employees), id); + } + ShowNotification = true; + }; + + private bool _showNotification; public bool ShowNotification { get @@ -202,7 +215,25 @@ public void SimpleListVM_Delete() Assert.IsFalse(employees.Exists(i => i.Id == 2)); } - [TestMethod] + [TestMethod] + public void SimpleListVM_DeleteMany() + { + var vmController = new MockVMController(_simpleListVM); + var vmEmployees = vmController.RequestVM(); + + var employees = _employeeService.GetAll(); + Assert.AreEqual(3, employees.Count); + Assert.IsTrue(employees.Exists(i => i.Id == 2)); + + vmController.UpdateVM(new Dictionary() { { "RemoveMany", "[1,2]" } }); + + employees = _employeeService.GetAll(); + Assert.AreEqual(1, employees.Count); + Assert.IsFalse(employees.Exists(i => i.Id == 2)); + Assert.IsFalse(employees.Exists(i => i.Id == 1)); + } + + [TestMethod] public void SimpleListVM_ShowNotification() { var vmController = new MockVMController(_simpleListVM);