Skip to content

Commit 5916e62

Browse files
Copilotkbeaugrand
andcommitted
Add comprehensive unit tests for custom menu entries feature
Co-authored-by: kbeaugrand <9513635+kbeaugrand@users.noreply.github.com>
1 parent 2ef7e21 commit 5916e62

3 files changed

Lines changed: 846 additions & 0 deletions

File tree

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Copyright (c) CGI France. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace IoTHub.Portal.Tests.Unit.Client.Services
5+
{
6+
using System;
7+
using System.Collections.Generic;
8+
using System.Linq;
9+
using System.Net;
10+
using System.Net.Http;
11+
using System.Net.Mime;
12+
using System.Text;
13+
using System.Text.Json;
14+
using System.Threading.Tasks;
15+
using AutoFixture;
16+
using FluentAssertions;
17+
using IoTHub.Portal.Client.Services;
18+
using IoTHub.Portal.Shared.Models.v10;
19+
using IoTHub.Portal.Tests.Unit.UnitTests.Bases;
20+
using Microsoft.Extensions.DependencyInjection;
21+
using NUnit.Framework;
22+
using RichardSzalay.MockHttp;
23+
24+
[TestFixture]
25+
public class MenuEntryClientServiceTests : BlazorUnitTest
26+
{
27+
private IMenuEntryClientService menuEntryClientService;
28+
29+
public override void Setup()
30+
{
31+
base.Setup();
32+
33+
_ = Services.AddSingleton<IMenuEntryClientService, MenuEntryClientService>();
34+
35+
this.menuEntryClientService = Services.GetRequiredService<IMenuEntryClientService>();
36+
}
37+
38+
[Test]
39+
public async Task GetMenuEntries_ShouldReturnMenuEntries()
40+
{
41+
// Arrange
42+
var expectedMenuEntries = Fixture.Build<MenuEntryDto>().CreateMany(3).ToList();
43+
44+
_ = MockHttpClient.When(HttpMethod.Get, "/api/menu-entries")
45+
.RespondJson(expectedMenuEntries);
46+
47+
// Act
48+
var result = await this.menuEntryClientService.GetMenuEntries();
49+
50+
// Assert
51+
_ = result.Should().BeEquivalentTo(expectedMenuEntries);
52+
MockHttpClient.VerifyNoOutstandingRequest();
53+
MockHttpClient.VerifyNoOutstandingExpectation();
54+
}
55+
56+
[Test]
57+
public async Task GetMenuEntryById_ShouldReturnMenuEntry()
58+
{
59+
// Arrange
60+
var expectedMenuEntry = Fixture.Create<MenuEntryDto>();
61+
62+
_ = MockHttpClient.When(HttpMethod.Get, $"/api/menu-entries/{expectedMenuEntry.Id}")
63+
.RespondJson(expectedMenuEntry);
64+
65+
// Act
66+
var result = await this.menuEntryClientService.GetMenuEntryById(expectedMenuEntry.Id);
67+
68+
// Assert
69+
_ = result.Should().BeEquivalentTo(expectedMenuEntry);
70+
MockHttpClient.VerifyNoOutstandingRequest();
71+
MockHttpClient.VerifyNoOutstandingExpectation();
72+
}
73+
74+
[Test]
75+
public async Task CreateMenuEntry_ShouldCreateMenuEntry()
76+
{
77+
// Arrange
78+
var menuEntryDto = Fixture.Create<MenuEntryDto>();
79+
var createdMenuEntry = Fixture.Build<MenuEntryDto>()
80+
.With(x => x.Name, menuEntryDto.Name)
81+
.With(x => x.Url, menuEntryDto.Url)
82+
.Create();
83+
84+
_ = MockHttpClient.When(HttpMethod.Post, "/api/menu-entries")
85+
.With(m =>
86+
{
87+
_ = m.Content.Should().BeAssignableTo<StringContent>();
88+
return true;
89+
})
90+
.Respond(HttpStatusCode.Created, new StringContent(
91+
JsonSerializer.Serialize(createdMenuEntry),
92+
Encoding.UTF8,
93+
MediaTypeNames.Application.Json));
94+
95+
// Act
96+
var result = await this.menuEntryClientService.CreateMenuEntry(menuEntryDto);
97+
98+
// Assert
99+
_ = result.Should().NotBeNullOrEmpty();
100+
MockHttpClient.VerifyNoOutstandingRequest();
101+
MockHttpClient.VerifyNoOutstandingExpectation();
102+
}
103+
104+
[Test]
105+
public async Task UpdateMenuEntry_ShouldUpdateMenuEntry()
106+
{
107+
// Arrange
108+
var menuEntryDto = Fixture.Create<MenuEntryDto>();
109+
110+
_ = MockHttpClient.When(HttpMethod.Put, $"/api/menu-entries/{menuEntryDto.Id}")
111+
.With(m =>
112+
{
113+
_ = m.Content.Should().BeAssignableTo<StringContent>();
114+
return true;
115+
})
116+
.Respond(HttpStatusCode.NoContent);
117+
118+
// Act
119+
await this.menuEntryClientService.UpdateMenuEntry(menuEntryDto);
120+
121+
// Assert
122+
MockHttpClient.VerifyNoOutstandingRequest();
123+
MockHttpClient.VerifyNoOutstandingExpectation();
124+
}
125+
126+
[Test]
127+
public async Task DeleteMenuEntry_ShouldDeleteMenuEntry()
128+
{
129+
// Arrange
130+
var menuEntryId = Guid.NewGuid().ToString();
131+
132+
_ = MockHttpClient.When(HttpMethod.Delete, $"/api/menu-entries/{menuEntryId}")
133+
.Respond(HttpStatusCode.NoContent);
134+
135+
// Act
136+
await this.menuEntryClientService.DeleteMenuEntry(menuEntryId);
137+
138+
// Assert
139+
MockHttpClient.VerifyNoOutstandingRequest();
140+
MockHttpClient.VerifyNoOutstandingExpectation();
141+
}
142+
143+
[Test]
144+
public async Task UpdateMenuEntryOrder_ShouldUpdateOrder()
145+
{
146+
// Arrange
147+
var menuEntryId = Guid.NewGuid().ToString();
148+
var newOrder = 5;
149+
150+
_ = MockHttpClient.When(HttpMethod.Patch, $"/api/menu-entries/{menuEntryId}/order")
151+
.With(m =>
152+
{
153+
_ = m.Content.Should().BeAssignableTo<StringContent>();
154+
return true;
155+
})
156+
.Respond(HttpStatusCode.NoContent);
157+
158+
// Act
159+
await this.menuEntryClientService.UpdateMenuEntryOrder(menuEntryId, newOrder);
160+
161+
// Assert
162+
MockHttpClient.VerifyNoOutstandingRequest();
163+
MockHttpClient.VerifyNoOutstandingExpectation();
164+
}
165+
}
166+
}

0 commit comments

Comments
 (0)