Skip to content

Commit

Permalink
feat(tests), chore(docs): added props for the component
Browse files Browse the repository at this point in the history
  • Loading branch information
ljbc1994 committed Jun 4, 2019
1 parent 2af8dd4 commit 9cf1223
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 18 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![Package Version](https://img.shields.io/nuget/v/BlazorIntersectionObserver.svg)](https://www.nuget.org/packages/BlazorIntersectionObserver)
[![NuGet Downloads](https://img.shields.io/nuget/dt/BlazorIntersectionObserver.svg)](https://www.nuget.org/packages/BlazorIntersectionObserver)
[![License](https://img.shields.io/github/license/ljbc1994/BlazorIntersectionObserver.svg)](https://github.com/ljbc1994/BlazorIntersectionObserver/blob/master/LICENCE)
[![Build Status](https://dev.azure.com/ljbc94/BlazorIntersectionObserver/_apis/build/status/ljbc1994.BlazorIntersectionObserver?branchName=master)](https://dev.azure.com/ljbc94/BlazorIntersectionObserver/_build/latest?definitionId=1&branchName=master)

> A comprehensive wrapper around the Intersection Observer API, giving you all the goodness of observing intersections in a performant way.
Expand All @@ -18,6 +19,8 @@ Install `BlazorIntersectionObserver` through NuGet.
> dotnet add package BlazorIntersectionObserver
```

*OR*

### 2. Register the service

Now you'll need to add the service to the service configuration.
Expand Down Expand Up @@ -203,6 +206,16 @@ Rather than directly interfacing with the service, you can use this convenience
@* Component code... *@
```

##### Props

- `OnChange` (`EventCallback<IntersectionObserverEntry>`) - When the intersection observer has a entry update.
- `IsIntersecting` (`bool`) - Whether the element is intersecting - used for two-way binding.
- `Options` (`IntersectionObserverOptions`) - The options for the observer.
- `Once` (`bool`) - Only observe once for an intersection, then the instance disposes of itself.
- `Style` (`string`) - The style for the element.
- `Class` (`string`) - The class for the element.


## Implementation Detail
To avoid creating an unnecessary number of observers for every element being observed, if a `Blazor Observer` shares exactly the same options as another, they will both use the same `IntersectionObserver` instance in JS. As each `Blazor Observer` has a unique id and callback, the elements that are being observed will still be passed to their respective `Blazor Observer`.

Expand Down
2 changes: 1 addition & 1 deletion azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ steps:
inputs:
command: test
projects: '**/*Test/*.csproj'
arguments: '--configuration $(buildConfiguration) --collect "Code coverage"'
arguments: '--configuration $(buildConfiguration) --collect "Code coverage" --settings codecoverage.runsettings'

- task: DotNetCoreCLI@2
inputs:
Expand Down
19 changes: 19 additions & 0 deletions codecoverage.runsettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="utf-8"?>
<RunSettings>
<DataCollectionRunSettings>
<DataCollectors>
<DataCollector friendlyName="Code Coverage" uri="datacollector://Microsoft/CodeCoverage/2.0" assemblyQualifiedName="Microsoft.VisualStudio.Coverage.DynamicCoverageDataCollector, Microsoft.VisualStudio.TraceCollector, Culture=neutral">
<Configuration>
<CodeCoverage>
<ModulePaths>
<Exclude>
<ModulePath>.*.testadapter.dll</ModulePath>
<ModulePath>.*.test.dll</ModulePath>
</Exclude>
</ModulePaths>
</CodeCoverage>
</Configuration>
</DataCollector>
</DataCollectors>
</DataCollectionRunSettings>
</RunSettings>
17 changes: 17 additions & 0 deletions test/Blazor.IntersectionObserver.Test/Configuration/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace Blazor.IntersectionObserver.Test.Configuration
{
internal static class Constants
{
public const string NAMESPACE = "BlazorIntersectionObserverJS";

public static string CREATE = $"{NAMESPACE}.create";

public static string OBSERVE = $"{NAMESPACE}.observe";

public static string UNOBSERVE = $"{NAMESPACE}.unobserve";

public static string DISCONNECT = $"{NAMESPACE}.disconnect";

public static string OBSERVE_ELEMENT = $"{NAMESPACE}.observeElement";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using System.Linq;
using Microsoft.AspNetCore.Components;
using Blazor.IntersectionObserver.Test.Configuration;

namespace Tests
{
Expand All @@ -32,7 +33,7 @@ public async Task CreateAnObserverAndNotifyJSInterop()

mockJsRuntime
.Verify(v => v.InvokeAsync<object>(
"BlazorIntersectionObserverJS.create",
Constants.CREATE,
It.IsAny<DotNetObjectRef>(),
It.IsAny<string>(),
testOptions
Expand All @@ -49,6 +50,7 @@ public async Task CreateAnObserverAndObserveMultipleElements()
RootMargin = "10px 10px 10px 10px"
};
var testElement = new ElementRef();
var otherTestElement = new ElementRef();

var observerService = new IntersectionObserverService(mockJsRuntime.Object);
var observer = await observerService.Create(
Expand All @@ -59,21 +61,22 @@ public async Task CreateAnObserverAndObserveMultipleElements()
var observerId = observer.Id;

observer.Observe(testElement);
observer.Observe(otherTestElement);

mockJsRuntime
.Verify(v => v.InvokeAsync<object>(
"BlazorIntersectionObserverJS.create",
Constants.CREATE,
It.IsAny<DotNetObjectRef>(),
It.IsAny<string>(),
testOptions
), Times.Once());

mockJsRuntime
.Verify(v => v.InvokeAsync<object>(
"BlazorIntersectionObserverJS.observeElement",
Constants.OBSERVE_ELEMENT,
observerId,
testElement
), Times.Once());
), Times.Exactly(2));
}

[Test]
Expand All @@ -100,7 +103,7 @@ public async Task CreateMultipleObserversAndNotifyJSInterop()

mockJsRuntime
.Verify(v => v.InvokeAsync<object>(
"BlazorIntersectionObserverJS.create",
Constants.CREATE,
It.IsAny<DotNetObjectRef>(),
It.IsAny<string>(),
It.IsAny<IntersectionObserverOptions>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Blazor.IntersectionObserver.Test.Configuration;

namespace Tests
{
Expand All @@ -17,28 +18,29 @@ public class IntersectionObserverService_ObserveShould
public async Task ObserveAnElementAndNotifyJSInterop()
{
var mockJsRuntime = new Mock<IJSRuntime>();
var mockOptions = new IntersectionObserverOptions
var testOptions = new IntersectionObserverOptions
{
RootMargin = "10px 10px 10px 10px",
Threshold = new List<double> { 0.25, 0.5, 1 }
};
var testElementRef = new ElementRef();

var observerService = new IntersectionObserverService(mockJsRuntime.Object);
var observer = await observerService.Observe(
It.IsAny<ElementRef>(),
testElementRef,
It.IsAny<Action<IList<IntersectionObserverEntry>>>(),
mockOptions
testOptions
);

var observerId = observer.Id;

mockJsRuntime
.Verify(v => v.InvokeAsync<object>(
"BlazorIntersectionObserverJS.observe",
Constants.OBSERVE,
It.IsAny<DotNetObjectRef>(),
observerId,
It.IsAny<ElementRef>(),
mockOptions
testElementRef,
testOptions
), Times.Once());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;
using Blazor.IntersectionObserver.Test.Configuration;

namespace Tests
{
Expand All @@ -18,15 +19,24 @@ public async Task UnobserveAnElementAndNotifyJSInterop()
{
var mockJsRuntime = new Mock<IJSRuntime>();

mockJsRuntime
.Setup(x => x.InvokeAsync<bool>(
Constants.UNOBSERVE,
It.IsAny<string>(),
It.IsAny<ElementRef>()
))
.ReturnsAsync(true);

var mockOptions = new IntersectionObserverOptions
{
RootMargin = "10px 10px 10px 10px",
Threshold = new List<double> { 0.25, 0.5, 1 }
};
var testElementRef = new ElementRef();

var observerService = new IntersectionObserverService(mockJsRuntime.Object);
var observer = await observerService.Observe(
It.IsAny<ElementRef>(),
testElementRef,
It.IsAny<Action<IList<IntersectionObserverEntry>>>(),
mockOptions
);
Expand All @@ -37,9 +47,9 @@ public async Task UnobserveAnElementAndNotifyJSInterop()

mockJsRuntime
.Verify(v => v.InvokeAsync<bool>(
"BlazorIntersectionObserverJS.unobserve",
Constants.UNOBSERVE,
observerId,
It.IsAny<ElementRef>()
testElementRef
), Times.Once());
}

Expand All @@ -48,6 +58,13 @@ public async Task DisconnectAnObserverCallbackAndNotifyJSInterop()
{
var mockJsRuntime = new Mock<IJSRuntime>();

mockJsRuntime
.Setup(x => x.InvokeAsync<bool>(
Constants.DISCONNECT,
It.IsAny<string>()
))
.ReturnsAsync(true);

var mockOptions = new IntersectionObserverOptions();

var observerService = new IntersectionObserverService(mockJsRuntime.Object);
Expand All @@ -63,7 +80,7 @@ public async Task DisconnectAnObserverCallbackAndNotifyJSInterop()

mockJsRuntime
.Verify(v => v.InvokeAsync<bool>(
"BlazorIntersectionObserverJS.disconnect",
Constants.DISCONNECT,
observerId
), Times.Once());
}
Expand All @@ -74,8 +91,17 @@ public async Task FromJSInteropTriggerObserverCallback()
var mockJsRuntime = new Mock<IJSRuntime>();
var mockOnIntersect = new Mock<Action<IList<IntersectionObserverEntry>>>();
var testEntries = new List<IntersectionObserverEntry> {
new IntersectionObserverEntry { IsVisible = true },
new IntersectionObserverEntry { IsIntersecting = true }
new IntersectionObserverEntry {
IsVisible = true
},
new IntersectionObserverEntry {
IsIntersecting = true,
IsVisible = true,
IntersectionRatio = 1.0,
BoundingClientRect = new DOMRectReadOnly(),
RootBounds = new DOMRectReadOnly(),
Target = new ElementRef()
}
};

var mockOptions = new IntersectionObserverOptions();
Expand Down

0 comments on commit 9cf1223

Please sign in to comment.