!2384 test(#I4SLOS): add unit test for BarcodeReader

* Merge branch 'main' into test-barcode-reader
* test: 增加 OnDeviceChanged 单元测试
* doc: 更新 OnDeviceChanged 说明文档
* doc: 更新资源文件
* feat: 增加 OnDeviceChanged 回调方法
* test: 增加 AutoStop 单元测试
* refactor: 根据单元测试重构代码
* test: 增加 Close 单元测试
* test: 增加 Start 单元测试
* test: 增加 GetError 单元测试
* test: 增加 GetResult 单元测试
* test: 增加 InitDevices 单元测试
* test: 增加单元测试文件
This commit is contained in:
Argo 2022-02-03 16:27:42 +00:00
parent f28347dc59
commit c06413f1d4
2 changed files with 142 additions and 5 deletions

View File

@ -145,7 +145,7 @@ public partial class BarcodeReader : IAsyncDisposable
/// <returns></returns>
protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender && JSRuntime != null)
if (firstRender)
{
Interop = new JSInterop<BarcodeReader>(JSRuntime);
await Interop.InvokeVoidAsync(this, ScannerElement, "bb_barcode", "init", AutoStart);
@ -225,11 +225,14 @@ public partial class BarcodeReader : IAsyncDisposable
/// <returns></returns>
protected virtual async ValueTask DisposeAsyncCore(bool disposing)
{
if (disposing && Interop != null)
if (disposing)
{
await Interop.InvokeVoidAsync(this, ScannerElement, "bb_barcode", "dispose");
Interop.Dispose();
Interop = null;
if (Interop != null)
{
await Interop.InvokeVoidAsync(this, ScannerElement, "bb_barcode", "dispose");
Interop.Dispose();
Interop = null;
}
}
}

View File

@ -0,0 +1,134 @@
// Copyright (c) Argo Zhang (argo@163.com). All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
// Website: https://www.blazor.zone or https://argozhang.github.io/
namespace UnitTest.Components;
public class BarcodeReaderTest : BootstrapBlazorTestBase
{
[Fact]
public void InitDevices_Ok()
{
var init = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Camera);
pb.Add(a => a.OnInit, items =>
{
init = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.InitDevices(new DeviceItem[]
{
new DeviceItem { DeviceId = "TestId", Label = "Test" }
}));
Assert.True(init);
cut.InvokeAsync(() => cut.Instance.InitDevices(Array.Empty<DeviceItem>()));
}
[Fact]
public void GetResult_Ok()
{
var init = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Camera);
pb.Add(a => a.OnResult, v =>
{
init = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.GetResult("Test"));
Assert.True(init);
}
[Fact]
public void GetError_Ok()
{
var init = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Camera);
pb.Add(a => a.OnError, v =>
{
init = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.GetError("Test"));
Assert.True(init);
}
[Fact]
public void Start_Ok()
{
var init = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Camera);
pb.Add(a => a.OnStart, () =>
{
init = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.Start());
Assert.True(init);
}
[Fact]
public void Close_Ok()
{
var init = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Camera);
pb.Add(a => a.OnClose, () =>
{
init = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.Stop());
Assert.True(init);
}
[Fact]
public void AutoStop_Ok()
{
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.ScanType, ScanType.Image);
pb.Add(a => a.AutoStop, true);
pb.Add(a => a.AutoStart, true);
});
Assert.Contains("data-autostop=\"true\"", cut.Markup);
Assert.Contains("scanner-image", cut.Markup);
}
[Fact]
public void OnDeviceChanged_Ok()
{
var changed = false;
var cut = Context.RenderComponent<BarcodeReader>(pb =>
{
pb.Add(a => a.OnDeviceChanged, item =>
{
changed = true;
return Task.CompletedTask;
});
});
cut.InvokeAsync(() => cut.Instance.InitDevices(new DeviceItem[]
{
new DeviceItem { DeviceId = "TestId", Label = "Test" }
}));
cut.Find(".dropdown-item").Click();
Assert.True(changed);
}
}