Skip to content

Commit 7792bb9

Browse files
Adding tests
1 parent fae461a commit 7792bb9

File tree

9 files changed

+170
-26
lines changed

9 files changed

+170
-26
lines changed

src/Components/Blazor/Templates/src/content/BlazorWasm-CSharp/Client/wwwroot/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<div id="error-ui">
1717
An unhandled error has occurred.
1818
<a href class='reload'>Reload</a>
19-
<a href class='dismiss'>🗙</a>
19+
<a class='dismiss'>🗙</a>
2020
</div>
2121
<script src="_framework/blazor.webassembly.js"></script>
2222
</body>

src/Components/Web.JS/src/Platform/Mono/MonoPlatform.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { MethodHandle, System_Object, System_String, System_Array, Pointer, Platform } from '../Platform';
22
import { getFileNameFromUrl } from '../Url';
33
import { attachDebuggerHotkey, hasDebuggingEnabled } from './MonoDebugger';
4+
import { showErrorNotification } from '../../BootErrors';
45

56
const assemblyHandleCache: { [assemblyName: string]: number } = {};
67
const typeHandleCache: { [fullyQualifiedTypeName: string]: number } = {};
@@ -233,31 +234,9 @@ function createEmscriptenModuleInstance(loadAssemblyUrls: string[], onReady: ()
233234

234235
module.print = line => (suppressMessages.indexOf(line) < 0 && console.log(`WASM: ${line}`));
235236

236-
const errorUiReloads = document.querySelectorAll<HTMLElement>('#error-ui .reload');
237-
errorUiReloads.forEach(reload => {
238-
reload.onclick = function (e) {
239-
location.reload();
240-
e.preventDefault();
241-
};
242-
});
243-
244-
let errorUiDismiss = document.querySelectorAll<HTMLElement>('#error-ui .dismiss');
245-
errorUiDismiss.forEach(dismiss => {
246-
dismiss.onclick = function (e) {
247-
const errorUi = document.querySelector<HTMLElement>('#error-ui');
248-
if (errorUi) {
249-
errorUi.style.display = 'none';
250-
}
251-
e.preventDefault();
252-
};
253-
});
254-
255237
module.printErr = line => {
256238
console.error(`WASM: ${line}`);
257-
let errorUi = document.querySelector('#error-ui') as HTMLElement;
258-
if (errorUi) {
259-
errorUi.style.display = 'block';
260-
}
239+
showErrorNotification();
261240
};
262241
module.preRun = [];
263242
module.postRun = [];
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using BasicTestApp;
5+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
6+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
7+
using Microsoft.AspNetCore.E2ETesting;
8+
using OpenQA.Selenium;
9+
using Xunit;
10+
using Xunit.Abstractions;
11+
12+
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
13+
{
14+
public class ErrorNotificationClientSideTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
15+
{
16+
public ErrorNotificationClientSideTest(
17+
BrowserFixture browserFixture,
18+
ToggleExecutionModeServerFixture<Program> serverFixture,
19+
ITestOutputHelper output)
20+
: base(browserFixture, serverFixture, output)
21+
{
22+
}
23+
24+
protected override void InitializeAsyncCore()
25+
{
26+
// On WebAssembly, page reloads are expensive so skip if possible
27+
Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Client);
28+
Browser.MountTestComponent<ErrorComponent>();
29+
Browser.Exists(By.Id("error-ui"));
30+
Browser.Exists(By.TagName("button"));
31+
}
32+
33+
[Fact]
34+
public void ShowsErrorNotificationClientSide_OnError()
35+
{
36+
var errorUi = Browser.FindElement(By.Id("error-ui"));
37+
Assert.Equal("none", errorUi.GetCssValue("display"));
38+
39+
var causeErrorButton = Browser.FindElement(By.TagName("button"));
40+
causeErrorButton.Click();
41+
42+
Browser.Exists(By.CssSelector("#error-ui[style='display: block;']"));
43+
44+
var reload = Browser.FindElement(By.ClassName("reload"));
45+
reload.Click();
46+
47+
Browser.MountTestComponent<ErrorComponent>();
48+
causeErrorButton = Browser.Exists(By.TagName("button"));
49+
errorUi = Browser.FindElement(By.Id("error-ui"));
50+
Assert.Equal("none", errorUi.GetCssValue("display"));
51+
52+
causeErrorButton.Click();
53+
Assert.Equal("block", errorUi.GetCssValue("display"));
54+
55+
var dismiss = Browser.FindElement(By.ClassName("dismiss"));
56+
dismiss.Click();
57+
Browser.Exists(By.CssSelector("#error-ui"));
58+
Browser.Exists(By.CssSelector("#error-ui[style='display: none;']"));
59+
}
60+
}
61+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using BasicTestApp;
5+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure;
6+
using Microsoft.AspNetCore.Components.E2ETest.Infrastructure.ServerFixtures;
7+
using Microsoft.AspNetCore.Components.E2ETest.ServerExecutionTests;
8+
using Microsoft.AspNetCore.E2ETesting;
9+
using OpenQA.Selenium;
10+
using Xunit;
11+
using Xunit.Abstractions;
12+
13+
namespace Microsoft.AspNetCore.Components.E2ETest.Tests
14+
{
15+
public class ErrorNotificationServerSideTest : ServerTestBase<ToggleExecutionModeServerFixture<Program>>
16+
{
17+
public ErrorNotificationServerSideTest(
18+
BrowserFixture browserFixture,
19+
ToggleExecutionModeServerFixture<Program> serverFixture,
20+
ITestOutputHelper output)
21+
: base(browserFixture, serverFixture.WithServerExecution(), output)
22+
{
23+
}
24+
25+
protected override void InitializeAsyncCore()
26+
{
27+
// On WebAssembly, page reloads are expensive so skip if possible
28+
Navigate(ServerPathBase, noReload: _serverFixture.ExecutionMode == ExecutionMode.Server);
29+
Browser.MountTestComponent<ErrorComponent>();
30+
Browser.Exists(By.Id("error-ui"));
31+
Browser.Exists(By.TagName("button"));
32+
}
33+
34+
[Fact]
35+
public void ShowsErrorNotificationServerSide_OnError()
36+
{
37+
var errorUi = Browser.FindElement(By.Id("error-ui"));
38+
Assert.Equal("none", errorUi.GetCssValue("display"));
39+
40+
var causeErrorButton = Browser.FindElement(By.TagName("button"));
41+
causeErrorButton.Click();
42+
43+
Browser.Exists(By.CssSelector("#error-ui[style='display: block;']"));
44+
45+
var reload = Browser.FindElement(By.ClassName("reload"));
46+
reload.Click();
47+
48+
Browser.MountTestComponent<ErrorComponent>();
49+
causeErrorButton = Browser.Exists(By.TagName("button"));
50+
errorUi = Browser.FindElement(By.Id("error-ui"));
51+
Assert.Equal("none", errorUi.GetCssValue("display"));
52+
53+
causeErrorButton.Click();
54+
Assert.Equal("block", errorUi.GetCssValue("display"));
55+
56+
var dismiss = Browser.FindElement(By.ClassName("dismiss"));
57+
dismiss.Click();
58+
Browser.Exists(By.CssSelector("#error-ui"));
59+
Browser.Exists(By.CssSelector("#error-ui[style='display: none;']"));
60+
Assert.Equal("none", errorUi.GetCssValue("display"));
61+
}
62+
}
63+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<div>
2+
<h2>Error throwing button</h2>
3+
<p>
4+
<button @onclick="@(IncrementCount)">Click me</button>
5+
</p>
6+
</div>
7+
8+
@code {
9+
int currentCount = 0;
10+
11+
void IncrementCount()
12+
{
13+
currentCount++;
14+
throw new NotImplementedException("Doing crazy things!");
15+
}
16+
17+
public void Reset()
18+
{
19+
currentCount = 0;
20+
StateHasChanged();
21+
}
22+
}

src/Components/test/testassets/BasicTestApp/Index.razor

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@using Microsoft.AspNetCore.Components.Rendering
22
<div id="test-selector">
3-
Select test:
3+
Select test:
44
<select id="test-selector-select" @bind=SelectedComponentTypeName>
55
<option value="none">Choose...</option>
66
<option value="BasicTestApp.AddRemoveChildComponents">Add/remove child components</option>
@@ -67,6 +67,7 @@
6767
<option value="BasicTestApp.SvgWithChildComponent">SVG with child component</option>
6868
<option value="BasicTestApp.TextOnlyComponent">Plain text</option>
6969
<option value="BasicTestApp.TouchEventComponent">Touch events</option>
70+
<option value="BasicTestApp.ErrorComponent">Error throwing</option>
7071
</select>
7172

7273
<span id="runtime-info"><code><tt>@System.Runtime.InteropServices.RuntimeInformation.FrameworkDescription</tt></code></span>
@@ -82,6 +83,13 @@
8283
@((RenderFragment)RenderSelectedComponent)
8384
</app>
8485

86+
87+
<div id="error-ui">
88+
An unhandled error has occurred.
89+
<a href class='reload'>Reload</a>
90+
<a class='dismiss' style="cursor: pointer;">🗙</a>
91+
</div>
92+
8593
@code {
8694
string SelectedComponentTypeName { get; set; } = "none";
8795

src/Components/test/testassets/BasicTestApp/wwwroot/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<!DOCTYPE html>
22
<html>
3+
34
<head>
45
<meta charset="utf-8" />
56
<title>Basic test app</title>
@@ -9,6 +10,7 @@
910
<!-- Used by ExternalContentPackage -->
1011
<link href="_content/TestContentPackage/styles.css" rel="stylesheet" />
1112
</head>
13+
1214
<body>
1315
<root>Loading...</root>
1416

@@ -31,4 +33,5 @@
3133
<!-- Used by ExternalContentPackage -->
3234
<script src="_content/TestContentPackage/prompt.js"></script>
3335
</body>
36+
3437
</html>

src/Components/test/testassets/BasicTestApp/wwwroot/style.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
outline: 1px solid red;
77
}
88

9+
#error-ui {
10+
display: none;
11+
}
12+
13+
#error-ui dismiss {
14+
cursor: pointer;
15+
}
16+
917
.validation-message {
1018
color: red;
1119
}

src/ProjectTemplates/Web.ProjectTemplates/content/BlazorServerWeb-CSharp/Pages/_Host.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
An unhandled exception has occurred. See browser dev tools for details.
2626
</environment>
2727
<a href class='reload'>Reload</a>
28-
<a href class='dismiss'>🗙</a>
28+
<a class='dismiss'>🗙</a>
2929
</div>
3030

3131
<script src="_framework/blazor.server.js"></script>

0 commit comments

Comments
 (0)