-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: possibility to override global services in Angular component tes…
…ts (#24394) * fix: allow to override global services in Angular component tests * move DI tests into their own context * exclude `componentProviders` when a component is mounted with a template * - revert and remove `componentProviders` - run `eslint --fix` on `mount.cy.ts` - add tests that use `TestBed.inject` Co-authored-by: Zachary Williams <ZachJW34@gmail.com>
- Loading branch information
1 parent
58e0ab9
commit 54d2853
Showing
5 changed files
with
285 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
system-tests/project-fixtures/angular/src/app/components/cart.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Component, Injectable } from '@angular/core' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class Cart { | ||
#items: string[] = [] | ||
|
||
add (product: string) { | ||
this.#items.push(product) | ||
} | ||
|
||
getItems () { | ||
return this.#items | ||
} | ||
} | ||
|
||
@Component({ | ||
template: `<div><h2>Great Product</h2><button (click)="buy()" data-testid="btn-buy">Buy</button></div>`, | ||
}) | ||
export class ProductComponent { | ||
constructor (private cart: Cart) { | ||
} | ||
|
||
buy () { | ||
this.cart.add('Great Product') | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
system-tests/project-fixtures/angular/src/app/components/component-provider.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Component, Injectable } from '@angular/core' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class MessageService { | ||
get message () { | ||
return 'globally provided service' | ||
} | ||
} | ||
|
||
@Component({ | ||
template: `<p>{{messageService.message}}</p>`, | ||
providers: [{ | ||
provide: MessageService, | ||
useValue: { message: 'component provided service' }, | ||
}], | ||
}) | ||
export class ComponentProviderComponent { | ||
constructor (public messageService: MessageService) { | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
system-tests/project-fixtures/angular/src/app/components/transient-services.component.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Component, Injectable } from '@angular/core' | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
export class TransientService { | ||
get message () { | ||
return 'Original Transient Service' | ||
} | ||
} | ||
|
||
@Injectable({ providedIn: 'root' }) | ||
class DirectService { | ||
constructor (private transientService: TransientService) { | ||
} | ||
|
||
get message () { | ||
return this.transientService.message | ||
} | ||
} | ||
|
||
@Component({ | ||
template: `<p>{{directService.message}}</p>`, | ||
}) | ||
export class TransientServicesComponent { | ||
constructor (public directService: DirectService) {} | ||
} |
Oops, something went wrong.