Skip to content

Commit ced2d3d

Browse files
timdeschryverbrandonroberts
authored andcommitted
feat(effects): add createEffect function (#1667)
Closes #1368
1 parent a41d1d6 commit ced2d3d

19 files changed

+779
-340
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { of } from 'rxjs';
2+
import { createEffect, getCreateEffectMetadata } from '../src/effect_creator';
3+
4+
describe('createEffect()', () => {
5+
it('should flag the variable with a meta tag', () => {
6+
const effect = createEffect(() => of({ type: 'a' }));
7+
8+
expect(effect.hasOwnProperty('__@ngrx/effects_create__')).toBe(true);
9+
});
10+
11+
it('should dispatch by default', () => {
12+
const effect: any = createEffect(() => of({ type: 'a' }));
13+
14+
expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true });
15+
});
16+
17+
it('should be possible to explicitly create a dispatching effect', () => {
18+
const effect: any = createEffect(() => of({ type: 'a' }), {
19+
dispatch: true,
20+
});
21+
22+
expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true });
23+
});
24+
25+
it('should be possible to create a non-dispatching effect', () => {
26+
const effect: any = createEffect(() => of({ type: 'a' }), {
27+
dispatch: false,
28+
});
29+
30+
expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: false });
31+
});
32+
33+
describe('getCreateEffectMetadata', () => {
34+
it('should get the effects metadata for a class instance', () => {
35+
class Fixture {
36+
a = createEffect(() => of({ type: 'a' }));
37+
b = createEffect(() => of({ type: 'b' }), { dispatch: true });
38+
c = createEffect(() => of({ type: 'c' }), { dispatch: false });
39+
}
40+
41+
const mock = new Fixture();
42+
43+
expect(getCreateEffectMetadata(mock)).toEqual([
44+
{ propertyName: 'a', dispatch: true },
45+
{ propertyName: 'b', dispatch: true },
46+
{ propertyName: 'c', dispatch: false },
47+
]);
48+
});
49+
50+
it('should return an empty array if the effect has not been created with createEffect()', () => {
51+
const fakeCreateEffect: any = () => {};
52+
class Fixture {
53+
a = fakeCreateEffect(() => of({ type: 'A' }));
54+
}
55+
56+
const mock = new Fixture();
57+
58+
expect(getCreateEffectMetadata(mock)).toEqual([]);
59+
});
60+
});
61+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { Effect, getEffectDecoratorMetadata } from '../src/effect_decorator';
2+
3+
describe('@Effect()', () => {
4+
describe('getEffectDecoratorMetadata', () => {
5+
it('should get the effects metadata for a class instance', () => {
6+
class Fixture {
7+
@Effect() a: any;
8+
@Effect() b: any;
9+
@Effect({ dispatch: false })
10+
c: any;
11+
}
12+
13+
const mock = new Fixture();
14+
15+
expect(getEffectDecoratorMetadata(mock)).toEqual([
16+
{ propertyName: 'a', dispatch: true },
17+
{ propertyName: 'b', dispatch: true },
18+
{ propertyName: 'c', dispatch: false },
19+
]);
20+
});
21+
22+
it('should return an empty array if the class has not been decorated', () => {
23+
class Fixture {
24+
a: any;
25+
}
26+
27+
const mock = new Fixture();
28+
29+
expect(getEffectDecoratorMetadata(mock)).toEqual([]);
30+
});
31+
});
32+
});

0 commit comments

Comments
 (0)