Skip to content

Commit f78e2d3

Browse files
committed
fix: address copilot review comments
1 parent cca39b4 commit f78e2d3

2 files changed

Lines changed: 39 additions & 12 deletions

File tree

src/app/devices/device-toolbar/device-toolbar.component.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,24 @@ describe('DeviceToolbarComponent', () => {
138138
expect(component.isLoading()()).toBeFalse()
139139
})
140140

141+
it('should fetch power state using non-cached API', () => {
142+
devicesService.getPowerState.calls.reset()
143+
devicesService.getPowerStateCached.calls.reset()
144+
145+
component.getPowerState()
146+
147+
expect(devicesService.getPowerState).toHaveBeenCalledWith('guid')
148+
expect(devicesService.getPowerStateCached).not.toHaveBeenCalled()
149+
})
150+
151+
it('should reset loading state when power state request fails', () => {
152+
devicesService.getPowerState.and.returnValue(throwError(() => new Error('Network error')))
153+
154+
component.getPowerState()
155+
156+
expect(component.isLoading()()).toBeFalse()
157+
})
158+
141159
it('should navigate to device', async () => {
142160
fixture.componentRef.setInput('deviceId', '12345-pokli-456772')
143161
const routerSpy = spyOn(component.router, 'navigate')

src/app/devices/device-toolbar/device-toolbar.component.ts

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -219,20 +219,29 @@ export class DeviceToolbarComponent implements OnInit {
219219

220220
getPowerState(): void {
221221
this.isLoading().set(true)
222-
// Bypass the cache so a manual refresh always fetches the latest state from
223-
// the device rather than returning a previously cached value.
222+
// Bypass the cache so each request fetches the latest state from the
223+
// device rather than returning a previously cached value.
224224
this.devicesService
225225
.getPowerState(this.deviceId())
226-
.pipe(takeUntilDestroyed(this.destroyRef))
227-
.subscribe((powerState) => {
228-
this.powerState.set(
229-
powerState.powerstate.toString() === '2'
230-
? 'deviceToolbar.power.on.value'
231-
: powerState.powerstate.toString() === '3' || powerState.powerstate.toString() === '4'
232-
? 'deviceToolbar.power.sleep.value'
233-
: 'deviceToolbar.power.off.value'
234-
)
235-
this.isLoading().set(false)
226+
.pipe(
227+
finalize(() => {
228+
this.isLoading().set(false)
229+
}),
230+
takeUntilDestroyed(this.destroyRef)
231+
)
232+
.subscribe({
233+
next: (powerState) => {
234+
this.powerState.set(
235+
powerState.powerstate.toString() === '2'
236+
? 'deviceToolbar.power.on.value'
237+
: powerState.powerstate.toString() === '3' || powerState.powerstate.toString() === '4'
238+
? 'deviceToolbar.power.sleep.value'
239+
: 'deviceToolbar.power.off.value'
240+
)
241+
},
242+
error: () => {
243+
// Keep existing error propagation behavior from DevicesService; finalize handles UI state.
244+
}
236245
})
237246
}
238247

0 commit comments

Comments
 (0)