// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone

namespace UnitTest.Components;

public class IFrameTest : BootstrapBlazorTestBase
{
    [Fact]
    public async Task Frame_Ok()
    {
        var postData = false;
        var cut = Context.Render<IFrame>(pb =>
        {
            pb.Add(a => a.Src, "/Cat");
            pb.Add(a => a.OnPostDataAsync, v =>
            {
                postData = true;
                return Task.CompletedTask;
            });
        });
        cut.Contains("iframe");

        cut.Render(pb =>
        {
            pb.Add(a => a.Data, new { Rows = new List<string>() { "1", "2" } });
        });

        await cut.InvokeAsync(async () =>
        {
            await cut.Instance.TriggerPostData(new List<string> { "2", "3" });
            Assert.True(postData);
        });

        var loaded = false;
        cut.Render(pb =>
        {
            pb.Add(a => a.OnReadyAsync, () =>
            {
                loaded = true;
                return Task.CompletedTask;
            });
        });

        await cut.InvokeAsync(async () =>
        {
            await cut.Instance.TriggerLoaded();
        });
        Assert.True(loaded);
    }
}
