Skip to content

Commit 210fabc

Browse files
YuriiPovkhthiennn
authored andcommitted
refactored DefaultAddressViewComponentTest (#170)
* refactored DefaultAddressViewComponentTest * add CarouselWidgetViewComponentTests * add PageControllerTests
1 parent 9b844d8 commit 210fabc

File tree

6 files changed

+278
-56
lines changed

6 files changed

+278
-56
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
using Microsoft.AspNetCore.Mvc.ViewComponents;
4+
using Moq;
5+
using Newtonsoft.Json;
6+
using SimplCommerce.Module.Cms.Components;
7+
using SimplCommerce.Module.Cms.ViewModels;
8+
using SimplCommerce.Module.Core.Services;
9+
using SimplCommerce.Module.Core.ViewModels;
10+
using Xunit;
11+
12+
namespace SimplCommerce.Module.Cms.Tests.Components
13+
{
14+
public class CarouselWidgetViewComponentTests
15+
{
16+
[Fact]
17+
public void CarouselWidgetViewComponent_ShouldReturns_NotNullView()
18+
{
19+
var mediaService = new Mock<IMediaService>();
20+
var widgetInstanceViewModel = WidgetInstanceViewModel();
21+
mediaService.Setup(x => x.GetMediaUrl(It.IsAny<string>())).Returns(It.IsAny<string>());
22+
var component = new CarouselWidgetViewComponent(mediaService.Object);
23+
24+
var view = component.Invoke(widgetInstanceViewModel) as ViewViewComponentResult;
25+
26+
Assert.NotNull(view);
27+
}
28+
29+
[Fact]
30+
public void CarouselWidgetViewComponent_ForAllImages_GetMediaUrlWasCalled()
31+
{
32+
const int numberOfImagesOnWidgetInstanceViewModel = 1;
33+
var mockMediaService = new Mock<IMediaService>();
34+
var widgetInstanceViewModel = WidgetInstanceViewModel();
35+
mockMediaService.Setup(x => x.GetMediaUrl(It.IsAny<string>())).Returns(It.IsAny<string>());
36+
var component = new CarouselWidgetViewComponent(mockMediaService.Object);
37+
38+
component.Invoke(widgetInstanceViewModel);
39+
40+
mockMediaService.Verify(x=>x.GetMediaUrl(It.IsAny<string>()), Times.Exactly(numberOfImagesOnWidgetInstanceViewModel));
41+
}
42+
43+
[Fact]
44+
public void CarouselWidgetViewComponent_PreparingViewModel_SuccesfulyDeserializedOneCarouselWidgetViewComponentVmInstance()
45+
{
46+
var mockMediaService = new Mock<IMediaService>();
47+
var widgetInstanceViewModel = WidgetInstanceViewModel();
48+
mockMediaService.Setup(x => x.GetMediaUrl(It.IsAny<string>())).Returns(It.IsAny<string>());
49+
var component = new CarouselWidgetViewComponent(mockMediaService.Object);
50+
51+
var result = component.Invoke(widgetInstanceViewModel) as ViewViewComponentResult;
52+
var returnedModel = (result?.ViewData.Model as CarouselWidgetViewComponentVm);
53+
54+
Assert.Equal(1, returnedModel?.Items.Count);
55+
}
56+
57+
[Fact]
58+
public void CarouselWidgetViewComponent_ShouldReturns_CorrectModelType()
59+
{
60+
var mockMediaService = new Mock<IMediaService>();
61+
var widgetInstanceViewModel = WidgetInstanceViewModel();
62+
mockMediaService.Setup(x => x.GetMediaUrl(It.IsAny<string>())).Returns(It.IsAny<string>());
63+
var component = new CarouselWidgetViewComponent(mockMediaService.Object);
64+
65+
var result = component.Invoke(widgetInstanceViewModel) as ViewViewComponentResult;
66+
67+
Assert.IsType<CarouselWidgetViewComponentVm>(result?.ViewData.Model);
68+
}
69+
70+
private WidgetInstanceViewModel WidgetInstanceViewModel()
71+
{
72+
return new WidgetInstanceViewModel()
73+
{
74+
Data = MakeCarouselWidgetViewModelsJson()
75+
};
76+
}
77+
78+
private string MakeCarouselWidgetViewModelsJson()
79+
{
80+
var carouselWidgetViewModels = new List<CarouselWidgetViewComponentItemVm>()
81+
{
82+
new CarouselWidgetViewComponentItemVm
83+
{
84+
Caption = "Carousel Item",
85+
TargetUrl = "localhost/",
86+
Image = "item.jpg"
87+
},
88+
};
89+
90+
return JsonConvert.SerializeObject(carouselWidgetViewModels);
91+
}
92+
}
93+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using Microsoft.AspNetCore.Mvc;
6+
using Moq;
7+
using SimplCommerce.Infrastructure.Data;
8+
using SimplCommerce.Module.Cms.Controllers;
9+
using SimplCommerce.Module.Cms.Models;
10+
using Xunit;
11+
12+
namespace SimplCommerce.Module.Cms.Tests.Controllers
13+
{
14+
public class PageControllerTests
15+
{
16+
[Fact]
17+
public void PageDetail_ShouldReturns_PageWithName()
18+
{
19+
Mock<IRepository<Page>> pageRepository = MakePageRepository();
20+
var pageController = new PageController(pageRepository.Object);
21+
22+
var result = pageController.PageDetail(It.IsAny<long>()) as ViewResult;
23+
24+
Assert.Equal("Page", (result?.ViewData.Model as Page)?.Name);
25+
}
26+
27+
[Fact]
28+
public void PageDetail_ShouldReturns_CorrectModelType()
29+
{
30+
var mock = new Mock<IRepository<Page>>();
31+
mock.Setup(x => x.Query()).Returns(new List<Page>() { new Page() { Name = "Page" } }.AsQueryable());
32+
var pageController = new PageController(mock.Object);
33+
34+
var result = pageController.PageDetail(It.IsAny<long>()) as ViewResult;
35+
36+
Assert.IsType<Page>(result?.ViewData.Model);
37+
}
38+
39+
private Mock<IRepository<Page>> MakePageRepository()
40+
{
41+
var pageRepository = new Mock<IRepository<Page>>();
42+
pageRepository.Setup(x => x.Query()).Returns(new List<Page>() { new Page() { Name = "Page" } }.AsQueryable());
43+
return pageRepository;
44+
}
45+
}
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
</PropertyGroup>
6+
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.0.0" />
9+
<PackageReference Include="Moq" Version="4.7.8" />
10+
<PackageReference Include="xunit" Version="2.2.0" />
11+
<PackageReference Include="xunit.runner.visualstudio" Version="2.2.0" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\src\Modules\SimplCommerce.Module.Cms\SimplCommerce.Module.Cms.csproj" />
16+
<ProjectReference Include="..\src\Modules\SimplCommerce.Module.Core\SimplCommerce.Module.Core.csproj" />
17+
</ItemGroup>
18+
19+
</Project>

SimplCommerce.sln

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
33
# Visual Studio 15
4-
VisualStudioVersion = 15.0.26403.0
4+
VisualStudioVersion = 15.0.26403.3
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{C9BFDDC4-5671-47A3-B57D-197C2A51FA8A}"
77
EndProject
@@ -48,6 +48,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.Core.T
4848
EndProject
4949
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.News", "src\Modules\SimplCommerce.Module.News\SimplCommerce.Module.News.csproj", "{4FE8CC2E-3DF9-4AE7-A04E-4293FABEE221}"
5050
EndProject
51+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimplCommerce.Module.Cms.Tests", "SimplCommerce.Module.Cms.Tests\SimplCommerce.Module.Cms.Tests.csproj", "{483C5E8D-99A4-410D-8DB3-2F695B05BF40}"
52+
EndProject
5153
Global
5254
GlobalSection(SolutionConfigurationPlatforms) = preSolution
5355
Debug|Any CPU = Debug|Any CPU
@@ -238,6 +240,18 @@ Global
238240
{4FE8CC2E-3DF9-4AE7-A04E-4293FABEE221}.Release|x64.Build.0 = Release|Any CPU
239241
{4FE8CC2E-3DF9-4AE7-A04E-4293FABEE221}.Release|x86.ActiveCfg = Release|Any CPU
240242
{4FE8CC2E-3DF9-4AE7-A04E-4293FABEE221}.Release|x86.Build.0 = Release|Any CPU
243+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
244+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|Any CPU.Build.0 = Debug|Any CPU
245+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|x64.ActiveCfg = Debug|Any CPU
246+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|x64.Build.0 = Debug|Any CPU
247+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|x86.ActiveCfg = Debug|Any CPU
248+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Debug|x86.Build.0 = Debug|Any CPU
249+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|Any CPU.ActiveCfg = Release|Any CPU
250+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|Any CPU.Build.0 = Release|Any CPU
251+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|x64.ActiveCfg = Release|Any CPU
252+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|x64.Build.0 = Release|Any CPU
253+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|x86.ActiveCfg = Release|Any CPU
254+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40}.Release|x86.Build.0 = Release|Any CPU
241255
EndGlobalSection
242256
GlobalSection(SolutionProperties) = preSolution
243257
HideSolutionNode = FALSE
@@ -260,5 +274,6 @@ Global
260274
{338A04B7-CB1A-4C98-84A7-02F48A41D860} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
261275
{52D24DE2-8661-4BBF-AC7B-B36BB1A655BA} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
262276
{4FE8CC2E-3DF9-4AE7-A04E-4293FABEE221} = {7EFA2FA7-32DD-4047-B021-50E77A83D714}
277+
{483C5E8D-99A4-410D-8DB3-2F695B05BF40} = {D9FD9ABA-AE5E-4427-AA6B-6285BE2E212D}
263278
EndGlobalSection
264279
EndGlobal

test/SimplCommerce.Module.Core.Tests/Components/DefaultAddressViewComponentTest.cs

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading.Tasks;
4+
using Microsoft.AspNetCore.Mvc.ViewComponents;
5+
using Moq;
6+
using SimplCommerce.Infrastructure.Data;
7+
using SimplCommerce.Module.Core.Components;
8+
using SimplCommerce.Module.Core.Extensions;
9+
using SimplCommerce.Module.Core.Models;
10+
using SimplCommerce.Module.Core.ViewModels.Manage;
11+
using Xunit;
12+
13+
namespace SimplCommerce.Module.Core.Tests.Components
14+
{
15+
public class DefaultAddressViewComponentTests
16+
{
17+
[Fact]
18+
public async Task DefaultAddressViewComponent_Should_Returns_DefaultAddress()
19+
{
20+
var address = MakeAddress();
21+
var component = MakeMockedDefaultAddressViewComponent(address, MakeUserWithShippingAdress());
22+
23+
var result = await component.InvokeAsync() as ViewViewComponentResult;
24+
var returnedModel = (result?.ViewData.Model as DefaultAddressViewComponentVm);
25+
26+
Assert.Equal(address.AddressLine1, returnedModel?.Address.AddressLine1);
27+
}
28+
29+
[Fact]
30+
public async Task DefaultAddressViewComponetn_ShouldReturns_CorrectModelType()
31+
{
32+
var component = MakeMockedDefaultAddressViewComponent(MakeAddress(), MakeUserWithShippingAdress());
33+
34+
var view = await component.InvokeAsync() as ViewViewComponentResult;
35+
36+
Assert.IsType<DefaultAddressViewComponentVm>(view?.ViewData.Model);
37+
}
38+
39+
[Fact]
40+
public async Task DefaultAddressViewComponetn_ShouldReturns_CorrectViewType()
41+
{
42+
var component = MakeMockedDefaultAddressViewComponent(MakeAddress(), MakeUserWithShippingAdress());
43+
44+
var view = await component.InvokeAsync() as ViewViewComponentResult;
45+
46+
Assert.IsType<ViewViewComponentResult>(view);
47+
}
48+
49+
[Fact]
50+
public async Task DefaultAddressViewComponetn_ShouldReturns_NotNullView()
51+
{
52+
var component = MakeMockedDefaultAddressViewComponent(MakeAddress(), MakeUserWithShippingAdress());
53+
54+
var view = await component.InvokeAsync() as ViewViewComponentResult;
55+
56+
Assert.NotNull(view);
57+
}
58+
59+
[Fact]
60+
public async Task DefaultAddressViewComponetn_ShouldReturns_CorrectViewName()
61+
{
62+
var component = MakeMockedDefaultAddressViewComponent(MakeAddress(), MakeUserWithShippingAdress());
63+
64+
var view = await component.InvokeAsync() as ViewViewComponentResult;
65+
66+
Assert.Equal("/Modules/SimplCommerce.Module.Core/Views/Components/DefaultAddress.cshtml", view?.ViewName);
67+
}
68+
69+
private DefaultAddressViewComponent MakeMockedDefaultAddressViewComponent(Address address, User user)
70+
{
71+
var mockRepository = new Mock<IRepository<Address>>();
72+
var mockWorkContext = new Mock<IWorkContext>();
73+
74+
mockRepository.Setup(x => x.Query()).Returns(new List<Address> { address }.AsQueryable());
75+
mockWorkContext.Setup(x => x.GetCurrentUser()).Returns(Task.FromResult(user));
76+
var component = new DefaultAddressViewComponent(mockRepository.Object, mockWorkContext.Object);
77+
78+
return component;
79+
}
80+
81+
private User MakeUserWithShippingAdress()
82+
{
83+
return new User { Id = 1, FullName = "Maher", DefaultShippingAddressId = 0 };
84+
}
85+
86+
private Address MakeAddress()
87+
{
88+
var country = new Country { Name = "France" };
89+
var stateOrProvince = new StateOrProvince { Name = "IDF", Country = country, Type = "State" };
90+
var district = new District { Location = "Center", StateOrProvince = stateOrProvince, Name = "Paris" };
91+
92+
var address = new Address
93+
{
94+
CountryId = 1,
95+
AddressLine1 = "115 Rue Marcel",
96+
Country = country,
97+
StateOrProvince = stateOrProvince,
98+
District = district
99+
};
100+
101+
return address;
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)