I'm trying to limit the time subscriptions are cached in rxjs. Previously the caching was done with pipe(publishReplay(1), refCount()). After finding this nice answer and reading the docs, I found out that this cache time can be limited by passing a second parameter to publishReplay.
Example: publishReplay(1, 60 * 1000)
I tried to make a minimal example:
<button (click)="test()">Test</button>
JS:
urlofApi = "https://api.github.com/search/repositories?q=helloWorld";
testX = this.http.get(this.urlofApi).pipe(
tap(() => console.log("called")),
publishReplay(1, 5),
refCount()
);
constructor(private http: HttpClient) {}
test() {
const x = this.testX.subscribe();
}
See: https://stackblitz.com/edit/angular-ivy-z7ecyn?file=src%2Fapp%2Fapp.component.ts
However the reslt is not discareded after 5ms, but held indefinitly. What am I missing?
After 3 Hours of debugging, I found the following answer: https://stackoverflow.com/a/54957061
All I had to do, was add a take(1) at the end.
New example:
testX = this.http.get(this.urlofApi).pipe(
tap(() => console.log("called")),
publishReplay(1, 5),
refCount(),
take(1)
);
StackBlitz: https://stackblitz.com/edit/angular-ivy-tyrbdw?file=src%2Fapp%2Fapp.component.ts
Full example:
import { Component, VERSION } from "#angular/core";
import { publishReplay, refCount, take, tap } from "rxjs/operators";
import { HttpClient } from "#angular/common/http";
#Component({
selector: "my-app",
templateUrl: "./app.component.html",
styleUrls: ["./app.component.css"]
})
export class AppComponent {
name = "Angular " + VERSION.major;
urlofApi = "https://api.github.com/search/repositories?q=helloWorld";
testX = this.http.get(this.urlofApi).pipe(
tap(() =>
console.log(
`++ api was newly called at ${new Date().toLocaleTimeString()} ++`
)
),
publishReplay(1, 5000),
refCount(),
take(1)
);
constructor(private http: HttpClient) {}
test() {
console.log("Requesting data from API");
this.testX.pipe(take(1)).subscribe(() => console.log("completed"));
}
}
Related
I want to run code in a function - and then return as a websocket Observable. Effectively monitoring a long running process. I can not figure out how to return the values correctly through the websockets in this format.
My long-running process: ( obviously not going to actually take a long time )
import { Observable } from 'rxjs';
export function longRunningProcess (): Observable<unknown> {
return new Observable(subscriber => {
subscriber.next('End of step 1');
subscriber.next('End of step 2');
subscriber.next('End of step 3');
setTimeout(() => {
subscriber.next('End of Step 4');
subscriber.complete();
}, 1000);
});
}
My NestJS endpoint that returns to the ws ( Websocket )
import { WsAdapter } from '#nestjs/platform-ws';
import {
MessageBody,
SubscribeMessage,
WebSocketGateway,
WebSocketServer,
WsResponse,
} from '#nestjs/websockets';
import { from, Observable, of } from 'rxjs';
import { map } from 'rxjs/operators';
import { Server } from 'ws';
import { longRunningProcess } from './test'
#WebSocketGateway()
export class EventsGateway {
#WebSocketServer()
server: Server;
#SubscribeMessage('events')
// send {"event":"events","data":"test"} in websockets
findAll (#MessageBody() data: any): Observable<WsResponse<unknown>> {
return from(longRunningProcess) // Not really sure how to return this
//return from([1, 2, 3]).pipe(map(item => ({ event: 'events', data: item }))); //<< this works from the sample
}
#SubscribeMessage('identity')
async identity (#MessageBody() data: number): Promise<number> {
return data;
}
}
just map your result from the longRunningProcess like you've did for the numbers array.
#SubscribeMessage('events')
findAll (#MessageBody() data: any): Observable<WsResponse<unknown>> {
return longRunningProcess().pipe(map(item => ({ event: 'events', data: item })));
}
I'm trying to write a test function to a redux-observable epic. The epic works fine inside the app and I can check that it correctly debounces the actions and waits 300ms before emitting. But for some reason while I'm trying to test it with jest the debounce operator triggers immediately. So my test case to ensure that the debounce is working fails.
This is the test case
it('shall not invoke the movies service neither dispatch stuff if the we invoke before 300ms', done => {
const $action = ActionsObservable.of(moviesActions.loadMovies('rambo'));
loadMoviesEpic($action).subscribe(actual => {
throw new Error('Should not have been invoked');
});
setTimeout(() => {
expect(spy).toHaveBeenCalledTimes(0);
done();
}, 200);
});
this is my spy definition.
jest.mock('services/moviesService');
const spy = jest.spyOn(moviesService, 'searchMovies');
beforeEach(() => {
moviesService.searchMovies.mockImplementation(keyword => {
return Promise.resolve(moviesResult);
});
spy.mockClear();
});
and this is the epic
import { Observable, interval } from 'rxjs';
import { combineEpics } from 'redux-observable';
import 'rxjs/add/operator/switchMap';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/debounce';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/catch';
import 'rxjs/add/observable/from';
import 'rxjs/add/observable/of';
import 'rxjs/add/observable/interval';
import 'rxjs/add/observable/concat';
import actionTypes from 'actions/actionTypes';
import * as moviesService from 'services/moviesService';
import * as moviesActions from 'actions/moviesActions';
const DEBOUNCE_INTERVAL_IN_MS = 300;
const MIN_MOVIES_SEARCH_LENGTH = 3;
export function loadMoviesEpic($action) {
return $action
.ofType(actionTypes.MOVIES.LOAD_MOVIES)
.debounce(() => Observable.interval(DEBOUNCE_INTERVAL_IN_MS))
.filter(({ payload }) => payload.length >= MIN_MOVIES_SEARCH_LENGTH)
.switchMap(({ payload }) => {
const loadingAction = Observable.of(moviesActions.loadingMovies());
const moviesResultAction = Observable.from(
moviesService.searchMovies(payload)
)
.map(moviesResultList => moviesActions.moviesLoaded(moviesResultList))
.catch(err => Observable.of(moviesActions.loadError(err)));
return Observable.concat(loadingAction, moviesResultAction);
});
}
const rootEpic = combineEpics(loadMoviesEpic);
export default rootEpic;
so basically this thing shall not be called, because the debounce time is 300ms and I'm trying to check the spy after 200ms. But after 10ms the spy is being invoked.
How can I properly test this epic? I accept any suggestion but preferably I would like to avoid marble testing and rely only on timers and fake timers.
Thanks :D
The issue is that debounce and debounceTime emit right away when they reach the end of the observable they are debouncing.
So since you are emitting with of, the end of the observable is reached and debounce allows the last emitted value through right away.
Here is a simple test that demonstrates the behavior:
import { of } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
test('of', () => {
const spy = jest.fn();
of(1, 2, 3)
.pipe(debounceTime(1000000))
.subscribe(v => spy(v));
expect(spy).toHaveBeenCalledWith(3); // 3 is emitted immediately
})
To effectively test debounce or debounceTime you need to use an observable that keeps emitting and doesn't end using something like interval:
import { interval } from 'rxjs';
import { debounceTime } from 'rxjs/operators';
test('interval', done => {
const spy = jest.fn();
interval(100)
.pipe(debounceTime(500))
.subscribe(v => spy(v));
setTimeout(() => {
expect(spy).not.toHaveBeenCalled(); // Success!
done();
}, 1000);
});
Instead of setTimeout try advanceTimersByTime
I have the following code in Angular 6, that worked fine before.
getNavigation(db): any {
return db.list('/pages', ref => {
let query = ref.limitToLast(100).orderByChild('sortOrder');
return query;
}).snapshotChanges().map(changes => {
return changes.map(change => ({key: change.payload.key, ...change.payload.val()}));
});
}
Suddenly, with some recent library update (rxjs ??) it throws an error? What syntax has changed that suddenly broke my code?
ERROR TypeError: db.list(...).snapshotChanges(...).map is not a
function
at NavigationComponent.push../src/app/navigation.component.ts.NavigationComponent.getNavigation
Or more importantly, how do I fix it? :-(
Pipe the map operator:
getNavigation(db): any {
return db.list('/pages', ref => {
let query = ref.limitToLast(100).orderByChild('sortOrder');
return query;
}).snapshotChanges().pipe(
map(changes => {
return changes.map(change => ({key: change.payload.key, ...change.payload.val()}));
}));
}
Also make sure you import map in the correct way:
import {map} from 'rxjs/operators';
Ok, finally figured this out.
Here is the working code for anyone who runs into a similar problem:
import { Component } from '#angular/core';
import { Observable} from 'rxjs';
import { AngularFireDatabase, AngularFireList } from 'angularfire2/database';
import { map } from 'rxjs/operators';
import { LogoComponent } from './logo.component';
#Component({
selector: 'app-navigation',
templateUrl: './navigation.component.html'
})
export class NavigationComponent {
items: Observable<any[]>;
childItems: Observable<any[]>;
constructor(db: AngularFireDatabase) {
this.items = this.getNavigation(db);
this.childItems = this.getNavigation(db);
}
getNavigation(db: AngularFireDatabase): any {
return db.list('/pages', ref => {
let query = ref.limitToLast(100).orderByChild('sortOrder');
return query;
}).snapshotChanges().pipe(
map(pages => {
return pages.map(p => ({ key: p.key, ...p.payload.val() }));
})
);
}
}
To get past the typescript error, I had to type the db parameter of getNavigation.
Then I had to remove the unnecessary subscribe function that was shown in both the feedback to this question and in AngularFire's migration documents. While this might be necessary in some use cases, it was not in mine.
I am trying to test the two way data binding of ngModel with the following code, but when I am running my test I always get: Expected '' to be 'test#wikitude.com', 'searchQuery property changes after text input'. Maybe it has something to do with the searchField.dispatchEvent part, but so far I couldn't figure out why the test is not changing the textContent of my displayField. The project was built with angular-cli": "1.0.0-beta.15. I tried to follow this guide but so far had no luck. Would be nice if you could help me make my test pass. I am not sure if I have to use fixture.whenStable() - as I've seen it used in the answer to another question - but I don't think that typing text into an input field is an asynchronous activity - I also implemented the sendInput() method mentioned in this question, but so far without any success.
This is my component:
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-search',
templateUrl: './search.component.html',
styleUrls: ['./search.component.css']
})
export class SearchComponent implements OnInit {
searchQuery: string;
active: boolean = false;
onSubmit(): void {
this.active = true;
}
}
This is my component template:
<input id="name" [(ngModel)]="searchQuery" placeholder="customer">
<h2><span>{{searchQuery}}</span></h2>
And here are is my spec:
/* tslint:disable:no-unused-variable */
import {TestBed, async, ComponentFixture, tick} from '#angular/core/testing';
import { SearchComponent } from './search.component';
import {CommonModule} from "#angular/common";
import {FormsModule} from "#angular/forms";
import {By} from "#angular/platform-browser";
describe('Component: SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
beforeEach(() => {
TestBed.configureTestingModule({
declarations: [SearchComponent],
imports: [
CommonModule,
FormsModule
]
});
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
});
it('should bind the search input to the searchQuery variable', () => {
const searchInputText: string = 'test#wikitude.com';
const searchField: HTMLInputElement = fixture.debugElement.query(By.css('input')).nativeElement;
const displayField: HTMLElement = fixture.debugElement.query(By.css('span')).nativeElement;
searchField.value = searchInputText;
searchField.dispatchEvent(new Event('input'));
fixture.detectChanges();
expect(displayField.textContent).toBe(searchInputText, 'searchQuery property changes after text input');
});
});
Update:
I changed my test to the following, which made it pass - as long as the input field is not inside a form tag:
/* tslint:disable:no-unused-variable */
import {TestBed, async, ComponentFixture, tick} from '#angular/core/testing';
import { SearchComponent } from './search.component';
import {CommonModule} from "#angular/common";
import {FormsModule} from "#angular/forms";
import {By} from "#angular/platform-browser";
describe('Component: SearchComponent', () => {
let component: SearchComponent;
let fixture: ComponentFixture<SearchComponent>;
function sendInput(text: string, inputElement: HTMLInputElement) {
inputElement.value = text;
inputElement.dispatchEvent(new Event('input'));
fixture.detectChanges();
return fixture.whenStable();
}
beforeEach(done => {
declarations: [SearchComponent],
imports: [
CommonModule,
FormsModule
]
});
TestBed.compileComponents().then(() => {
fixture = TestBed.createComponent(SearchComponent);
component = fixture.componentInstance;
fixture.detectChanges();
done();
})
});
it('should bind the search input to the searchQuery variable', done => {
const searchInputText: string = 'test#wikitude.com';
const searchField: HTMLInputElement = fixture.debugElement.query(By.css('#name')).nativeElement;
sendInput(searchInputText, searchField).then(() => {
expect(component.searchQuery).toBe(searchInputText);
done();
});
});
});
Here is the updated template:
<form>
<input id="name" [(ngModel)]="searchQuery" placeholder="customer" name="name" #name="ngModel">
</form>
<h2><span>{{searchQuery}}</span></h2>
The test result I get is: Expected undefined to be 'test#wikitude.com'.
I have an Angular 2 component I am trying to put under test, but I am having trouble because the data is set in the ngOnInit function, so is not immediately available in the unit test.
user-view.component.ts:
import {Component, OnInit} from 'angular2/core';
import {RouteParams} from 'angular2/router';
import {User} from './user';
import {UserService} from './user.service';
#Component({
selector: 'user-view',
templateUrl: './components/users/view.html'
})
export class UserViewComponent implements OnInit {
public user: User;
constructor(
private _routeParams: RouteParams,
private _userService: UserService
) {}
ngOnInit() {
const id: number = parseInt(this._routeParams.get('id'));
this._userService
.getUser(id)
.then(user => {
console.info(user);
this.user = user;
});
}
}
user.service.ts:
import {Injectable} from 'angular2/core';
// mock-users is a static JS array
import {users} from './mock-users';
import {User} from './user';
#Injectable()
export class UserService {
getUsers() : Promise<User[]> {
return Promise.resolve(users);
}
getUser(id: number) : Promise<User> {
return Promise.resolve(users[id]);
}
}
user-view.component.spec.ts:
import {
beforeEachProviders,
describe,
expect,
it,
injectAsync,
TestComponentBuilder
} from 'angular2/testing';
import {provide} from 'angular2/core';
import {RouteParams} from 'angular2/router';
import {DOM} from 'angular2/src/platform/dom/dom_adapter';
import {UserViewComponent} from './user-view.component';
import {UserService} from './user.service';
export function main() {
describe('User view component', () => {
beforeEachProviders(() => [
provide(RouteParams, { useValue: new RouteParams({ id: '0' }) }),
UserService
]);
it('should have a name', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
return tcb.createAsync(UserViewComponent)
.then((rootTC) => {
spyOn(console, 'info');
let uvDOMEl = rootTC.nativeElement;
rootTC.detectChanges();
expect(console.info).toHaveBeenCalledWith(0);
expect(DOM.querySelectorAll(uvDOMEl, 'h2').length).toBe(0);
});
}));
});
}
The route param is getting passed correctly, but the view hasn't changed before the tests are run. How do I set up a test that happens after the promise in ngOnInit is resolved?
IMO the best solution for this use case is to just make a synchronous mock service . You can't use fakeAsync for this particular case because of the XHR call for templateUrl. And personally I don't think the "hack" to make ngOnInit return a promise is very elegant. And you should not have to call ngOnInit directly, as it should be called by the framework.
You should already be using mocks anyway, as you are only unit testing the component, and don't want to be dependent on the real service working correctly.
To make a service that is synchronous, simple return the service itself from whatever methods are being called. You can then add your then and catch (subscribe if you are using Observable) methods to the mock, so it acts like a Promise. For example
class MockService {
data;
error;
getData() {
return this;
}
then(callback) {
if (!this.error) {
callback(this.data);
}
return this;
}
catch(callback) {
if (this.error) {
callback(this.error);
}
}
setData(data) {
this.data = data;
}
setError(error) {
this.error = error;
}
}
This has a few benefits. For one it gives you a lot of control over the service during execution, so you can easily customize it's behavior. And of course it's all synchronous.
Here's another example.
A common thing you will see with components is the use of ActivatedRoute and subscribing to its params. This is asynchronous, and done inside the ngOnInit. What I tend to do with this is create a mock for both the ActivatedRoute and the params property. The params property will be a mock object and have some functionality that appears to the outside world like an observable.
export class MockParams {
subscription: Subscription;
error;
constructor(private _parameters?: {[key: string]: any}) {
this.subscription = new Subscription();
spyOn(this.subscription, 'unsubscribe');
}
get params(): MockParams {
return this;
}
subscribe(next: Function, error: Function): Subscription {
if (this._parameters && !this.error) {
next(this._parameters);
}
if (this.error) {
error(this.error);
}
return this.subscription;
}
}
export class MockActivatedRoute {
constructor(public params: MockParams) {}
}
You can see we have a subscribe method that behaves like an Observable#subscribe. Another thing we do is spy on the Subscription so that we can test that it is destroyed. In most cases you will have unsubscribed inside your ngOnDestroy. To set up these mocks in your test you can just do something like
let mockParams: MockParams;
beforeEach(() => {
mockParams = new MockParams({ id: 'one' });
TestBed.configureTestingModule({
imports: [ CommonModule ],
declarations: [ TestComponent ],
providers: [
{ provide: ActivatedRoute, useValue: new MockActivatedRoute(mockParams) }
]
});
});
Now all the params are set for the route, and we have access to the mock params so we can set the error, and also check the subscription spy to make sure its been unsubscribed from.
If you look at the tests below, you will see that they are all synchronous tests. No need for async or fakeAsync, and it passes with flying colors.
Here is the complete test (using RC6)
import { Component, OnInit, OnDestroy, DebugElement } from '#angular/core';
import { CommonModule } from '#angular/common';
import { ActivatedRoute } from '#angular/router';
import { Subscription } from 'rxjs/Subscription';
import { TestBed, async } from '#angular/core/testing';
import { By } from '#angular/platform-browser';
#Component({
template: `
<span *ngIf="id">{{ id }}</span>
<span *ngIf="error">{{ error }}</span>
`
})
export class TestComponent implements OnInit, OnDestroy {
id: string;
error: string;
subscription: Subscription;
constructor(private _route: ActivatedRoute) {}
ngOnInit() {
this.subscription = this._route.params.subscribe(
(params) => {
this.id = params['id'];
},
(error) => {
this.error = error;
}
);
}
ngOnDestroy() {
this.subscription.unsubscribe();
}
}
export class MockParams {
subscription: Subscription;
error;
constructor(private _parameters?: {[key: string]: any}) {
this.subscription = new Subscription();
spyOn(this.subscription, 'unsubscribe');
}
get params(): MockParams {
return this;
}
subscribe(next: Function, error: Function): Subscription {
if (this._parameters && !this.error) {
next(this._parameters);
}
if (this.error) {
error(this.error);
}
return this.subscription;
}
}
export class MockActivatedRoute {
constructor(public params: MockParams) {}
}
describe('component: TestComponent', () => {
let mockParams: MockParams;
beforeEach(() => {
mockParams = new MockParams({ id: 'one' });
TestBed.configureTestingModule({
imports: [ CommonModule ],
declarations: [ TestComponent ],
providers: [
{ provide: ActivatedRoute, useValue: new MockActivatedRoute(mockParams) }
]
});
});
it('should set the id on success', () => {
let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
let debugEl = fixture.debugElement;
let spanEls: DebugElement[] = debugEl.queryAll(By.css('span'));
expect(spanEls.length).toBe(1);
expect(spanEls[0].nativeElement.innerHTML).toBe('one');
});
it('should set the error on failure', () => {
mockParams.error = 'Something went wrong';
let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
let debugEl = fixture.debugElement;
let spanEls: DebugElement[] = debugEl.queryAll(By.css('span'));
expect(spanEls.length).toBe(1);
expect(spanEls[0].nativeElement.innerHTML).toBe('Something went wrong');
});
it('should unsubscribe when component is destroyed', () => {
let fixture = TestBed.createComponent(TestComponent);
fixture.detectChanges();
fixture.destroy();
expect(mockParams.subscription.unsubscribe).toHaveBeenCalled();
});
});
Return a Promise from #ngOnInit:
ngOnInit(): Promise<any> {
const id: number = parseInt(this._routeParams.get('id'));
return this._userService
.getUser(id)
.then(user => {
console.info(user);
this.user = user;
});
}
I ran into the same issue a few days back, and found this to be the most workable solution. As far as I can tell, it doesn't impact anywhere else in the application; since #ngOnInit has no specified return type in the source's TypeScript, I doubt anything in the source code is expecting a return value from that.
Link to OnInit: https://github.com/angular/angular/blob/2.0.0-beta.6/modules/angular2/src/core/linker/interfaces.ts#L79-L122
Edit
In your test, you'd return a new Promise:
it('should have a name', injectAsync([TestComponentBuilder], (tcb: TestComponentBuilder) => {
// Create a new Promise to allow greater control over when the test finishes
//
return new Promise((resolve, reject) => {
tcb.createAsync(UserViewComponent)
.then((rootTC) => {
// Call ngOnInit manually and put your test inside the callback
//
rootTC.debugElement.componentInstance.ngOnInit().then(() => {
spyOn(console, 'info');
let uvDOMEl = rootTC.nativeElement;
rootTC.detectChanges();
expect(console.info).toHaveBeenCalledWith(0);
expect(DOM.querySelectorAll(uvDOMEl, 'h2').length).toBe(0);
// Test is done
//
resolve();
});
});
}));
}
I had the same issue, here is how I managed to fix it. I had to use fakeAsync and tick.
fakeAsync(
inject([TestComponentBuilder], (tcb: TestComponentBuilder) => {
tcb
.overrideProviders(UsersComponent, [
{ provide: UserService, useClass: MockUserService }
])
.createAsync(UsersComponent)
.then(fixture => {
fixture.autoDetectChanges(true);
let component = <UsersComponent>fixture.componentInstance;
component.ngOnInit();
flushMicrotasks();
let element = <HTMLElement>fixture.nativeElement;
let items = element.querySelectorAll('li');
console.log(items);
});
})
)