test(ui/templates): cover drag-template-to-root via bUnit DragEventArgs

This commit is contained in:
Joseph Doherty
2026-05-11 12:00:07 -04:00
parent 8155dbc411
commit 17e690f6ef
2 changed files with 39 additions and 1 deletions

View File

@@ -144,7 +144,8 @@
</div> </div>
<div style="max-height: calc(100vh - 160px); overflow-y: auto;"> <div style="max-height: calc(100vh - 160px); overflow-y: auto;">
<div @ondragover:preventDefault="true" @ondrop="OnDropOnRoot" <div class="templates-root-dropzone"
@ondragover:preventDefault="true" @ondrop="OnDropOnRoot"
style="min-height: 100%; padding: 4px;"> style="min-height: 100%; padding: 4px;">
<TreeView @ref="_tree" TItem="TmplNode" Items="_treeRoots" <TreeView @ref="_tree" TItem="TmplNode" Items="_treeRoots"
ChildrenSelector="n => n.Children" ChildrenSelector="n => n.Children"

View File

@@ -1,6 +1,7 @@
using System.Security.Claims; using System.Security.Claims;
using Bunit; using Bunit;
using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Authorization;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NSubstitute; using NSubstitute;
using ScadaLink.Commons.Entities.Templates; using ScadaLink.Commons.Entities.Templates;
@@ -118,6 +119,42 @@ public class TemplatesPageTests : BunitContext
Assert.Contains("DelmiaReceiver", cut.Markup); Assert.Contains("DelmiaReceiver", cut.Markup);
Assert.Contains("→", cut.Markup); Assert.Contains("→", cut.Markup);
} }
[Fact]
public async Task DragTemplateOntoRoot_CallsMoveTemplateAsync_WithNullFolderId()
{
// Arrange: one template currently parented to a folder; the test simulates
// the user dragging that template onto the root drop-zone, which should
// result in TemplateService.MoveTemplateAsync(..., newFolderId: null) being
// invoked. We keep the template at the root in the rendered tree (FolderId
// null) so it renders without needing an expand-click; the drag payload only
// cares about the in-memory id captured by OnDragStart, not the visual
// parent.
var template = new Template("RootDragTarget") { Id = 5, FolderId = null };
_repo.GetAllTemplatesAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<Template>>(new List<Template> { template }));
_repo.GetAllFoldersAsync(Arg.Any<CancellationToken>())
.Returns(Task.FromResult<IReadOnlyList<TemplateFolder>>(new List<TemplateFolder>()));
_repo.GetTemplateByIdAsync(5, Arg.Any<CancellationToken>())
.Returns(Task.FromResult<Template?>(template));
var cut = Render<TemplatesPage>();
// Act: fire ondragstart on the template's draggable <div> (RenderNodeLabel
// emits draggable="true" for template/folder nodes), then ondrop on the
// root wrapper marked with the test-affordance class added to the page.
var templateNode = cut.Find("div[draggable='true']");
await templateNode.TriggerEventAsync("ondragstart", new DragEventArgs());
var rootDropZone = cut.Find("div.templates-root-dropzone");
await rootDropZone.TriggerEventAsync("ondrop", new DragEventArgs());
// Assert: TemplateService.MoveTemplateAsync delegates to the repository's
// UpdateTemplateAsync with FolderId set to null.
await _repo.Received(1).UpdateTemplateAsync(
Arg.Is<Template>(t => t.Id == 5 && t.FolderId == null),
Arg.Any<CancellationToken>());
}
} }
internal sealed class TestAuthStateProvider : AuthenticationStateProvider internal sealed class TestAuthStateProvider : AuthenticationStateProvider