|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the Apache 2.0 License |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | +// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone |
| 5 | + |
| 6 | +using AngleSharp.Dom; |
| 7 | +using Microsoft.AspNetCore.Components.Rendering; |
| 8 | + |
| 9 | +namespace UnitTest.Services; |
| 10 | + |
| 11 | +public class FullScreenServiceTest : BootstrapBlazorTestBase |
| 12 | +{ |
| 13 | + [Fact] |
| 14 | + public async Task ButtonIcon_Ok() |
| 15 | + { |
| 16 | + var cut = Context.RenderComponent<FullScreenButton>(builder => |
| 17 | + { |
| 18 | + builder.Add(s => s.Icon, "fa-solid fa-maximize"); |
| 19 | + builder.Add(s => s.Text, "button-text"); |
| 20 | + }); |
| 21 | + cut.Contains("bb-fs-off"); |
| 22 | + cut.Contains("bb-fs-on"); |
| 23 | + |
| 24 | + var element = cut.Find(".btn-fs"); |
| 25 | + await cut.InvokeAsync(() => element.Click()); |
| 26 | + } |
| 27 | + |
| 28 | + [Fact] |
| 29 | + public void FullScreenIcon_Ok() |
| 30 | + { |
| 31 | + var cut = Context.RenderComponent<FullScreenButton>(builder => builder.Add(s => s.Icon, "fa-test")); |
| 32 | + cut.Contains("fa-test"); |
| 33 | + } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public async Task ToggleByElement_Ok() |
| 37 | + { |
| 38 | + ElementReference element = default; |
| 39 | + var cut = Context.Render(builder => |
| 40 | + { |
| 41 | + builder.OpenElement(0, "div"); |
| 42 | + builder.AddElementReferenceCapture(1, e => |
| 43 | + { |
| 44 | + element = e; |
| 45 | + }); |
| 46 | + builder.CloseElement(); |
| 47 | + |
| 48 | + builder.OpenComponent<MockFullScreen>(0); |
| 49 | + builder.CloseComponent(); |
| 50 | + }); |
| 51 | + var fs = cut.FindComponent<MockFullScreen>(); |
| 52 | + await cut.InvokeAsync(() => fs.Instance.Test(element)); |
| 53 | + } |
| 54 | + |
| 55 | + [Fact] |
| 56 | + public async Task ToggleById_Ok() |
| 57 | + { |
| 58 | + var cut = Context.Render(builder => |
| 59 | + { |
| 60 | + builder.OpenElement(0, "div"); |
| 61 | + builder.AddAttribute(1, "id", "test-id"); |
| 62 | + builder.CloseElement(); |
| 63 | + |
| 64 | + builder.OpenComponent<MockFullScreen>(0); |
| 65 | + builder.CloseComponent(); |
| 66 | + }); |
| 67 | + var fs = cut.FindComponent<MockFullScreen>(); |
| 68 | + await cut.InvokeAsync(() => fs.Instance.TestById("test-id")); |
| 69 | + } |
| 70 | + |
| 71 | + [Fact] |
| 72 | + public async Task Toggle_Ok() |
| 73 | + { |
| 74 | + var cut = Context.Render(builder => |
| 75 | + { |
| 76 | + builder.OpenElement(0, "div"); |
| 77 | + builder.AddAttribute(1, "id", "test-id"); |
| 78 | + builder.CloseElement(); |
| 79 | + |
| 80 | + builder.OpenComponent<MockFullScreen>(0); |
| 81 | + builder.CloseComponent(); |
| 82 | + }); |
| 83 | + var fs = cut.FindComponent<MockFullScreen>(); |
| 84 | + await cut.InvokeAsync(fs.Instance.Toggle); |
| 85 | + } |
| 86 | + |
| 87 | + [Fact] |
| 88 | + public void FullScreenOption_Ok() |
| 89 | + { |
| 90 | + var option = new FullScreenOption() { Element = new("test01", null), Id = "test" }; |
| 91 | + Assert.NotNull(option.Id); |
| 92 | + Assert.Null(option.Element.Context); |
| 93 | + } |
| 94 | + |
| 95 | + private class MockFullScreen : ComponentBase |
| 96 | + { |
| 97 | + [Inject] |
| 98 | + [NotNull] |
| 99 | + public FullScreenService? FullScreenService { get; set; } |
| 100 | + |
| 101 | + protected override void BuildRenderTree(RenderTreeBuilder builder) |
| 102 | + { |
| 103 | + base.BuildRenderTree(builder); |
| 104 | + } |
| 105 | + |
| 106 | + public Task Test(ElementReference ele) => FullScreenService.ToggleByElement(ele); |
| 107 | + |
| 108 | + public Task TestById(string id) => FullScreenService.ToggleById(id); |
| 109 | + |
| 110 | + public Task Toggle() => FullScreenService.Toggle(); |
| 111 | + } |
| 112 | +} |
0 commit comments