Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8df99ea

Browse filesBrowse files
authored
fix(cdk-render-strategies): don't send complete event to strategy-behavior (#954)
1 parent e3db397 commit 8df99ea
Copy full SHA for 8df99ea

File tree

2 files changed

+101
-31
lines changed
Filter options

2 files changed

+101
-31
lines changed
+94Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { ɵglobal } from '@angular/core';
2+
import { TestScheduler } from 'rxjs/testing';
3+
import * as rxjs from 'rxjs';
4+
// tslint:disable-next-line:nx-enforce-module-boundaries
5+
import { jestMatcher } from '@test-helpers';
6+
// tslint:disable-next-line:nx-enforce-module-boundaries
7+
import { onStrategy, RX_NATIVE_STRATEGIES } from '@rx-angular/cdk';
8+
import { animationFrameScheduler, observeOn, of } from 'rxjs';
9+
10+
// tslint:disable: no-duplicate-string
11+
describe('onStrategy', () => {
12+
13+
let testScheduler: TestScheduler;
14+
let handles = [];
15+
let animationRuns = 0;
16+
function _animate() {
17+
handles.forEach(handle => handle());
18+
}
19+
let _animationFrameRestore;
20+
let _cancelAnimationFrameRestore;
21+
beforeEach(() => {
22+
testScheduler = new TestScheduler(jestMatcher);
23+
_animationFrameRestore = ɵglobal.requestAnimationFrame;
24+
_cancelAnimationFrameRestore = ɵglobal.cancelAnimationFrame;
25+
ɵglobal.requestAnimationFrame = (cb?) => {
26+
handles[animationRuns] = cb;
27+
animationRuns++;
28+
return animationRuns;
29+
}
30+
ɵglobal.cancelAnimationFrame = (id: number) => {
31+
handles = handles.splice(id, 1);
32+
}
33+
});
34+
35+
36+
afterEach(() => {
37+
ɵglobal.requestAnimationFrame = _animationFrameRestore;
38+
ɵglobal.cancelAnimationFrame = _cancelAnimationFrameRestore;
39+
animationRuns = 0;
40+
handles = [];
41+
});
42+
43+
it('should emit 42', () => {
44+
testScheduler.run(({expectObservable}) => {
45+
const work = jest.fn();
46+
const values = { x: 42 };
47+
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.native, work);
48+
const expected = '(x|)';
49+
expectObservable(stream$).toBe(expected, values)
50+
});
51+
});
52+
53+
it('should throw error', () => {
54+
testScheduler.run(({expectObservable}) => {
55+
const error = new Error('error');
56+
const work = () => {
57+
throw error;
58+
}
59+
const value = 42;
60+
const errorValues = [error, value];
61+
const expected = '#';
62+
const stream$ = onStrategy(value, RX_NATIVE_STRATEGIES.native, work);
63+
expectObservable(stream$).toBe(expected, null, errorValues);
64+
});
65+
});
66+
67+
it('should run on animationFrame', () => {
68+
testScheduler.run(({ expectObservable, animate }) => {
69+
const work = jest.fn();
70+
animate(' --------x');
71+
const expected = '--------(x|)';
72+
of(null).pipe(observeOn(animationFrameScheduler)).subscribe(() => {
73+
_animate();
74+
});
75+
const values = { x: 42 };
76+
const stream$ = onStrategy(values.x, RX_NATIVE_STRATEGIES.local, work);
77+
expectObservable(stream$).toBe(expected, values);
78+
});
79+
});
80+
81+
it('it should call work once when async', () => {
82+
const work = jest.fn();
83+
onStrategy(42, RX_NATIVE_STRATEGIES.local, work).subscribe();
84+
expect(work).not.toHaveBeenCalled();
85+
_animate();
86+
expect(work).toHaveBeenCalledTimes(1);
87+
});
88+
89+
it('it should call work', () => {
90+
const work = jest.fn();
91+
onStrategy(42, RX_NATIVE_STRATEGIES.native, work).subscribe();
92+
expect(work).toHaveBeenCalledTimes(1);
93+
});
94+
});

‎libs/cdk/src/lib/utils/onStrategy.ts

Copy file name to clipboard
+7-31Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,14 @@
11
import { RxCoalescingOptions } from '@rx-angular/cdk/coalescing';
2-
import { switchMap } from 'rxjs/operators';
2+
import { switchMap, take } from 'rxjs/operators';
33
import {
44
RxRenderWork,
55
RxStrategyCredentials,
66
} from '../model';
7-
import { Observable, Observer, of, throwError } from 'rxjs';
7+
import { Observable, of, throwError } from 'rxjs';
88
import {
9-
RxRenderError,
109
RxRenderErrorFactory,
1110
} from '../template-management/render-error';
1211

13-
/**
14-
* @internal
15-
*
16-
* @param value
17-
* @param strategy
18-
* @param workFactory
19-
* @param options
20-
*/
21-
/*export function onStrategy<T>(
22-
value: T,
23-
strategy: RxStrategyCredentials,
24-
workFactory: (
25-
value: T,
26-
work: RxRenderWork,
27-
options: RxCoalescingOptions
28-
) => void,
29-
options: RxCoalescingOptions = {}
30-
): Observable<T> {
31-
return of(value).pipe(
32-
strategy.behavior(
33-
() => workFactory(value, strategy.work, options),
34-
options.scope || {}
35-
)
36-
);
37-
}*/
38-
3912
/**
4013
* @internal
4114
*
@@ -57,7 +30,9 @@ export function onStrategy<T>(
5730
errorFactory: RxRenderErrorFactory<T, any> = (e, v) => [e, v]
5831
): Observable<T> {
5932
let error: Error;
60-
return of(value).pipe(
33+
return new Observable<T>(subscriber => {
34+
subscriber.next(value);
35+
}).pipe(
6136
strategy.behavior(() => {
6237
try {
6338
workFactory(value, strategy.work, options);
@@ -67,6 +42,7 @@ export function onStrategy<T>(
6742
}, options.scope || {}),
6843
switchMap(() =>
6944
error ? throwError(errorFactory(error, value)) : of(value)
70-
)
45+
),
46+
take(1)
7147
);
7248
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.