-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
42 changed files
with
371 additions
and
1,606 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
BlazorPro.BlazorSize.Bunit/BlazorPro.BlazorSize.Bunit.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="bunit.web" Version="1.1.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\BlazorSize\BlazorPro.BlazorSize.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using BlazorPro.BlazorSize; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Bunit | ||
{ | ||
public static class BunitExtensions | ||
{ | ||
public static FakeMediaQueryService AddBlazorSize(this TestContextBase testContext) | ||
{ | ||
testContext.RenderTree.Add<MediaQueryList>(); | ||
var mediaQueryServices = new FakeMediaQueryService(testContext); | ||
testContext.Services.AddSingleton<IMediaQueryService>(mediaQueryServices); | ||
return mediaQueryServices; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using BlazorPro.BlazorSize; | ||
using Bunit; | ||
using Microsoft.JSInterop; | ||
|
||
namespace Bunit | ||
{ | ||
public class FakeMediaQueryService : IMediaQueryService | ||
{ | ||
private readonly TestContextBase contextBase; | ||
private MediaQueryArgs? activeMediaQuery = null; | ||
|
||
public List<MediaQueryCache> MediaQueries { get; } = new(); | ||
|
||
public FakeMediaQueryService(TestContextBase contextBase) | ||
{ | ||
this.contextBase = contextBase; | ||
} | ||
|
||
public void AddQuery(MediaQuery newMediaQuery) | ||
{ | ||
var mediaQueryCache = GetMediaQueryFromCache(newMediaQuery.Media); | ||
|
||
if (mediaQueryCache is null) | ||
{ | ||
mediaQueryCache = new MediaQueryCache() | ||
{ | ||
MediaRequested = newMediaQuery.Media | ||
}; | ||
MediaQueries.Add(mediaQueryCache); | ||
} | ||
|
||
mediaQueryCache.MediaQueries.Add(newMediaQuery); | ||
|
||
SetActiveMediaQuery(activeMediaQuery); | ||
} | ||
|
||
public Task CreateMediaQueryList(DotNetObjectReference<MediaQueryList> dotNetObjectReference) | ||
=> Task.CompletedTask; | ||
|
||
public Task Initialize(MediaQuery mediaQuery) | ||
=> Task.CompletedTask; | ||
|
||
public Task RemoveQuery(MediaQuery mediaQuery) | ||
{ | ||
var mediaQueryFromCache = GetMediaQueryFromCache(mediaQuery.Media); | ||
if (mediaQueryFromCache is null) | ||
return Task.CompletedTask; | ||
|
||
mediaQueryFromCache.MediaQueries.Remove(mediaQuery); | ||
|
||
if (mediaQueryFromCache.MediaQueries.Count == 0) | ||
MediaQueries.Remove(mediaQueryFromCache); | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public void SetActiveBreakPoint(string breakpoint) | ||
=> SetActiveMediaQuery(new MediaQueryArgs | ||
{ | ||
Matches = true, | ||
Media = breakpoint | ||
}); | ||
|
||
public void SetActiveMediaQuery(MediaQueryArgs args) | ||
{ | ||
if (args is null) | ||
return; | ||
|
||
activeMediaQuery = args; | ||
|
||
// cache must be compared by actual value, not RequestedMedia when invoked from JavaScript | ||
// DOM Media value my be different that the initally requested media query value. | ||
var cache = GetMediaQueryFromCache(args.Media); | ||
|
||
if (cache is null) return; | ||
|
||
// Dispatch events to all subscribers | ||
contextBase.Renderer.Dispatcher.InvokeAsync(() => | ||
{ | ||
foreach (var item in cache.MediaQueries) | ||
{ | ||
item.MediaQueryChanged(args); | ||
} | ||
}); | ||
} | ||
|
||
private MediaQueryCache GetMediaQueryFromCache(string byMedia) | ||
=> MediaQueries?.Find(q => q.MediaRequested == byMedia); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.