Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
new: unit tests
  • Loading branch information
stsrki committed Feb 18, 2019
1 parent f15fb46 commit 4436a38
Show file tree
Hide file tree
Showing 29 changed files with 196 additions and 694 deletions.
4 changes: 2 additions & 2 deletions Blazorise.sln
Expand Up @@ -47,9 +47,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazorise.Icons.Material",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Blazorise.UnitTests", "Tests\Blazorise.UnitTests\Blazorise.UnitTests.csproj", "{B4563EEC-6076-497D-B877-D8D809F8C33B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazorise.BasicTestApp.Server", "Tests\Blazorise.BasicTestApp\Blazorise.BasicTestApp.Server\Blazorise.BasicTestApp.Server.csproj", "{ACAD2FC6-F3DE-40C0-A5BC-DB214B2C66F9}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicTestApp.Server", "Tests\BasicTestApp.Server\BasicTestApp.Server.csproj", "{ACAD2FC6-F3DE-40C0-A5BC-DB214B2C66F9}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Blazorise.BasicTestApp.Client", "Tests\Blazorise.BasicTestApp\Blazorise.BasicTestApp.Client\Blazorise.BasicTestApp.Client.csproj", "{3BAB3F4A-3177-44EF-8F52-F76DDDCCE59F}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicTestApp.Client", "Tests\BasicTestApp.Client\BasicTestApp.Client.csproj", "{3BAB3F4A-3177-44EF-8F52-F76DDDCCE59F}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
Expand Up @@ -11,4 +11,10 @@
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="0.8.0-preview-19104-04" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\Source\Blazorise.Bootstrap\Blazorise.Bootstrap.csproj" />
<ProjectReference Include="..\..\Source\Blazorise\Blazorise.csproj" />
<ProjectReference Include="..\..\Source\Extensions\Blazorise.Icons.FontAwesome\Blazorise.Icons.FontAwesome.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Tests/BasicTestApp.Client/ButtonComponent.cshtml
@@ -0,0 +1 @@
<SimpleButton Type="ButtonType.Button" Color="Color.Primary">hello primary</SimpleButton>
7 changes: 7 additions & 0 deletions Tests/BasicTestApp.Client/DropdownComponent.cshtml
@@ -0,0 +1,7 @@
<Dropdown>
<DropdownToggle Color="Color.Secondary">Dropdown</DropdownToggle>
<DropdownMenu>
<DropdownItem>Item 1</DropdownItem>
<DropdownItem>Item 2</DropdownItem>
</DropdownMenu>
</Dropdown>
Expand Up @@ -3,8 +3,11 @@
Select test:
<select bind=@SelectedComponentTypeName>
<option value="none">Choose...</option>
<option value="Blazorise.BasicTestApp.Client.TextOnlyComponent">Plain text</option>
<option value="Blazorise.BasicTestApp.Client.ButtonComponent">Simple Button</option>
<option value="BasicTestApp.Client.TextOnlyComponent">Plain text</option>
<option value="BasicTestApp.Client.ButtonComponent">Button</option>
<option value="BasicTestApp.Client.DropdownComponent">Dropdown</option>
<option value="BasicTestApp.Client.TextEditComponent">TextEdit</option>
<option value="BasicTestApp.Client.TextEditValidationComponent">TextValidation</option>
</select>

@if ( SelectedComponentType != null )
Expand Down
@@ -1,6 +1,6 @@
using Microsoft.AspNetCore.Blazor.Hosting;

