Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9c65994

Browse files
BioPhotonhoebbelsB
authored andcommitted
refactor(state): add docs tests
1 parent 2b9344a commit 9c65994

File tree

1 file changed

+13
-24
lines changed

1 file changed

+13
-24
lines changed

libs/state/effects/src/lib/docs.spec.ts

Lines changed: 13 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { rxEffects } from '@rx-angular/state/effects';
22
import { Component, inject, Injectable, InjectionToken } from '@angular/core';
3-
import { of, timer } from 'rxjs';
3+
import { debounceTime, of, Subject, timer } from 'rxjs';
44
import { TestBed } from '@angular/core/testing';
55
import { wait } from 'nx-cloud/lib/utilities/waiter';
66

@@ -9,52 +9,43 @@ type Movie = {};
99
@Injectable({ providedIn: 'root' })
1010
export class LocalStorage {
1111
items = {};
12+
1213
setItem(prop: string, value: string) {
1314
this.items[prop] = value;
1415
}
16+
1517
removeItem(prop: string) {
1618
delete this.items[prop];
1719
}
20+
1821
getItem(prop: string) {
1922
return this.items[prop];
2023
}
2124
}
2225

23-
const BackupInterval = new InjectionToken<number>('BackupInterval');
24-
2526
@Component({
26-
template: ` <input name="title" [(ngModel)]="movie" />
27+
template: ` <input name="title" (change)="change.next(title.value)" #title />
2728
<button name="save" (click)="save()">Save</button>`,
28-
providers: [{ provide: BackupInterval, useValue: 40 }],
2929
})
3030
class ListComponent {
31-
protected movie = '';
32-
private backupInterval = inject(BackupInterval);
31+
private change = new Subject<string>();
3332
private localStorage = inject(LocalStorage);
3433

3534
private ef = rxEffects(({ register }) => {
36-
const updateBackup = () =>
37-
this.localStorage.setItem('editName', this.movie);
38-
register(timer(0, this.backupInterval), updateBackup);
35+
const updateBackup = (title) => this.localStorage.setItem('title', title);
36+
register(this.change.pipe(debounceTime(300)), updateBackup);
3937
});
4038

4139
save() {
4240
localStorage.removeItem('editName');
4341
}
44-
45-
ngOnInit() {
46-
this.effects.register(this.util.rotationChanged$, () => {
47-
console.log('viewport rotation changed');
48-
});
49-
}
5042
}
5143

5244
// Test helper code ==========
5345

5446
function setupComponent() {
5547
TestBed.configureTestingModule({
5648
declarations: [ListComponent],
57-
providers: [{ provide: BackupInterval, useValue: 10 }],
5849
});
5950

6051
const localStorage = TestBed.inject(LocalStorage);
@@ -63,7 +54,7 @@ function setupComponent() {
6354
const component = fixture.componentInstance;
6455

6556
const searchInputElem: HTMLInputElement = fixture.nativeElement.querySelector(
66-
'input[name="search"]'
57+
'input[name="title"]'
6758
);
6859
const searchInputChange = (value: string) => {
6960
searchInputElem.value = value;
@@ -86,19 +77,17 @@ describe('effects usage in a component', () => {
8677
const spyRemoveItem = jest.spyOn(localStorage, 'removeItem');
8778

8879
expect(spySetItem).toBeCalledTimes(0);
89-
await wait(200);
90-
expect(spySetItem).toBeCalledTimes(1);
91-
expect(spySetItem).toBeCalledWith('');
92-
80+
searchInputChange('abc');
81+
expect(spySetItem).toBeCalledTimes(0); // debounceed
82+
await wait(350);
9383
expect(spySetItem).toBeCalledTimes(1);
94-
expect(spySetItem).toBeCalledWith(1);
84+
expect(spySetItem).toBeCalledWith('title', 'abc');
9585
});
9686
});
9787

9888
function setupComponent2() {
9989
TestBed.configureTestingModule({
10090
declarations: [ListComponent],
101-
providers: [{ provide: BackupInterval, useValue: 10 }],
10291
});
10392

10493
const localStorage = TestBed.inject(LocalStorage);

0 commit comments

Comments
 (0)