Skip to content

Commit f5a695e

Browse files
committed
test: update tests to use signals
1 parent 93ab1aa commit f5a695e

File tree

9 files changed

+88
-222
lines changed

9 files changed

+88
-222
lines changed

.github/instructions/copilot.instructions.md

Lines changed: 0 additions & 134 deletions
This file was deleted.

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
node_modules
33
dist
44
coverage
5-
*.xml
5+
*.xml
6+
*/update_angular.sh

ider/src/ider.component.spec.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ describe('IderComponent', () => {
2020
}).compileComponents()
2121
fixture = TestBed.createComponent(IDERComponent)
2222
component = fixture.componentInstance
23+
24+
// Initialize with deviceConnection = false to avoid triggering effect during setup
25+
fixture.componentRef.setInput('deviceConnection', false)
2326
fixture.detectChanges()
2427
})
2528

@@ -51,15 +54,27 @@ describe('IderComponent', () => {
5154
expect(component.deviceStatus.emit).toHaveBeenCalledWith(1)
5255
})
5356

54-
it('should call instantiate when deviceConnection emits true', () => {
55-
spyOn(component, 'instantiate')
56-
component.deviceConnection().emit(true)
57-
expect(component.instantiate).toHaveBeenCalled()
57+
it('should call init when deviceConnection is true', () => {
58+
spyOn(component, 'init')
59+
fixture.componentRef.setInput('deviceConnection', true)
60+
fixture.detectChanges()
61+
expect(component.init).toHaveBeenCalled()
5862
})
5963

60-
it('should call stopIder when deviceConnection emits false', () => {
64+
it('should call stopIder when deviceConnection is false', () => {
6165
spyOn(component, 'stopIder')
62-
component.deviceConnection().emit(false)
66+
67+
// First set deviceConnection to true to initialize
68+
fixture.componentRef.setInput('deviceConnection', true)
69+
fixture.detectChanges()
70+
71+
// Set up a mock redirector so the effect will call stopIder
72+
component.redirector = {} as any
73+
74+
// Then set deviceConnection to false
75+
fixture.componentRef.setInput('deviceConnection', false)
76+
fixture.detectChanges()
77+
6378
expect(component.stopIder).toHaveBeenCalled()
6479
})
6580

ider/src/ider.component.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ export class IDERComponent implements OnDestroy {
4545
})
4646
}
4747

48-
private init(): void {
48+
init(): void {
4949
this.instantiate()
5050
setTimeout(() => {
5151
this.startIder()
5252
}, 4000)
5353
}
5454

55-
private startIder(): void {
55+
startIder(): void {
5656
if (this.redirector != null) {
5757
this.redirector.start(WebSocket)
5858
}
5959
}
6060

61-
private instantiate(): void {
61+
instantiate(): void {
6262
const config: RedirectorConfig = {
6363
mode: 'ider',
6464
protocol: Protocol.IDER,
@@ -80,7 +80,7 @@ export class IDERComponent implements OnDestroy {
8080
this.ider.sectorStats = this.iderSectorStats.bind(this)
8181
}
8282

83-
private iderSectorStats(mode: number, dev: number, total: number, start: number, len: number): void {
83+
iderSectorStats(mode: number, dev: number, total: number, start: number, len: number): void {
8484
if (this.ider == null) {
8585
return
8686
}
@@ -109,18 +109,18 @@ export class IDERComponent implements OnDestroy {
109109
})
110110
}
111111

112-
private onConnectionStateChange = (redirector: any, state: number): any => {
112+
onConnectionStateChange = (redirector: any, state: number): any => {
113113
this.deviceStatus.emit(state)
114114
}
115115

116-
private stopIder(): void {
117-
if (this.redirector !== null) {
116+
stopIder(): void {
117+
if (this.redirector != null) {
118118
this.redirector.stop()
119119
this.cleanup()
120120
}
121121
}
122122

123-
private cleanup(): void {
123+
cleanup(): void {
124124
this.redirector = null
125125
this.ider = null
126126
}

kvm/src/kvm.component.spec.ts

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,28 @@ describe('KvmComponent', () => {
2727
const setup = (): void => {
2828
fixture = TestBed.createComponent(KVMComponent)
2929
component = fixture.componentInstance
30-
// Set initial inputs via setInput
30+
// Set initial inputs via setInput to prevent effect from triggering during setup
31+
fixture.componentRef.setInput('deviceConnection', false)
3132
fixture.componentRef.setInput('mpsServer', '')
32-
fixture.componentRef.setInput('authToken', '')
33+
fixture.componentRef.setInput('authToken', 'authToken')
3334
fixture.componentRef.setInput('deviceId', '')
3435
fixture.detectChanges()
36+
37+
// Now enable connection to trigger init
38+
fixture.componentRef.setInput('deviceConnection', true)
39+
fixture.detectChanges()
3540
}
3641

3742
const asyncSetup = fakeAsync(() => {
3843
fixture = TestBed.createComponent(KVMComponent)
3944
component = fixture.componentInstance
45+
fixture.componentRef.setInput('deviceConnection', false)
4046
fixture.componentRef.setInput('mpsServer', 'wss://localhost')
4147
fixture.componentRef.setInput('authToken', 'authToken')
42-
fixture.componentRef.setInput('deviceId', '')
48+
fixture.detectChanges()
49+
50+
fixture.componentRef.setInput('deviceConnection', true) // Enable connection to trigger init
51+
fixture.detectChanges()
4352
tick(4500)
4453
fixture.detectChanges()
4554
flush()
@@ -53,17 +62,17 @@ describe('KvmComponent', () => {
5362
expect(component.mouseHelper).toBeInstanceOf(MouseHelper)
5463
expect(component.keyboardHelper).toBeInstanceOf(KeyBoardHelper)
5564
expect(component.dataProcessor).toBeInstanceOf(DataProcessor)
56-
expect(component.selected).toEqual(1)
65+
expect(component.selected()).toEqual(1)
5766
expect(component.encodings.length).toEqual(2)
5867
expect(component.mpsServer()).toBe('')
5968
expect(component.deviceId()).toBe('')
60-
expect(component.authToken()).toBe('')
69+
expect(component.authToken()).toBe('authToken')
6170
})
6271

6372
it('should autoconnect on pageload', () => {
6473
asyncSetup()
6574
spyOn<any>(component.redirector, 'start')
66-
spyOn(component.keyboardHelper, 'GrabKeyInput')
75+
spyOn(component.keyboardHelper!, 'GrabKeyInput')
6776
expect(component.redirector).not.toBeNull()
6877
expect(component.mpsServer()).toEqual('wss://localhost')
6978
expect(component.authToken()).toEqual('authToken')
@@ -72,22 +81,30 @@ describe('KvmComponent', () => {
7281
it('should reset all the objects once kvm is stopped', () => {
7382
setup()
7483
spyOn<any>(component.redirector, 'stop')
75-
spyOn(component.keyboardHelper, 'UnGrabKeyInput')
84+
spyOn(component.keyboardHelper!, 'UnGrabKeyInput')
7685
const resetSpy = spyOn(component, 'reset')
7786
component.stopKvm()
7887
expect(component.redirector?.stop).toHaveBeenCalled()
79-
expect(component.keyboardHelper.UnGrabKeyInput).toHaveBeenCalled()
88+
expect(component.keyboardHelper!.UnGrabKeyInput).toHaveBeenCalled()
8089
expect(resetSpy).toHaveBeenCalled()
8190
})
8291

8392
it('should disconnect the active KVM session if there is an encoding change', fakeAsync(() => {
8493
setup()
8594
const stopKvmSpy = spyOn(component, 'stopKvm')
8695
const autoConnectSpy = spyOn(component, 'autoConnect')
87-
component.selectedEncoding().emit(1)
88-
tick(1100)
96+
97+
// First set deviceConnection to true and ensure component is connected
98+
fixture.componentRef.setInput('deviceConnection', true)
8999
fixture.detectChanges()
90-
expect(component.selected).toEqual(1)
100+
tick()
101+
102+
// Then change the encoding
103+
fixture.componentRef.setInput('selectedEncoding', 2) // Change from default 1 to 2
104+
fixture.detectChanges()
105+
tick(1100) // Wait for the timer in onEncodingChange
106+
107+
expect(component.selected()).toEqual(2)
91108
expect(stopKvmSpy).toHaveBeenCalled()
92109
expect(autoConnectSpy).toHaveBeenCalled()
93110
flush()
@@ -103,9 +120,9 @@ describe('KvmComponent', () => {
103120

104121
it('should trigger the core components method on mouse interactions', () => {
105122
setup()
106-
spyOn(component.mouseHelper, 'mousedown')
107-
spyOn(component.mouseHelper, 'mouseup')
108-
spyOn(component.mouseHelper, 'mousemove')
123+
spyOn(component.mouseHelper!, 'mousedown')
124+
spyOn(component.mouseHelper!, 'mouseup')
125+
spyOn(component.mouseHelper!, 'mousemove')
109126

110127
const event: any = {
111128
button: 1,
@@ -114,14 +131,14 @@ describe('KvmComponent', () => {
114131
}
115132
component.onMousedown(event as MouseEvent)
116133
expect(component.mouseHelper).not.toBeNull()
117-
expect(component.mouseHelper.mousedown).toHaveBeenCalled()
134+
expect(component.mouseHelper!.mousedown).toHaveBeenCalled()
118135

119136
component.onMouseup(event as MouseEvent)
120137
expect(component.mouseHelper).not.toBeNull()
121-
expect(component.mouseHelper.mouseup).toHaveBeenCalled()
138+
expect(component.mouseHelper!.mouseup).toHaveBeenCalled()
122139

123140
component.onMousemove(event as MouseEvent)
124141
expect(component.mouseHelper).not.toBeNull()
125-
expect(component.mouseHelper.mousemove).toHaveBeenCalled()
142+
expect(component.mouseHelper!.mousemove).toHaveBeenCalled()
126143
})
127144
})

0 commit comments

Comments
 (0)