Add JsonUtil unit tests plus E2E tests for new StandaloneApp pages

This commit is contained in:
Steve Sanderson 2018-02-26 13:01:11 +00:00
parent 1c5acfbdcc
commit 02a0be5c2b
2 changed files with 122 additions and 0 deletions

View File

@ -6,6 +6,7 @@ using Microsoft.AspNetCore.Blazor.E2ETest.Infrastructure.ServerFixtures;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.UI;
using System;
using System.Linq;
using Xunit;
namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
@ -71,6 +72,47 @@ namespace Microsoft.AspNetCore.Blazor.E2ETest.Tests
item => Assert.Equal("Home", item.Text));
}
[Fact]
public void HasCounterPage()
{
// Navigate to "Counter"
Browser.FindElement(By.LinkText("Counter")).Click();
Assert.Equal("Counter", Browser.FindElement(By.TagName("h1")).Text);
// Observe the initial value is zero
var countDisplayElement = Browser.FindElement(By.CssSelector("h1 + p"));
Assert.Equal("Current count: 0", countDisplayElement.Text);
// Click the button; see it counts
var button = Browser.FindElement(By.CssSelector(".col-sm-9 button"));
button.Click();
button.Click();
button.Click();
Assert.Equal("Current count: 3", countDisplayElement.Text);
}
[Fact]
public void HasFetchDataPage()
{
// Navigate to "Counter"
Browser.FindElement(By.LinkText("Fetch data")).Click();
Assert.Equal("Weather forecast", Browser.FindElement(By.TagName("h1")).Text);
// Wait until loaded
var tableSelector = By.CssSelector("table.table");
new WebDriverWait(Browser, TimeSpan.FromSeconds(10)).Until(
driver => driver.FindElement(tableSelector) != null);
// Check the table is displayed correctly
var rows = Browser.FindElements(By.CssSelector("table.table tbody tr"));
Assert.Equal(5, rows.Count);
var cells = rows.SelectMany(row => row.FindElements(By.TagName("td")));
foreach (var cell in cells)
{
Assert.True(!string.IsNullOrEmpty(cell.Text));
}
}
private void WaitUntilLoaded()
{
new WebDriverWait(Browser, TimeSpan.FromSeconds(30)).Until(

View File

@ -0,0 +1,80 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Xunit;
namespace Microsoft.AspNetCore.Blazor.Test
{
public class JsonUtilTest
{
// It's not useful to have a complete set of behavior specifications for
// what the JSON serializer/deserializer does in all cases here. We merely
// expose a simple wrapper over a third-party library that maintains its
// own specs and tests.
//
// We should only add tests here to cover behaviors that Blazor itself
// depends on.
[Theory]
[InlineData(null, "null")]
[InlineData("My string", "\"My string\"")]
[InlineData(123, "123")]
[InlineData(123.456f, "123.456")]
[InlineData(123.456d, "123.456")]
[InlineData(true, "true")]
public void CanSerializePrimitivesToJson(object value, string expectedJson)
{
Assert.Equal(expectedJson, JsonUtil.Serialize(value));
}
[Theory]
[InlineData("null", null)]
[InlineData("\"My string\"", "My string")]
[InlineData("123", 123L)] // Would also accept 123 as a System.Int32, but Int64 is fine as a default
[InlineData("123.456", 123.456d)]
[InlineData("true", true)]
public void CanDeserializePrimitivesFromJson(string json, object expectedValue)
{
Assert.Equal(expectedValue, JsonUtil.Deserialize<object>(json));
}
[Fact]
public void CanSerializeClassToJson()
{
// Arrange
var person = new Person
{
Id = 1844,
Name = "Athos",
Pets = new[] { "Aramis", "Porthos", "D'Artagnan" }
};
// Act/Assert
Assert.Equal(
"{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"]}",
JsonUtil.Serialize(person));
}
[Fact]
public void CanDeserializeClassFromJson()
{
// Arrange
var json = "{\"Id\":1844,\"Name\":\"Athos\",\"Pets\":[\"Aramis\",\"Porthos\",\"D'Artagnan\"]}";
// Act
var person = JsonUtil.Deserialize<Person>(json);
// Assert
Assert.Equal(1844, person.Id);
Assert.Equal("Athos", person.Name);
Assert.Equal(new[] { "Aramis", "Porthos", "D'Artagnan" }, person.Pets);
}
class Person
{
public int Id { get; set; }
public string Name { get; set; }
public string[] Pets { get; set; }
}
}
}