Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RXJS Observables and how to mock them #5543

Open
claridgicus opened this issue Oct 21, 2023 · 0 comments
Open

RXJS Observables and how to mock them #5543

claridgicus opened this issue Oct 21, 2023 · 0 comments

Comments

@claridgicus
Copy link

claridgicus commented Oct 21, 2023

Subject

A simple example for implementing an RXJS observable

Description

Super common use case in Angular is a component that subscribes to an observable from a service to react to changes in the applications state, could that be documented in the angular examples please as the current documentation is pretty limited to the most basic examples.

Heres an example from my own testing suite.
In my current code example below, the first value is emitted by the mock and the observable in the component correctly reflects the state.

describe('FilterOpenComponent', () => {
	let config: any
	let activeFacetSubject: ReplaySubject<any> 
	beforeEach(() => {
		activeFacetSubject = new ReplaySubject<string[]>(1) 
		const mock: MountConfig<FilterOpenComponent> = {
			imports: [],
			providers: [
				{
					provide: UtilityService,
					useValue: {
						activeFacetObservable: activeFacetSubject.asObservable(), 
					},
				},
				SessionStorageService,
			],
			declarations: [FilterOpenComponent],
		}
		config = mock
	})

	it('should update active filter count when UtilityService emits new data', () => {
		cy.mount(FilterOpenComponent, config)
		activeFacetSubject.next(['test'])
		cy.get('.filter-open .count').should('contain', '1')
	})
})

But if I were to chain them it would not work, so my implementation is clearly not up to scratch

	it('should update active filter count when UtilityService emits new data', () => {
		cy.mount(FilterOpenComponent, config)
		activeFacetSubject.next(['test'])
		cy.get('.filter-open .count').should('contain', '1')
		activeFacetSubject.next(['test','reactivity'])
		cy.get('.filter-open .count').should('contain', '2')
	})
	
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant