Skip to content

Commit 459047f

Browse files
committed
convert old rxjs code from angular 18+ to signals as this is the new recommended approach since then
1 parent 0d85a78 commit 459047f

File tree

6 files changed

+31
-63
lines changed

6 files changed

+31
-63
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
import { Component } from '@angular/core'
1+
import { Component, signal } from '@angular/core'
22
import { ChildProvidersService } from './child-providers.service'
3-
import { take } from 'rxjs/operators'
43

54
@Component({
65
selector: 'app-another-child',
76
standalone: false,
8-
template: `<button (click)="handleClick()">{{ message }}</button>`,
7+
template: `<button (click)="handleClick()">{{ message() }}</button>`,
98
providers: [ChildProvidersService],
109
})
1110
export class AnotherChildProvidersComponent {
12-
message = 'default another child message'
11+
message = signal('default another child message')
1312

1413
constructor (private readonly service: ChildProvidersService) {}
1514

16-
handleClick (): void {
17-
this.service.getMessage().pipe(
18-
take(1),
19-
).subscribe((message) => this.message = message)
15+
async handleClick (): Promise<void> {
16+
const message = await this.service.getMessage()
17+
18+
this.message.set(message)
2019
}
2120
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1-
import { Component } from '@angular/core'
1+
import { Component, signal } from '@angular/core'
22
import { ChildProvidersService } from './child-providers.service'
3-
import { take } from 'rxjs/operators'
43

54
@Component({
65
selector: 'app-child-providers',
76
standalone: false,
8-
template: `<button (click)="handleClick()">{{ message }}</button>`,
7+
template: `<button (click)="handleClick()">{{ message() }}</button>`,
98
})
109
export class ChildProvidersComponent {
11-
message = 'default message'
10+
message = signal('default message')
1211

1312
constructor (private readonly service: ChildProvidersService) {}
1413

15-
handleClick (): void {
16-
this.service.getMessage().pipe(
17-
take(1),
18-
).subscribe((message) => this.message = message)
14+
async handleClick (): Promise<void> {
15+
const message = await this.service.getMessage()
16+
17+
this.message.set(message)
1918
}
2019
}
Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
import { Injectable } from '@angular/core'
2-
import { HttpClient } from '@angular/common/http'
3-
import { Observable } from 'rxjs'
4-
import { map } from 'rxjs/operators'
52

63
@Injectable()
74
export class ChildProvidersService {
8-
constructor (private readonly http: HttpClient) {}
5+
async getMessage (): Promise<string> {
6+
const response = await fetch('https://myfakeapiurl.com/api/message')
7+
const data = await response.json()
98

10-
getMessage (): Observable<string> {
11-
return this.http.get<{ message: string }>('https://myfakeapiurl.com/api/message').pipe(
12-
map((response) => response.message),
13-
)
9+
return data.message
1410
}
1511
}
Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,16 @@
1-
import { Component } from '@angular/core'
2-
import { CounterService } from './counter.service'
1+
import { Component, model } from '@angular/core'
32

43
@Component({
54
selector: 'counter-component',
65
standalone: false,
76
template: `<button (click)="increment()">
8-
Increment: {{ count$ | async }}
7+
Increment: {{ count() }}
98
</button>`,
109
})
1110
export class CounterComponent {
12-
count$
13-
14-
constructor (private counterService: CounterService) {
15-
this.count$ = this.counterService.count$
16-
}
11+
count = model<number>(0)
1712

1813
increment () {
19-
this.counterService.increment()
14+
this.count.set(this.count() + 1)
2015
}
2116
}

system-tests/project-fixtures/angular/src/app/components/counter.service.ts

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

system-tests/project-fixtures/angular/src/app/mount.cy.ts

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { ParentChildModule } from './components/parent-child.module'
22
import { ParentComponent } from './components/parent.component'
33
import { CounterComponent } from './components/counter.component'
4-
import { CounterService } from './components/counter.service'
54
import { ChildComponent } from './components/child.component'
65
import { WithDirectivesComponent } from './components/with-directives.component'
76
import { ButtonOutputComponent } from './components/button-output.component'
@@ -10,8 +9,6 @@ import { EventEmitter, Component } from '@angular/core'
109
import { ProjectionComponent } from './components/projection.component'
1110
import { ChildProvidersComponent } from './components/child-providers.component'
1211
import { ParentProvidersComponent } from './components/parent-providers.component'
13-
import { HttpClientModule } from '@angular/common/http'
14-
import { of } from 'rxjs'
1512
import { ChildProvidersService } from './components/child-providers.service'
1613
import { AnotherChildProvidersComponent } from './components/another-child-providers.component'
1714
import { TestBed } from '@angular/core/testing'
@@ -28,7 +25,7 @@ import { UrlImageComponent } from './components/url-image.component'
2825
})
2926
class WrapperComponent {}
3027

31-
// Staring with Angular v19, standalone = true is the new default behavior.
28+
// Starting with Angular v19, standalone = true is the new default behavior.
3229
// This means that the ng module configurations, including test module configurations,
3330
// do not work by default with components. Cypress for non standalone components
3431
// injects the CommonModule by default and allows users to add module declarations.
@@ -58,7 +55,7 @@ describe('angular mount', () => {
5855
})
5956

6057
it('accepts providers', () => {
61-
cy.mount(CounterComponent, { providers: [CounterService] })
58+
cy.mount(CounterComponent)
6259
cy.contains('button', 'Increment: 0').click().contains('Increment: 1')
6360
})
6461

@@ -203,7 +200,6 @@ describe('angular mount', () => {
203200
})
204201

205202
cy.mount(ChildProvidersComponent, {
206-
imports: [HttpClientModule],
207203
providers: [ChildProvidersService],
208204
})
209205

@@ -220,7 +216,6 @@ describe('angular mount', () => {
220216

221217
cy.mount(ParentProvidersComponent, {
222218
declarations: [ChildProvidersComponent, AnotherChildProvidersComponent],
223-
imports: [HttpClientModule],
224219
providers: [ChildProvidersService],
225220
})
226221

@@ -231,13 +226,12 @@ describe('angular mount', () => {
231226
it('can make test doubles for child components', () => {
232227
cy.mount(ParentProvidersComponent, {
233228
declarations: [ChildProvidersComponent, AnotherChildProvidersComponent],
234-
imports: [HttpClientModule],
235229
providers: [
236230
{
237231
provide: ChildProvidersService,
238232
useValue: {
239-
getMessage () {
240-
return of('test')
233+
async getMessage (): Promise<string> {
234+
return 'test'
241235
},
242236
} as ChildProvidersService,
243237
},
@@ -257,7 +251,6 @@ describe('angular mount', () => {
257251
})
258252

259253
cy.mount(AnotherChildProvidersComponent, {
260-
imports: [HttpClientModule],
261254
providers: [ChildProvidersService],
262255
})
263256

@@ -266,12 +259,12 @@ describe('angular mount', () => {
266259
})
267260

268261
it('can use a test double for a component with a provider override', () => {
269-
cy.mount(AnotherChildProvidersComponent, { imports: [HttpClientModule] })
262+
cy.mount(AnotherChildProvidersComponent)
270263
TestBed.overrideComponent(AnotherChildProvidersComponent, { add: { providers: [{
271264
provide: ChildProvidersService,
272265
useValue: {
273-
getMessage () {
274-
return of('test')
266+
async getMessage (): Promise<string> {
267+
return 'test'
275268
},
276269
},
277270
}] } })
@@ -290,7 +283,6 @@ describe('angular mount', () => {
290283

291284
cy.mount(ParentProvidersComponent, {
292285
declarations: [ChildProvidersComponent, AnotherChildProvidersComponent],
293-
imports: [HttpClientModule],
294286
providers: [ChildProvidersService],
295287
})
296288

@@ -301,15 +293,14 @@ describe('angular mount', () => {
301293
it('can use a test double for a child component with a provider override', () => {
302294
TestBed.overrideProvider(ChildProvidersService, {
303295
useValue: {
304-
getMessage () {
305-
return of('test')
296+
async getMessage (): Promise<string> {
297+
return 'test'
306298
},
307299
} as ChildProvidersService,
308300
})
309301

310302
cy.mount(ParentProvidersComponent, {
311303
declarations: [ChildProvidersComponent, AnotherChildProvidersComponent],
312-
imports: [HttpClientModule],
313304
})
314305

315306
cy.get('button').contains('default another child message').click()

0 commit comments

Comments
 (0)