Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/increase coverage unit tests #239

Merged
merged 8 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ coverage:
status:
project:
default:
target: 90% # the required coverage value
target: 100% # the required coverage value
threshold: 0.1% # the leniency in hitting the target
patch:
default:
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ And then use it in Razor file ([for example](https://github.com/CropperBlazor/Cr
IsErrorLoadImage="@IsErrorLoadImage"
OnErrorLoadImageEvent="OnErrorLoadImageEvent"
Options="Options"
IsAvaibleInitCropper="IsAvaibleInitCropper">
IsAvailableInitCropper="IsAvailableInitCropper">
</CropperComponent>
```

Expand Down
1 change: 1 addition & 0 deletions src/Cropper.Blazor/Client/Pages/About.razor
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
@page "/about"
@layout LandingLayout

<SeoHeader Title="About"
Overview="About Cropper.Blazor."
Expand Down
2 changes: 1 addition & 1 deletion src/Cropper.Blazor/Client/Pages/CropperDemo.razor
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
IsErrorLoadImage="@IsErrorLoadImage"
OnErrorLoadImageEvent="OnErrorLoadImageEvent"
Options="Options"
IsAvaibleInitCropper="IsAvaibleInitCropper" />
IsAvailableInitCropper="IsAvailableInitCropper" />
</div>
</MudPaper>
</MudItem>
Expand Down
14 changes: 7 additions & 7 deletions src/Cropper.Blazor/Client/Pages/CropperDemo.razor.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Cropper.Blazor.Client.Components;
using System.Reflection;
using System.Text.Json;
using Cropper.Blazor.Client.Components;
using Cropper.Blazor.Client.Enums;
using Cropper.Blazor.Components;
using Cropper.Blazor.Events;
Expand All @@ -14,8 +16,6 @@
using Microsoft.JSInterop;
using MudBlazor;
using MudBlazor.Services;
using System.Reflection;
using System.Text.Json;
using ErrorEventArgs = Microsoft.AspNetCore.Components.Web.ErrorEventArgs;

namespace Cropper.Blazor.Client.Pages
Expand All @@ -38,7 +38,7 @@ public partial class CropperDemo : IDisposable

private string Src = "https://fengyuanchen.github.io/cropperjs/v2/picture.jpg";
private bool IsErrorLoadImage { get; set; } = false;
private bool IsAvaibleInitCropper { get; set; } = true;
private bool IsAvailableInitCropper { get; set; } = true;
private readonly string _errorLoadImageSrc = "not-found-image.jpg";
private Breakpoint Start;
private Guid SubscriptionId;
Expand Down Expand Up @@ -392,7 +392,7 @@ public async Task InputFileChangeAsync(InputFileChangeEventArgs inputFileChangeE

Src = await CropperComponent!.GetImageUsingStreamingAsync(imageFile, imageFile.Size);

IsAvaibleInitCropper = true;
IsAvailableInitCropper = true;
IsErrorLoadImage = false;

CropperComponent?.Destroy();
Expand All @@ -411,12 +411,12 @@ public async Task ReplaceImageAsync(InputFileChangeEventArgs inputFileChangeEven

if (IsErrorLoadImage)
{
IsAvaibleInitCropper = true;
IsAvailableInitCropper = true;
IsErrorLoadImage = false;
}
else
{
IsAvaibleInitCropper = false;
IsAvailableInitCropper = false;
}

CropperComponent?.ReplaceAsync(src);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ public async Task Should_Render_CropperComponent_SuccessfulAsync()
ComponentParameter isErrorLoadImageParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsErrorLoadImage),
false);
ComponentParameter isAvaibleInitCropperParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsAvaibleInitCropper),
ComponentParameter isAvailableInitCropperParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsAvailableInitCropper),
true);
ComponentParameter onLoadImageParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.OnLoadImageEvent),
Expand Down Expand Up @@ -347,7 +347,7 @@ public async Task Should_Render_CropperComponent_SuccessfulAsync()
loadingParameter,
errorLoadImageSrcParameter,
isErrorLoadImageParameter,
isAvaibleInitCropperParameter,
isAvailableInitCropperParameter,
srcParameter,
imageClassParameter,
onLoadImageParameter,
Expand Down Expand Up @@ -597,8 +597,8 @@ public void Should_Not_Render_CropperComponent_With_IsNotAvaibleInitCropper_Para
ComponentParameter isErrorLoadImage = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsErrorLoadImage),
true);
ComponentParameter isAvaibleInitCropperParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsAvaibleInitCropper),
ComponentParameter isAvailableInitCropperParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.IsAvailableInitCropper),
false);

// act
Expand All @@ -608,7 +608,7 @@ public void Should_Not_Render_CropperComponent_With_IsNotAvaibleInitCropper_Para
loadingParameter,
errorLoadImageSrcParameter,
isErrorLoadImage,
isAvaibleInitCropperParameter);
isAvailableInitCropperParameter);

// assert
IElement expectedElement = cropperComponent.Find($"img.{errorLoadImageClass}");
Expand Down Expand Up @@ -650,6 +650,81 @@ public void Verify_Method_To_Be_Invokable_From_JS<T>(string methodName)
attribute.Should().NotBeNull();
}

[Fact]
public async Task Should_Render_CropperComponent_Without_Action_Parameters_SuccessfulAsync()
{
// arrange
CancellationToken cancellationToken = new();
ProgressEventArgs progressEventArgs = new Faker<ProgressEventArgs>()
.Generate();
Options options = new Faker<Options>()
.Generate();
ErrorEventArgs errorEventArgs = new Faker<ErrorEventArgs>()
.Generate();
JSEventData<CropReadyEvent> cropReadyEvent = new Faker<JSEventData<CropReadyEvent>>()
.Generate();
JSEventData<ZoomEvent> zoomEvent = new Faker<JSEventData<ZoomEvent>>()
.Generate();
JSEventData<CropStartEvent> cropStartEvent = new Faker<JSEventData<CropStartEvent>>()
.Generate();
JSEventData<CropMoveEvent> cropMoveEvent = new Faker<JSEventData<CropMoveEvent>>()
.Generate();
JSEventData<CropEndEvent> cropEndEvent = new Faker<JSEventData<CropEndEvent>>()
.Generate();
JSEventData<CropEvent> cropEvent = new Faker<JSEventData<CropEvent>>()
.Generate();

ComponentParameter optionsParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.Options),
options);
ComponentParameter onLoadImageParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.OnLoadImageEvent),
null);
ComponentParameter onErrorLoadImageParameter = ComponentParameter.CreateParameter(
nameof(CropperComponent.OnErrorLoadImageEvent),
null);

// act
IRenderedComponent<CropperComponent> cropperComponent = _testContext
.RenderComponent<CropperComponent>(
optionsParameter,
onLoadImageParameter,
onErrorLoadImageParameter);

// assert
IElement expectedElement = cropperComponent.Find($"img");
ElementReference elementReference = (ElementReference)cropperComponent.Instance
.GetInstanceField("ImageReference");
Guid cropperComponentId = (Guid)cropperComponent.Instance
.GetInstanceField("CropperComponentId");

_mockCropperJsInterop.Verify(c => c.LoadModuleAsync(cancellationToken), Times.Once());
elementReference.Id.Should().NotBeNullOrEmpty();
expectedElement.ClassName.Should().BeNull();
expectedElement.GetAttribute("src").Should().BeNull();
expectedElement.GetAttribute("blazor:elementreference").Should().Be(elementReference.Id);

expectedElement.TriggerEvent("onload", progressEventArgs);
_mockCropperJsInterop.Verify(c => c.InitCropperAsync(
cropperComponentId,
elementReference,
options,
It.IsAny<DotNetObjectReference<ICropperComponentBase>>(),
cancellationToken), Times.Once());

expectedElement.TriggerEvent("onerror", errorEventArgs);

await cropperComponent.InvokeAsync(() =>
{
cropperComponent.Instance.IsReady(cropReadyEvent);
cropperComponent.Instance.CropperIsCroped(cropEvent);
cropperComponent.Instance.CropperIsEnded(cropEndEvent);
cropperComponent.Instance.CropperIsMoved(cropMoveEvent);
cropperComponent.Instance.CropperIsStarted(cropStartEvent);
cropperComponent.Instance.CropperIsZoomed(zoomEvent);
});
}

public void Dispose()
{
_testContext.DisposeComponents();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus;
using System.Collections.Generic;
using Bogus;
using Microsoft.JSInterop;
using Moq;
using Xunit;
Expand All @@ -8,24 +9,37 @@ namespace Cropper.Blazor.UnitTests.Events.CropEndEvent
{
public class CropEndEvent_Should
{
private readonly Mock<IJSObjectReference> _mockIJSObjectReference;
private readonly Event _event;
private static Mock<IJSObjectReference> _mockIJSObjectReference = null!;

public CropEndEvent_Should()
[Theory, MemberData(nameof(TestData_Verify_Dispose))]
public void Verify_Dispose(IJSObjectReference? jSObjectReference, Times times)
{
_mockIJSObjectReference = new Mock<IJSObjectReference>();
_event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, _mockIJSObjectReference.Object);
}
// arrange
Event @event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, jSObjectReference);

[Fact]
public void Verify_Dispose()
{
// act
_event.Dispose();
@event.Dispose();

// assert
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), Times.Once());
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), times);
}

public static IEnumerable<object[]> TestData_Verify_Dispose()
{
yield return WrapArgs(null, Times.Never());

_mockIJSObjectReference = new Mock<IJSObjectReference>();
yield return WrapArgs(_mockIJSObjectReference.Object, Times.Once());

static object[] WrapArgs(
IJSObjectReference? jSObjectReference,
Times times)
=> new object[]
{
jSObjectReference,
times
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus;
using System.Collections.Generic;
using Bogus;
using Microsoft.JSInterop;
using Moq;
using Xunit;
Expand All @@ -8,24 +9,37 @@ namespace Cropper.Blazor.UnitTests.Events.CropMoveEvent
{
public class CropMoveEvent_Should
{
private readonly Mock<IJSObjectReference> _mockIJSObjectReference;
private readonly Event _event;
private static Mock<IJSObjectReference> _mockIJSObjectReference = null!;

public CropMoveEvent_Should()
[Theory, MemberData(nameof(TestData_Verify_Dispose))]
public void Verify_Dispose(IJSObjectReference? jSObjectReference, Times times)
{
_mockIJSObjectReference = new Mock<IJSObjectReference>();
_event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, _mockIJSObjectReference.Object);
}
// arrange
Event @event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, jSObjectReference);

[Fact]
public void Verify_Dispose()
{
// act
_event.Dispose();
@event.Dispose();

// assert
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), Times.Once());
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), times);
}

public static IEnumerable<object[]> TestData_Verify_Dispose()
{
yield return WrapArgs(null, Times.Never());

_mockIJSObjectReference = new Mock<IJSObjectReference>();
yield return WrapArgs(_mockIJSObjectReference.Object, Times.Once());

static object[] WrapArgs(
IJSObjectReference? jSObjectReference,
Times times)
=> new object[]
{
jSObjectReference,
times
};
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus;
using System.Collections.Generic;
using Bogus;
using Microsoft.JSInterop;
using Moq;
using Xunit;
Expand All @@ -8,24 +9,37 @@ namespace Cropper.Blazor.UnitTests.Events.CropStartEvent
{
public class CropStartEvent_Should
{
private readonly Mock<IJSObjectReference> _mockIJSObjectReference;
private readonly Event _event;
private static Mock<IJSObjectReference> _mockIJSObjectReference = null!;

public CropStartEvent_Should()
[Theory, MemberData(nameof(TestData_Verify_Dispose))]
public void Verify_Dispose(IJSObjectReference? jSObjectReference, Times times)
{
_mockIJSObjectReference = new Mock<IJSObjectReference>();
_event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, _mockIJSObjectReference.Object);
}
// arrange
Event @event = new Faker<Event>()
.RuleFor(x => x.OriginalEvent, jSObjectReference);

[Fact]
public void Verify_Dispose()
{
// act
_event.Dispose();
@event.Dispose();

// assert
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), Times.Once());
_mockIJSObjectReference.Verify(c => c.DisposeAsync(), times);
}

public static IEnumerable<object[]> TestData_Verify_Dispose()
{
yield return WrapArgs(null, Times.Never());

_mockIJSObjectReference = new Mock<IJSObjectReference>();
yield return WrapArgs(_mockIJSObjectReference.Object, Times.Once());

static object[] WrapArgs(
IJSObjectReference? jSObjectReference,
Times times)
=> new object[]
{
jSObjectReference,
times
};
}
}
}
Loading