namespace Blazorise.BasicTestApp.Client
namespace BasicTestApp.Client
{
public class Program
{
Expand Down
@@ -1,18 +1,32 @@
using System.Runtime.InteropServices;
using Blazorise;
using Blazorise.Bootstrap;
using Blazorise.Icons.FontAwesome;
using Microsoft.AspNetCore.Blazor.Http;
using Microsoft.AspNetCore.Components.Builder;
using Microsoft.Extensions.DependencyInjection;

namespace Blazorise.BasicTestApp.Client
namespace BasicTestApp.Client
{
public class Startup
{
public void ConfigureServices( IServiceCollection services )
{
services
.AddBlazorise( options =>
{
options.ChangeTextOnKeyPress = true;
} )
.AddBootstrapProviders()
.AddFontAwesomeIcons();
}

public void Configure( IComponentsApplicationBuilder app )
{
app
.UseBootstrapProviders()
.UseFontAwesomeIcons();

if ( RuntimeInformation.IsOSPlatform( OSPlatform.Create( "WEBASSEMBLY" ) ) )
{
// Needed because the test server runs on a different port than the client app,
Expand Down
10 changes: 10 additions & 0 deletions Tests/BasicTestApp.Client/TextEditComponent.cshtml
@@ -0,0 +1,10 @@
@* https://github.com/aspnet/AspNetCore/issues/6351 *@
<TextEdit Text="@inputText" TextChanged="@((s)=>{ inputText = s; StateHasChanged(); })"></TextEdit>

<p>The text below should update automatically as you type in the text field above</p>

<p id="test-result">@inputText</p>

@functions {
string inputText { get; set; }
}
8 changes: 8 additions & 0 deletions Tests/BasicTestApp.Client/TextEditValidationComponent.cshtml
@@ -0,0 +1,8 @@
<Validation Validator="@ValidationRule.IsNotEmpty">
<TextEdit>
<Feedback>
<ValidationSuccess>success text</ValidationSuccess>
<ValidationError>error text</ValidationError>
</Feedback>
</TextEdit>
</Validation>
4 changes: 4 additions & 0 deletions Tests/BasicTestApp.Client/_ViewImports.cshtml
@@ -0,0 +1,4 @@
@addTagHelper *, Blazorise

@using Blazorise
@using Blazorise.Bootstrap
@@ -1,6 +1,6 @@
// We'll store the results from the tests here
var results = {};
var assemblyName = 'Blazorise.BasicTestApp.Client';
var assemblyName = 'BasicTestApp.Client';

function invokeDotNetInteropMethodsAsync(shouldSupportSyncInterop, dotNetObjectByRef, instanceMethodsTarget) {
if (shouldSupportSyncInterop) {
Expand Down
Expand Up @@ -12,7 +12,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Blazorise.BasicTestApp.Client\Blazorise.BasicTestApp.Client.csproj" />
<ProjectReference Include="..\BasicTestApp.Client\BasicTestApp.Client.csproj" />
</ItemGroup>

</Project>
Expand Up @@ -2,7 +2,7 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;

namespace Blazorise.BasicTestApp.Server
namespace BasicTestApp.Server
{
public class Program
{
Expand Down
Expand Up @@ -5,7 +5,7 @@
using Newtonsoft.Json.Serialization;
using System.Linq;

namespace Blazorise.BasicTestApp.Server
namespace BasicTestApp.Server
{
public class Startup
{
Expand Down

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/Blazorise.UnitTests/Blazorise.UnitTests.csproj
Expand Up @@ -39,7 +39,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Blazorise.BasicTestApp\Blazorise.BasicTestApp.Server\Blazorise.BasicTestApp.Server.csproj" />
<ProjectReference Include="..\BasicTestApp.Server\BasicTestApp.Server.csproj" />
</ItemGroup>

</Project>
89 changes: 78 additions & 11 deletions Tests/Blazorise.UnitTests/ComponentRenderingTest.cs
@@ -1,23 +1,21 @@
using System;
using System.Collections.Generic;
using System.Text;
using DevHostServerProgram = Blazorise.BasicTestApp.Server.Program;
#region Using directives
using BasicTestApp.Client;
using Blazorise.UnitTests.Infrastructure;
using Blazorise.UnitTests.Infrastructure.ServerFixtures;
using OpenQA.Selenium;
using Xunit;
using Xunit.Abstractions;
using Blazorise.BasicTestApp.Client;
using DevHostServerProgram = BasicTestApp.Server.Program;
#endregion

namespace Blazorise.UnitTests
{
public class ComponentRenderingTest : BasicTestAppTestBase
{
public ComponentRenderingTest(
BrowserFixture browserFixture,
ToggleExecutionModeServerFixture<DevHostServerProgram> serverFixture,
ITestOutputHelper output )
: base( browserFixture, serverFixture, output )
public ComponentRenderingTest( BrowserFixture browserFixture,
ToggleExecutionModeServerFixture<DevHostServerProgram> serverFixture,
ITestOutputHelper output )
: base( browserFixture, serverFixture, output )
{
Navigate( ServerPathBase, noReload: !serverFixture.UsingAspNetHost );
}
Expand All @@ -40,7 +38,76 @@ public void CanRenderButtonComponent()
{
var appElement = MountTestComponent<ButtonComponent>();
var btnElement = appElement.FindElement( By.TagName( "button" ) );
Assert.Equal( "hello", btnElement.Text );
Assert.Equal( "hello primary", btnElement.Text );
Assert.Equal( "btn btn-primary", btnElement.GetAttribute( "class" ) );
}

[Fact]
public void CanShowAndHideDropdownComponent()
{
var appElement = MountTestComponent<DropdownComponent>();
var drpElement = appElement.FindElement( By.ClassName( "dropdown" ) );
var btnElement = drpElement.FindElement( By.TagName( "button" ) );
var mnuElement = drpElement.FindElement( By.ClassName( "dropdown-menu" ) );

btnElement.Click();
WaitAssert.Contains( "show", () => drpElement.GetAttribute( "class" ) );
WaitAssert.Contains( "show", () => mnuElement.GetAttribute( "class" ) );

btnElement.Click();
WaitAssert.NotContains( "show", () => drpElement.GetAttribute( "class" ) );
WaitAssert.NotContains( "show", () => mnuElement.GetAttribute( "class" ) );
}

[Fact]
public void CanTextEditChangeText()
{
var appElement = MountTestComponent<TextEditComponent>();
var txtElement = appElement.FindElement( By.TagName( "input" ) );

txtElement.SendKeys( "abc" );
WaitAssert.Contains( "abc", () => txtElement.GetAttribute( "value" ) );
}

[Fact]
public void CanTextEditBindValue()
{
var appElement = MountTestComponent<TextEditComponent>();
var txtElement = appElement.FindElement( By.TagName( "input" ) );
var output = Browser.FindElement( By.Id( "test-result" ) );

WaitAssert.Equal( string.Empty, () => output.Text );

SendKeysSequentially( txtElement, "abcdefghijklmnopqrstuvwxyz" );
WaitAssert.Equal( "abcdefghijklmnopqrstuvwxyz", () => output.Text );

txtElement.SendKeys( Keys.Backspace );
WaitAssert.Equal( "abcdefghijklmnopqrstuvwxy", () => output.Text );
}

[Fact]
public void CanValidateTextEditComponent()
{
var appElement = MountTestComponent<TextEditValidationComponent>();
var txtElement = appElement.FindElement( By.TagName( "input" ) );

Assert.NotNull( appElement.FindElement( By.ClassName( "invalid-feedback" ) ) );

txtElement.SendKeys( "a" );
WaitAssert.NotNull( () => appElement.FindElement( By.ClassName( "valid-feedback" ) ) );

txtElement.SendKeys( Keys.Backspace );
WaitAssert.NotNull( () => appElement.FindElement( By.ClassName( "invalid-feedback" ) ) );
}

void SendKeysSequentially( IWebElement target, string text )
{
// Calling it for each character works around some chars being skipped
// https://stackoverflow.com/a/40986041
foreach ( var c in text )
{
target.SendKeys( c.ToString() );
}
}
}
}
Expand Up @@ -7,7 +7,7 @@
using OpenQA.Selenium.Support.UI;
using System;
using Xunit.Abstractions;
using Blazorise.BasicTestApp.Server;
using BasicTestApp.Server;

namespace Blazorise.UnitTests.Infrastructure
{
Expand Down
Expand Up @@ -3,7 +3,7 @@
using System.Text;
using System.Threading.Tasks;

namespace Blazorise.UnitTests
namespace Blazorise.UnitTests.Infrastructure
{
public static class Extensions
{
Expand Down
Expand Up @@ -5,7 +5,7 @@
using System.Runtime.InteropServices;
using System.Text;

namespace Blazorise.UnitTests
namespace Blazorise.UnitTests.Infrastructure
{
internal static class ProcessExtensions
{
Expand Down
Expand Up @@ -3,7 +3,7 @@

using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic;
using DevHostServerProgram = Blazorise.BasicTestApp.Server.Program;
using DevHostServerProgram = BasicTestApp.Server.Program;

namespace Blazorise.UnitTests.Infrastructure.ServerFixtures
{
Expand Down
18 changes: 9 additions & 9 deletions Tests/Blazorise.UnitTests/Infrastructure/ServerTestBase.cs
Expand Up @@ -10,31 +10,31 @@ namespace Blazorise.UnitTests.Infrastructure
{
public abstract class ServerTestBase<TServerFixture>
: BrowserTestBase, IClassFixture<TServerFixture>
where TServerFixture: ServerFixture
where TServerFixture : ServerFixture
{
protected readonly TServerFixture _serverFixture;

public ServerTestBase(BrowserFixture browserFixture, TServerFixture serverFixture, ITestOutputHelper output)
: base(browserFixture, output)
public ServerTestBase( BrowserFixture browserFixture, TServerFixture serverFixture, ITestOutputHelper output )
: base( browserFixture, output )
{
_serverFixture = serverFixture;
}

public void Navigate(string relativeUrl, bool noReload = false)
public void Navigate( string relativeUrl, bool noReload = false )
{
var absoluteUrl = new Uri(_serverFixture.RootUri, relativeUrl);
var absoluteUrl = new Uri( _serverFixture.RootUri, relativeUrl );

if (noReload)
if ( noReload )
{
var existingUrl = Browser.Url;
if (string.Equals(existingUrl, absoluteUrl.AbsoluteUri, StringComparison.Ordinal))
if ( string.Equals( existingUrl, absoluteUrl.AbsoluteUri, StringComparison.Ordinal ) )
{
return;
}
}

Browser.Navigate().GoToUrl("about:blank");
Browser.Navigate().GoToUrl(absoluteUrl);
Browser.Navigate().GoToUrl( "about:blank" );
Browser.Navigate().GoToUrl( absoluteUrl );
}
}
}

0 comments on commit 4436a38

Please sign in to comment.