Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Effects, Example): add createEffect #1667

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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