Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testing for accessibility, Playwright, and mocked API #20

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions server/Tests/AccessibilityTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Threading.Tasks;
using Xunit;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;
using AxeCore.NET;

public class AccessibilityTests : PageTest
{
[Fact]
public async Task CheckAccessibility()
{
await Page.GotoAsync("http://localhost:5000");

var axeBuilder = new AxeBuilder(Page);
var results = await axeBuilder.AnalyzeAsync();

Assert.Empty(results.Violations);
}

[Fact]
public async Task CheckButtonAccessibility()
{
await Page.GotoAsync("http://localhost:5000");

var button = await Page.QuerySelectorAsync("button");
var axeBuilder = new AxeBuilder(button);
var results = await axeBuilder.AnalyzeAsync();

Assert.Empty(results.Violations);
}

[Fact]
public async Task CheckFormAccessibility()
{
await Page.GotoAsync("http://localhost:5000");

var form = await Page.QuerySelectorAsync("form");
var axeBuilder = new AxeBuilder(form);
var results = await axeBuilder.AnalyzeAsync();

Assert.Empty(results.Violations);
}
}
61 changes: 61 additions & 0 deletions server/Tests/MockApiTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Moq;
using Xunit;

public class MockApiTests
{
private readonly Mock<HttpMessageHandler> _mockHttpMessageHandler;
private readonly HttpClient _httpClient;

public MockApiTests()
{
_mockHttpMessageHandler = new Mock<HttpMessageHandler>();
_httpClient = new HttpClient(_mockHttpMessageHandler.Object);
}

[Fact]
public async Task Test_GetEndpoint_ReturnsExpectedResponse()
{
// Arrange
var expectedResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("{\"message\":\"Hello, World!\"}")
};

_mockHttpMessageHandler
.Setup(handler => handler.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedResponse);

// Act
var response = await _httpClient.GetAsync("https://api.example.com/endpoint");
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal("{\"message\":\"Hello, World!\"}", content);
}

[Fact]
public async Task Test_PostEndpoint_ReturnsExpectedResponse()
{
// Arrange
var expectedResponse = new HttpResponseMessage(System.Net.HttpStatusCode.Created)
{
Content = new StringContent("{\"id\":1,\"message\":\"Created\"}")
};

_mockHttpMessageHandler
.Setup(handler => handler.SendAsync(It.IsAny<HttpRequestMessage>(), It.IsAny<CancellationToken>()))
.ReturnsAsync(expectedResponse);

var postData = new StringContent("{\"name\":\"Test\"}");

// Act
var response = await _httpClient.PostAsync("https://api.example.com/endpoint", postData);
var content = await response.Content.ReadAsStringAsync();

// Assert
Assert.Equal("{\"id\":1,\"message\":\"Created\"}", content);
}
}
39 changes: 39 additions & 0 deletions server/Tests/PlaywrightTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using System;
using System.Threading.Tasks;
using Xunit;
using Microsoft.Playwright;
using Microsoft.Playwright.NUnit;

public class PlaywrightTests : PageTest
{
[Fact]
public async Task CheckHomePageTitle()
{
await Page.GotoAsync("http://localhost:5000");
Assert.Equal("Home - Tailwind Traders", await Page.TitleAsync());
}

[Fact]
public async Task CheckButtonExists()
{
await Page.GotoAsync("http://localhost:5000");
var button = await Page.QuerySelectorAsync("button");
Assert.NotNull(button);
}

[Fact]
public async Task CheckFormExists()
{
await Page.GotoAsync("http://localhost:5000");
var form = await Page.QuerySelectorAsync("form");
Assert.NotNull(form);
}

[Fact]
public async Task CheckLinkExists()
{
await Page.GotoAsync("http://localhost:5000");
var link = await Page.QuerySelectorAsync("a");
Assert.NotNull(link);
}
}