mirror of
https://github.com/DrMboga/WotBlitzStatisticsBlazor.git
synced 2026-10-21 02:17:20 +02:00
feat: added login/out buttons to nav menu and index page
This commit is contained in:
1 parent
6cbb809f44
commit
6390c454d0
8 files changed
+190
-6
No files matched your search
@@ -1,22 +1,28 @@
|
||||
using System.Threading;
|
||||
using Bunit;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using Radzen;
|
||||
using WotBlitzStatisticsPro.Blazor.Messages;
|
||||
using WotBlitzStatisticsPro.Blazor.Model;
|
||||
using WotBlitzStatisticsPro.Blazor.Services;
|
||||
using WotBlitzStatisticsPro.Blazor.Shared;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Blazor.Tests.Pages
|
||||
{
|
||||
public class MainLayoutTest : TestContextBase
|
||||
{
|
||||
private Mock<ILocalStorageService> _localStorageServiceMock;
|
||||
private IRenderedComponent<MainLayout> _component;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_localStorageServiceMock = new();
|
||||
TestContext?.Services.AddSingleton(_localStorageServiceMock.Object);
|
||||
|
||||
_component = TestContext?.RenderComponent<MainLayout>();
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,12 @@ using System.Threading;
|
||||
using AngleSharp.Dom;
|
||||
using Bunit;
|
||||
using FluentAssertions;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Moq;
|
||||
using NUnit.Framework;
|
||||
using WotBlitzStatisticsPro.Blazor.Messages;
|
||||
using WotBlitzStatisticsPro.Blazor.Model;
|
||||
using WotBlitzStatisticsPro.Blazor.Services;
|
||||
using WotBlitzStatisticsPro.Blazor.Shared;
|
||||
using Index = WotBlitzStatisticsPro.Blazor.Pages.Index;
|
||||
|
||||
@@ -25,10 +27,17 @@ namespace WotBlitzStatisticsPro.Blazor.Tests.Pages
|
||||
private IRenderedComponent<Index> _indexComponent;
|
||||
private IRefreshableElementCollection<IElement> _indexButtons;
|
||||
|
||||
private LoginInfo _loginInfo;
|
||||
private Mock<ILocalStorageService> _localStorageServiceMock;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
_loginInfo = new LoginInfo { NickName = "Fake_Nick", AccountId = 123456789 };
|
||||
_localStorageServiceMock = new();
|
||||
|
||||
TestContext?.Services.AddSingleton(_localStorageServiceMock.Object);
|
||||
|
||||
_navMenuComponent = TestContext?.RenderComponent<NavMenu>();
|
||||
_indexComponent = TestContext?.RenderComponent<Index>();
|
||||
|
||||
@@ -104,5 +113,112 @@ namespace WotBlitzStatisticsPro.Blazor.Tests.Pages
|
||||
|
||||
}
|
||||
|
||||
[TestCase(ComponentType.NavMenu)]
|
||||
[TestCase(ComponentType.Index)]
|
||||
public void ShouldShowLoginItemAndHideNickIfLoginInfoIsMissing(ComponentType componentType)
|
||||
{
|
||||
switch (componentType)
|
||||
{
|
||||
case ComponentType.NavMenu:
|
||||
_navMenuElements.Should().Contain(e => e.InnerHtml.Contains("Login with WG.net ID"));
|
||||
_navMenuElements.Should().NotContain(e => e.InnerHtml.Contains(_loginInfo.NickName));
|
||||
_navMenuElements.Should().NotContain(e => e.InnerHtml.Contains("Log out"));
|
||||
break;
|
||||
case ComponentType.Index:
|
||||
_indexButtons.Should().Contain(e => e.InnerHtml.Contains("Login with WG.net ID"));
|
||||
_indexButtons.Should().NotContain(e => e.InnerHtml.Contains(_loginInfo.NickName));
|
||||
_indexButtons.Should().NotContain(e => e.InnerHtml.Contains("Log out"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase(ComponentType.NavMenu)]
|
||||
[TestCase(ComponentType.Index)]
|
||||
public void ShouldShowNickItemAndHideLoginIfLoginInfoIsPresent(ComponentType componentType)
|
||||
{
|
||||
_localStorageServiceMock.Setup(s => s.GetItemAsync<LoginInfo>(It.IsAny<string>())).ReturnsAsync(_loginInfo);
|
||||
|
||||
switch (componentType)
|
||||
{
|
||||
case ComponentType.NavMenu:
|
||||
var navMenuComponent = TestContext?.RenderComponent<NavMenu>();
|
||||
var navMenuElements = navMenuComponent?.FindAll(".rz-navigation-item");
|
||||
navMenuElements.Should().NotBeNull();
|
||||
navMenuElements.Count.Should().Be(5);
|
||||
navMenuElements.Should().NotContain(e => e.InnerHtml.Contains("Login with WG.net ID"));
|
||||
navMenuElements.Should().Contain(e => e.InnerHtml.Contains(_loginInfo.NickName));
|
||||
navMenuElements.Should().Contain(e => e.InnerHtml.Contains("Log out"));
|
||||
break;
|
||||
case ComponentType.Index:
|
||||
var indexComponent = TestContext?.RenderComponent<Index>();
|
||||
var indexButtons = indexComponent?.FindAll(".rz-button");
|
||||
indexButtons.Should().NotBeNull();
|
||||
indexButtons.Count.Should().Be(4);
|
||||
indexButtons.Should().NotContain(e => e.InnerHtml.Contains("Login with WG.net ID"));
|
||||
indexButtons.Should().Contain(e => e.InnerHtml.Contains(_loginInfo.NickName));
|
||||
indexButtons.Should().Contain(e => e.InnerHtml.Contains("Log out"));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
[TestCase(ComponentType.NavMenu)]
|
||||
[TestCase(ComponentType.Index)]
|
||||
public void ShouldFireOpenPlayerMessageWhenNickItemClicked(ComponentType componentType)
|
||||
{
|
||||
_localStorageServiceMock.Setup(s => s.GetItemAsync<LoginInfo>(It.IsAny<string>())).ReturnsAsync(_loginInfo);
|
||||
|
||||
IElement targetElement = null;
|
||||
|
||||
switch (componentType)
|
||||
{
|
||||
case ComponentType.NavMenu:
|
||||
var navMenuComponent = TestContext?.RenderComponent<NavMenu>();
|
||||
var navMenuElements = navMenuComponent?.FindAll(".rz-navigation-item");
|
||||
targetElement = navMenuElements[1];
|
||||
break;
|
||||
case ComponentType.Index:
|
||||
var indexComponent = TestContext?.RenderComponent<Index>();
|
||||
var indexButtons = indexComponent?.FindAll(".rz-button");
|
||||
targetElement = indexButtons[0];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
targetElement?.Click();
|
||||
|
||||
MediatorMock.Verify(m => m.Publish(
|
||||
It.Is<OpenPlayerInfoMessage>(m => m.AccountId == _loginInfo.AccountId && m.IsLoggedIn),
|
||||
It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
|
||||
[TestCase(ComponentType.NavMenu)]
|
||||
[TestCase(ComponentType.Index)]
|
||||
public void ShouldFireLoGoutMessageIfLogOutItemClicked(ComponentType componentType)
|
||||
{
|
||||
_localStorageServiceMock.Setup(s => s.GetItemAsync<LoginInfo>(It.IsAny<string>())).ReturnsAsync(_loginInfo);
|
||||
|
||||
IElement targetElement = null;
|
||||
|
||||
switch (componentType)
|
||||
{
|
||||
case ComponentType.NavMenu:
|
||||
var navMenuComponent = TestContext?.RenderComponent<NavMenu>();
|
||||
var navMenuElements = navMenuComponent?.FindAll(".rz-navigation-item");
|
||||
targetElement = navMenuElements[2];
|
||||
break;
|
||||
case ComponentType.Index:
|
||||
var indexComponent = TestContext?.RenderComponent<Index>();
|
||||
var indexButtons = indexComponent?.FindAll(".rz-button");
|
||||
targetElement = indexButtons[1];
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
targetElement?.Click();
|
||||
|
||||
MediatorMock.Verify(m => m.Publish(
|
||||
It.IsAny<LogOutFromWgMessage>(),
|
||||
It.IsAny<CancellationToken>()), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
using MediatR;
|
||||
|
||||
namespace WotBlitzStatisticsPro.Blazor.Messages
|
||||
{
|
||||
public class LogOutFromWgMessage: INotification
|
||||
{
|
||||
}
|
||||
}
|
||||
@@ -2,16 +2,31 @@
|
||||
@using MediatR
|
||||
@using WotBlitzStatisticsPro.Blazor.Messages
|
||||
@using WotBlitzStatisticsPro.Blazor.Model
|
||||
@using WotBlitzStatisticsPro.Blazor.Services
|
||||
@inject IStringLocalizer<App> Localize
|
||||
@inject IMediator Mediator
|
||||
@inject ILocalStorageService LocalStorage
|
||||
<div class="splash">
|
||||
<div class="splash-content">
|
||||
<RadzenLabel Text=@Localize["Welcome to WOT Blitz statistics application!"] Style="margin: 30px 0px 100px 0px"></RadzenLabel>
|
||||
|
||||
<RadzenButton Click=@(async args => { await Mediator.Publish(new LoginToWgMessage()); })
|
||||
Icon="vpn_key"
|
||||
Text=@Localize["Login with WG.net ID"]
|
||||
Style="width: 300px; margin: 30px" />
|
||||
@if(LoginInfo != null)
|
||||
{
|
||||
<RadzenButton Click=@(async args => { await Mediator.Publish(new OpenPlayerInfoMessage(LoginInfo.AccountId, true)); })
|
||||
Icon="person"
|
||||
[email protected]
|
||||
Style="width: 300px; margin: 30px" />
|
||||
<RadzenButton Click=@(async args => { await Mediator.Publish(new LogOutFromWgMessage()); })
|
||||
Icon="logout"
|
||||
Text=@Localize["Log out"]
|
||||
Style="width: 300px; margin: 30px" />
|
||||
}
|
||||
else
|
||||
{
|
||||
<RadzenButton Click=@(async args => { await Mediator.Publish(new LoginToWgMessage()); })
|
||||
Icon="vpn_key"
|
||||
Text=@Localize["Login with WG.net ID"]
|
||||
Style="width: 300px; margin: 30px" />
|
||||
}
|
||||
<RadzenButton Click=@(async args => { await Mediator.Publish(new OpenSearchDialogMessage(DialogType.FindPlayer)); })
|
||||
Icon="person_search"
|
||||
Text=@Localize["Search player"]
|
||||
@@ -24,3 +39,12 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@code {
|
||||
public LoginInfo? LoginInfo { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
LoginInfo = await LocalStorage.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,6 +177,9 @@
|
||||
<data name="Last battle" xml:space="preserve">
|
||||
<value>Letzte Gefecht</value>
|
||||
</data>
|
||||
<data name="Log out" xml:space="preserve">
|
||||
<value>Abmelden</value>
|
||||
</data>
|
||||
<data name="Login with WG.net ID" xml:space="preserve">
|
||||
<value>Anmelden mit WG.net-ID</value>
|
||||
</data>
|
||||
|
||||
@@ -177,6 +177,9 @@
|
||||
<data name="Last battle" xml:space="preserve">
|
||||
<value>Последний бой</value>
|
||||
</data>
|
||||
<data name="Log out" xml:space="preserve">
|
||||
<value>Выйти</value>
|
||||
</data>
|
||||
<data name="Login with WG.net ID" xml:space="preserve">
|
||||
<value>Войти с WG.net ID</value>
|
||||
</data>
|
||||
|
||||
@@ -4,7 +4,15 @@
|
||||
<div>
|
||||
<RadzenPanelMenu Click="@(MenuClicked)">
|
||||
<RadzenPanelMenuItem Text="@Localize["Home"]" Icon="home"></RadzenPanelMenuItem>
|
||||
<RadzenPanelMenuItem Text="@Localize["Login with WG.net ID"]" Icon="vpn_key"></RadzenPanelMenuItem>
|
||||
@if(LoginInfo != null)
|
||||
{
|
||||
<RadzenPanelMenuItem Text="@LoginInfo.NickName" Icon="person"></RadzenPanelMenuItem>
|
||||
<RadzenPanelMenuItem Text="@Localize["Log out"]" Icon="logout"></RadzenPanelMenuItem>
|
||||
}
|
||||
else
|
||||
{
|
||||
<RadzenPanelMenuItem Text="@Localize["Login with WG.net ID"]" Icon="vpn_key"></RadzenPanelMenuItem>
|
||||
}
|
||||
<RadzenPanelMenuItem Text="@Localize["Search player"]" Icon="person_search"></RadzenPanelMenuItem>
|
||||
<RadzenPanelMenuItem Text="@Localize["Search clan"]" Icon="screen_search_desktop"></RadzenPanelMenuItem>
|
||||
</RadzenPanelMenu>
|
||||
|
||||
@@ -23,10 +23,20 @@ namespace WotBlitzStatisticsPro.Blazor.Shared
|
||||
[Inject]
|
||||
private IStringLocalizer<App> Localizer { get; set; }
|
||||
|
||||
[Inject]
|
||||
public ILocalStorageService LocalStorage { get; set; }
|
||||
|
||||
|
||||
[Parameter]
|
||||
public EventCallback CloseSideBar { get; set; }
|
||||
|
||||
public LoginInfo? LoginInfo { get; set; }
|
||||
|
||||
protected override async Task OnInitializedAsync()
|
||||
{
|
||||
LoginInfo = await LocalStorage.GetItemAsync<LoginInfo>(Constants.LoginInfoLocalStorageKey);
|
||||
}
|
||||
|
||||
public async Task MenuClicked(MenuItemEventArgs menuItem)
|
||||
{
|
||||
await CloseSideBar.InvokeAsync();
|
||||
@@ -38,6 +48,9 @@ namespace WotBlitzStatisticsPro.Blazor.Shared
|
||||
case var _ when (Localizer.GetString("Login with WG.net ID")) == menuItem.Text:
|
||||
await Mediator.Publish(new LoginToWgMessage());
|
||||
break;
|
||||
case var _ when (Localizer.GetString("Log out")) == menuItem.Text:
|
||||
await Mediator.Publish(new LogOutFromWgMessage());
|
||||
break;
|
||||
case var _ when (Localizer.GetString("Search player")) == menuItem.Text:
|
||||
await Mediator.Publish(new OpenSearchDialogMessage(DialogType.FindPlayer));
|
||||
//await SearchDialogService.OpenSearchDialog(DialogType.FindPlayer);
|
||||
@@ -45,6 +58,9 @@ namespace WotBlitzStatisticsPro.Blazor.Shared
|
||||
case var _ when (Localizer.GetString("Search clan")) == menuItem.Text:
|
||||
await Mediator.Publish(new OpenSearchDialogMessage(DialogType.FindClan));
|
||||
break;
|
||||
case var _ when (LoginInfo?.NickName) == menuItem.Text:
|
||||
await Mediator.Publish(new OpenPlayerInfoMessage(LoginInfo.AccountId, true));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user