|
| 1 | +using System.Threading.Tasks; |
| 2 | +using Microsoft.Playwright; |
| 3 | +using Soenneker.Playwrights.Extensions.TestPages; |
| 4 | +using Soenneker.Playwrights.Session; |
| 5 | +using Soenneker.Playwrights.Tests.Unit; |
| 6 | +using Xunit; |
| 7 | + |
| 8 | +namespace Soenneker.Quark.Suite.Playwrights.Tests; |
| 9 | + |
| 10 | +[Collection("Collection")] |
| 11 | +public sealed class QuarkFormsPlaywrightTests : PlaywrightUnitTest |
| 12 | +{ |
| 13 | + public QuarkFormsPlaywrightTests(QuarkPlaywrightFixture fixture, ITestOutputHelper outputHelper) : base(fixture, outputHelper) |
| 14 | + { |
| 15 | + } |
| 16 | + |
| 17 | + [Fact] |
| 18 | + public async ValueTask Toggle_demo_updates_pressed_state_and_bound_label() |
| 19 | + { |
| 20 | + await using BrowserSession session = await CreateSession(); |
| 21 | + IPage page = session.Page; |
| 22 | + |
| 23 | + await page.GotoAndWaitForReady( |
| 24 | + $"{BaseUrl}toggles", |
| 25 | + static p => p.Locator("section").Filter(new LocatorFilterOptions { HasText = "Additional examples" }).GetByRole(AriaRole.Button, new LocatorGetByRoleOptions { Name = "Toggle bookmark", Exact = true }), |
| 26 | + expectedTitle: "Toggle - Quark Suite"); |
| 27 | + |
| 28 | + ILocator bookmarkToggle = page.Locator("section").Filter(new LocatorFilterOptions { HasText = "Additional examples" }).GetByRole(AriaRole.Button, new LocatorGetByRoleOptions { Name = "Toggle bookmark", Exact = true }); |
| 29 | + |
| 30 | + await Assertions.Expect(bookmarkToggle).ToHaveAttributeAsync("aria-pressed", "false"); |
| 31 | + await Assertions.Expect(bookmarkToggle).ToContainTextAsync("Bookmark"); |
| 32 | + |
| 33 | + await bookmarkToggle.ClickAsync(); |
| 34 | + |
| 35 | + await Assertions.Expect(bookmarkToggle).ToHaveAttributeAsync("aria-pressed", "true"); |
| 36 | + await Assertions.Expect(bookmarkToggle).ToContainTextAsync("Saved"); |
| 37 | + |
| 38 | + await bookmarkToggle.ClickAsync(); |
| 39 | + |
| 40 | + await Assertions.Expect(bookmarkToggle).ToHaveAttributeAsync("aria-pressed", "false"); |
| 41 | + await Assertions.Expect(bookmarkToggle).ToContainTextAsync("Bookmark"); |
| 42 | + } |
| 43 | + |
| 44 | + [Fact] |
| 45 | + public async ValueTask Toggle_group_demo_enforces_single_selection_and_preserves_multiple_selection() |
| 46 | + { |
| 47 | + await using BrowserSession session = await CreateSession(); |
| 48 | + IPage page = session.Page; |
| 49 | + |
| 50 | + await page.GotoAndWaitForReady( |
| 51 | + $"{BaseUrl}toggle-groups", |
| 52 | + static p => p.GetByRole(AriaRole.Radio, new PageGetByRoleOptions { Name = "Toggle italic", Exact = true }), |
| 53 | + expectedTitle: "Toggle Group - Quark Suite"); |
| 54 | + |
| 55 | + ILocator singleItalic = page.GetByRole(AriaRole.Radio, new PageGetByRoleOptions { Name = "Toggle italic", Exact = true }).First; |
| 56 | + ILocator singleBold = page.GetByRole(AriaRole.Radio, new PageGetByRoleOptions { Name = "Toggle bold", Exact = true }).First; |
| 57 | + |
| 58 | + await Assertions.Expect(singleItalic).ToHaveAttributeAsync("aria-checked", "true"); |
| 59 | + await Assertions.Expect(singleBold).ToHaveAttributeAsync("aria-checked", "false"); |
| 60 | + |
| 61 | + await singleBold.ClickAsync(); |
| 62 | + |
| 63 | + await Assertions.Expect(singleBold).ToHaveAttributeAsync("aria-checked", "true"); |
| 64 | + await Assertions.Expect(singleItalic).ToHaveAttributeAsync("aria-checked", "false"); |
| 65 | + |
| 66 | + ILocator multipleBookmark = page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Toggle bookmark", Exact = true }).First; |
| 67 | + ILocator multipleStar = page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Toggle star", Exact = true }); |
| 68 | + |
| 69 | + await Assertions.Expect(multipleBookmark).ToHaveAttributeAsync("aria-pressed", "true"); |
| 70 | + await Assertions.Expect(multipleStar).ToHaveAttributeAsync("aria-pressed", "false"); |
| 71 | + |
| 72 | + await multipleStar.ClickAsync(); |
| 73 | + |
| 74 | + await Assertions.Expect(multipleBookmark).ToHaveAttributeAsync("aria-pressed", "true"); |
| 75 | + await Assertions.Expect(multipleStar).ToHaveAttributeAsync("aria-pressed", "true"); |
| 76 | + } |
| 77 | + |
| 78 | + [Fact] |
| 79 | + public async ValueTask Select_demo_updates_selection_and_closes_listbox() |
| 80 | + { |
| 81 | + await using BrowserSession session = await CreateSession(); |
| 82 | + IPage page = session.Page; |
| 83 | + |
| 84 | + await page.GotoAndWaitForReady( |
| 85 | + $"{BaseUrl}selects", |
| 86 | + static p => p.GetByRole(AriaRole.Combobox).First, |
| 87 | + expectedTitle: "Select Component - Quark Suite"); |
| 88 | + |
| 89 | + ILocator trigger = page.GetByRole(AriaRole.Combobox).First; |
| 90 | + |
| 91 | + await Assertions.Expect(trigger).ToContainTextAsync("Select a fruit"); |
| 92 | + |
| 93 | + await trigger.ClickAsync(); |
| 94 | + |
| 95 | + ILocator listbox = page.Locator("[role='listbox']").First; |
| 96 | + await Assertions.Expect(listbox).ToBeVisibleAsync(); |
| 97 | + |
| 98 | + await page.GetByRole(AriaRole.Option, new PageGetByRoleOptions { Name = "Banana", Exact = true }).ClickAsync(); |
| 99 | + |
| 100 | + await Assertions.Expect(trigger).ToContainTextAsync("Banana"); |
| 101 | + await Assertions.Expect(listbox).Not.ToBeVisibleAsync(); |
| 102 | + } |
| 103 | + |
| 104 | + [Fact] |
| 105 | + public async ValueTask Select_form_demo_requires_selection_before_submit() |
| 106 | + { |
| 107 | + await using BrowserSession session = await CreateSession(); |
| 108 | + IPage page = session.Page; |
| 109 | + |
| 110 | + await page.GotoAndWaitForReady( |
| 111 | + $"{BaseUrl}selects", |
| 112 | + static p => p.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Submit", Exact = true }), |
| 113 | + expectedTitle: "Select Component - Quark Suite"); |
| 114 | + |
| 115 | + ILocator submit = page.GetByRole(AriaRole.Button, new PageGetByRoleOptions { Name = "Submit", Exact = true }); |
| 116 | + await submit.ClickAsync(); |
| 117 | + |
| 118 | + await Assertions.Expect(page.GetByText("Please select an email to display.", new PageGetByTextOptions { Exact = true })).ToBeVisibleAsync(); |
| 119 | + } |
| 120 | +} |
0 commit comments