Skip to content

Commit

Permalink
Update E2E test for the "ExcludeSelector" option
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamoto committed Oct 1, 2023
1 parent 97871eb commit 066e181
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
39 changes: 39 additions & 0 deletions HotKeys2.E2ETest/HotKeysOnBrowserTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,45 @@ public async Task ExcludeContentEditable_Test(HostingModel hostingModel)
await page.AssertUrlIsAsync(host.GetUrl("/fetchdata"));
}

[Test]
[TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))]
public async Task ExcludeSelector_Test(HostingModel hostingModel)
{
var context = TestContext.Instance;
var host = await context.StartHostAsync(hostingModel);

// Navigate to the "Counter" page,
var page = await context.GetPageAsync();
await page.GotoAndWaitForReadyAsync(host.GetUrl("/counter"));

// Verify the counter is 0.
var counter = page.Locator("h1+p");
await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 0");

// Set focus to the "Hotkeys are enabled in this field" input element, and type "U" key.
// Then the counter should be incremented.
var inputElement1 = page.GetByPlaceholder("Hotkeys are enabled in this field");
await inputElement1.FocusAsync();
await page.Keyboard.DownAsync("u");
await page.Keyboard.UpAsync("u");
await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 1");
await page.Keyboard.DownAsync("u");
await page.Keyboard.UpAsync("u");
await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2");

// Set focus to the "Hotkeys are disabled in this field" input element, and type "U" key.
// Then the counter should not be incremented.
var inputElement2 = page.GetByPlaceholder("Hotkeys are disabled in this field");
await inputElement2.FocusAsync();
await page.Keyboard.DownAsync("u");
await page.Keyboard.UpAsync("u");
await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2");
await page.Keyboard.DownAsync("u");
await page.Keyboard.UpAsync("u");
await page.AssertEqualsAsync(_ => counter.TextContentAsync(), "Current count: 2");
await page.AssertEqualsAsync(_ => inputElement2.InputValueAsync(), "uu");
}

[Test]
[TestCaseSource(typeof(HotKeysOnBrowserTest), nameof(AllHostingModels))]
public async Task ByNativeKey_Test(HostingModel hostingModel)
Expand Down
10 changes: 9 additions & 1 deletion SampleSites/Components/Pages/Counter.razor
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
</div>
}

<div>
<input type="text" class="form-control" placeholder="Hotkeys are enabled in this field." />
</div>

<div class="mt-4 disabled-hotkeys">
<input type="text" class="form-control" placeholder="Hotkeys are disabled in this field." />
</div>

@code {
private readonly bool IsWasm = RuntimeInformation.OSDescription == "web" || RuntimeInformation.OSDescription == "Browser";

Expand All @@ -27,7 +35,7 @@
protected override void OnInitialized()
{
this.HotKeysContext = this.HotKeys.CreateContext()
.Add(Code.U, this.IncrementCount);
.Add(Code.U, this.IncrementCount, new() { Exclude = Exclude.None, ExcludeSelector = ".disabled-hotkeys *" });
this.HotKeys.KeyDown += HotKeys_KeyDown;
}

Expand Down

0 comments on commit 066e181

Please sign in to comment.