Skip to content

Commit 28e1ca4

Browse files
Copilotkbeaugrand
andcommitted
Fix checkbox state not updating when device is unselected in layer management
Co-authored-by: kbeaugrand <9513635+kbeaugrand@users.noreply.github.com>
1 parent 30ec474 commit 28e1ca4

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

src/IoTHub.Portal.Client/Dialogs/Layer/LinkDeviceLayerDialog.razor

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@
182182
if (DeviceList.Contains(device.DeviceID)) DeviceList.Remove(device.DeviceID);
183183
else DeviceList.Add(device.DeviceID);
184184
}
185+
186+
StateHasChanged();
185187
}
186188

187189
private async Task SaveDevice()

src/IoTHub.Portal.Tests.Unit/Client/Dialogs/Layer/LinkDeviceLayerDialogTest.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,86 @@ public async Task LinkDeviceLayerDialog_Save_UpdatesDevices()
175175
cut.WaitForAssertion(() => MockRepository.VerifyAll());
176176
}
177177

178+
[Test]
179+
public async Task LinkDeviceLayerDialog_UpdateChecked_UnselectingAlreadyRegisteredDevice_ShouldUpdateCheckboxState()
180+
{
181+
// Arrange
182+
var expectedLayerDto = new LayerDto
183+
{
184+
Id = "layer-123",
185+
Name = "Test Layer"
186+
};
187+
188+
var mockDeviceModel = new DeviceModelDto
189+
{
190+
ModelId = Guid.NewGuid().ToString(),
191+
Name = "Test Model"
192+
};
193+
194+
var alreadyRegisteredDevice = new DeviceListItem
195+
{
196+
DeviceID = "device-already-registered",
197+
DeviceName = "Already Registered Device",
198+
IsEnabled = true,
199+
IsConnected = true,
200+
DeviceModelId = mockDeviceModel.ModelId,
201+
Image = "image.png",
202+
StatusUpdatedTime = DateTime.UtcNow,
203+
LastActivityTime = DateTime.UtcNow,
204+
Labels = new List<LabelDto>(),
205+
LayerId = expectedLayerDto.Id // Already registered to this layer
206+
};
207+
208+
_ = this.mockDeviceClientService.Setup(service =>
209+
service.GetDevices($"{this.apiBaseUrl}?pageNumber=0&pageSize=5&searchText="))
210+
.ReturnsAsync(new PaginationResult<DeviceListItem>
211+
{
212+
Items = new[] { alreadyRegisteredDevice },
213+
TotalItems = 1
214+
});
215+
216+
_ = this.mockDeviceModelsClientService.Setup(service => service.GetDeviceModelsAsync(It.IsAny<DeviceModelFilter>()))
217+
.ReturnsAsync(new PaginationResult<DeviceModelDto>
218+
{
219+
Items = new List<DeviceModelDto> { mockDeviceModel }
220+
});
221+
222+
// Act
223+
var cut = RenderComponent<MudDialogProvider>();
224+
var service = Services.GetService<IDialogService>() as DialogService;
225+
226+
var parameters = new DialogParameters
227+
{
228+
{"InitLayer", expectedLayerDto},
229+
{"LayerList", new HashSet<LayerHash>()}
230+
};
231+
232+
_ = await cut.InvokeAsync(() => service?.Show<LinkDeviceLayerDialog>(string.Empty, parameters));
233+
234+
// Wait for the table to render
235+
cut.WaitForState(() => cut.FindAll("table tbody tr").Count == 1);
236+
237+
// Find the checkbox button (should be checked with success color initially)
238+
var checkboxButton = cut.Find("table tbody tr td:last-child button");
239+
240+
// Verify initial state - should be checked (CheckBox icon, success color)
241+
cut.WaitForAssertion(() => checkboxButton.OuterHtml.Should().Contain("CheckBox"));
242+
243+
// Click to unselect
244+
checkboxButton.Click();
245+
246+
// Assert - after clicking, the checkbox should visually update
247+
// The LayerId is set to null, so the condition should no longer match "Already registered"
248+
// and should show either the "Add device" state or intermediate state
249+
cut.WaitForAssertion(() =>
250+
{
251+
var updatedButton = cut.Find("table tbody tr td:last-child button");
252+
// After unselecting, the button should not show the CheckBox icon anymore
253+
// It should show CheckBoxOutlineBlank (unselected state)
254+
updatedButton.OuterHtml.Should().Contain("CheckBoxOutlineBlank");
255+
});
256+
}
257+
178258
[Test]
179259
public async Task LinkDeviceLayerDialog_Save_UpdatesDevicesFromMultiplePages()
180260
{

0 commit comments

Comments
 (0)