Skip to content

Commit 35f2c6e

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

File tree

6 files changed

+85
-46
lines changed

6 files changed

+85
-46
lines changed

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)
99+
fixture.detectChanges()
100+
tick()
101+
102+
// Then change the encoding
103+
fixture.componentRef.setInput('selectedEncoding', 2) // Change from default 1 to 2
89104
fixture.detectChanges()
90-
expect(component.selected).toEqual(1)
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
})

kvm/src/kvm.component.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ export class KVMComponent implements OnDestroy {
6464
readonly selectedEncoding = input<number>(1)
6565

6666
// Component state
67-
private module: AMTDesktop | null = null
68-
private redirector: AMTKvmDataRedirector | null = null
69-
private dataProcessor: IDataProcessor | null = null
70-
private mouseHelper: MouseHelper | null = null
71-
private keyboardHelper: KeyBoardHelper | null = null
72-
private selected = signal(1)
67+
module: AMTDesktop | null = null
68+
redirector: AMTKvmDataRedirector | null = null
69+
dataProcessor: IDataProcessor | null = null
70+
mouseHelper: MouseHelper | null = null
71+
keyboardHelper: KeyBoardHelper | null = null
72+
selected = signal(1)
7373
private mouseMove$?: Subscription
7474

7575
readonly encodings: EncodingOption[] = [
@@ -122,7 +122,7 @@ export class KVMComponent implements OnDestroy {
122122
this.mouseHelper?.resetOffsets()
123123
}
124124

125-
private instantiate(): void {
125+
instantiate(): void {
126126
const canvas = this.canvas()
127127
this.context = canvas?.nativeElement.getContext('2d') || null
128128
const config: RedirectorConfig = {
@@ -164,18 +164,18 @@ export class KVMComponent implements OnDestroy {
164164
this.deviceStatus.emit(state)
165165
}
166166

167-
private onRedirectorError(): void {
167+
onRedirectorError(): void {
168168
this.reset()
169169
}
170170

171-
private init(): void {
171+
init(): void {
172172
this.instantiate()
173173
setTimeout(() => {
174174
this.autoConnect()
175175
}, 4000)
176176
}
177177

178-
private autoConnect(): void {
178+
autoConnect(): void {
179179
if (this.redirector) {
180180
this.redirector.start(WebSocket)
181181
this.keyboardHelper?.GrabKeyInput()
@@ -189,7 +189,7 @@ export class KVMComponent implements OnDestroy {
189189
})
190190
}
191191

192-
private reset(): void {
192+
reset(): void {
193193
this.redirector = null
194194
this.module = null
195195
this.dataProcessor = null
@@ -198,7 +198,7 @@ export class KVMComponent implements OnDestroy {
198198
this.instantiate()
199199
}
200200

201-
private stopKvm(): void {
201+
stopKvm(): void {
202202
this.redirector?.stop()
203203
this.keyboardHelper?.UnGrabKeyInput()
204204
this.reset()

sol/src/sol.component.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,16 @@ describe('SolComponent', () => {
2525
const setup = (): void => {
2626
fixture = TestBed.createComponent(SOLComponent)
2727
component = fixture.componentInstance
28+
// Set deviceConnection to false first to prevent immediate effect trigger
29+
fixture.componentRef.setInput('deviceConnection', false)
2830
fixture.componentRef.setInput('mpsServer', '')
2931
fixture.componentRef.setInput('authToken', '')
3032
fixture.componentRef.setInput('deviceId', '')
3133
fixture.detectChanges()
34+
35+
// Now enable connection to trigger init
36+
fixture.componentRef.setInput('deviceConnection', true)
37+
fixture.detectChanges()
3238
}
3339

3440
const asyncSetup = fakeAsync(() => {
@@ -37,6 +43,7 @@ describe('SolComponent', () => {
3743
fixture.componentRef.setInput('mpsServer', 'wss://localhost')
3844
fixture.componentRef.setInput('authToken', 'authToken')
3945
fixture.componentRef.setInput('deviceId', '')
46+
fixture.componentRef.setInput('deviceConnection', true) // Enable connection to trigger init
4047
tick(4500)
4148
fixture.detectChanges()
4249
flush()

sol/src/sol.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,14 @@ export class SOLComponent implements OnDestroy, AfterViewInit {
6565
this.init()
6666
}
6767

68-
private init(): void {
68+
init(): void {
6969
this.instantiate()
7070
setTimeout(() => {
7171
this.startSol()
7272
}, 4000)
7373
}
7474

75-
private instantiate(): void {
75+
instantiate(): void {
7676
this.terminal = new AmtTerminal()
7777
this.dataProcessor = new TerminalDataProcessor(this.terminal)
7878
const config: RedirectorConfig = {
@@ -127,7 +127,7 @@ export class SOLComponent implements OnDestroy, AfterViewInit {
127127
}
128128

129129
stopSol(): void {
130-
if (this.redirector !== null) {
130+
if (this.redirector != null) {
131131
this.redirector.stop()
132132
this.handleClearTerminal()
133133
this.term?.dispose()

0 commit comments

Comments
 (0)