Skip to content

Commit

Permalink
feat(Effects): add createEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
timdeschryver committed Mar 30, 2019
1 parent 78e237c commit c0466ac
Show file tree
Hide file tree
Showing 14 changed files with 677 additions and 248 deletions.
61 changes: 61 additions & 0 deletions modules/effects/spec/create_effect.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { of } from 'rxjs';
import { createEffect, getCreateEffectMetadata } from '../src/effect_creator';

describe('createEffect()', () => {
it('should flag the variable with a meta tag', () => {
const effect = createEffect(() => of({ type: 'a' }));

expect(effect.hasOwnProperty('__@ngrx/effects_create__')).toBe(true);
});

it('should dispatch by default', () => {
const effect: any = createEffect(() => of({ type: 'a' }));

expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true });
});

it('should be possible to explicitly create a dispatching effect', () => {
const effect: any = createEffect(() => of({ type: 'a' }), {
dispatch: true,
});

expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: true });
});

it('should be possible to create a non-dispatching effect', () => {
const effect: any = createEffect(() => of({ type: 'a' }), {
dispatch: false,
});

expect(effect['__@ngrx/effects_create__']).toEqual({ dispatch: false });
});

describe('getCreateEffectMetadata', () => {
it('should get the effects metadata for a class instance', () => {
class Fixture {
a = createEffect(() => of({ type: 'a' }));
b = createEffect(() => of({ type: 'b' }), { dispatch: true });
c = createEffect(() => of({ type: 'c' }), { dispatch: false });
}

const mock = new Fixture();

expect(getCreateEffectMetadata(mock)).toEqual([
{ propertyName: 'a', dispatch: true },
{ propertyName: 'b', dispatch: true },
{ propertyName: 'c', dispatch: false },
]);
});

it('should return an empty array if the effect has not been created with createEffect()', () => {
const fakeCreateEffect: any = () => {};
class Fixture {
a = fakeCreateEffect(() => of({ type: 'A' }));
}

const mock = new Fixture();

expect(getCreateEffectMetadata(mock)).toEqual([]);
});
});
});
32 changes: 32 additions & 0 deletions modules/effects/spec/effect_decorator.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Effect, getEffectDecoratorMetadata } from '../src/effect_decorator';

describe('@Effect()', () => {
describe('getEffectDecoratorMetadata', () => {
it('should get the effects metadata for a class instance', () => {
class Fixture {
@Effect() a: any;
@Effect() b: any;
@Effect({ dispatch: false })
c: any;
}

const mock = new Fixture();

expect(getEffectDecoratorMetadata(mock)).toEqual([
{ propertyName: 'a', dispatch: true },
{ propertyName: 'b', dispatch: true },
{ propertyName: 'c', dispatch: false },
]);
});

it('should return an empty array if the class has not been decorated', () => {
class Fixture {
a: any;
}

const mock = new Fixture();

expect(getEffectDecoratorMetadata(mock)).toEqual([]);
});
});
});
Loading

0 comments on commit c0466ac

Please sign in to comment.