@ngrx/effects#getEffectsMetadata TypeScript Examples

The following examples show how to use @ngrx/effects#getEffectsMetadata. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: auth.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 5 votes vote down vote up
describe('AuthEffects', () => {
  let localStorageService: jasmine.SpyObj<LocalStorageService>;
  let router: jasmine.SpyObj<Router>;

  beforeEach(() => {
    localStorageService = jasmine.createSpyObj('LocalStorageService', [
      'setItem'
    ]);
    router = jasmine.createSpyObj('Router', ['navigateByUrl']);
  });

  describe('login', () => {
    it('should not dispatch any action', () => {
      const actions = new Actions(EMPTY);
      const effect = new AuthEffects(actions, localStorageService, router);
      const metadata = getEffectsMetadata(effect);

      expect(metadata.login.dispatch).toEqual(false);
    });

    it('should call setItem on LocalStorageService', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;
        const loginAction = authLogin();
        const source = cold('a', { a: loginAction });
        const actions = new Actions(source);
        const effect = new AuthEffects(actions, localStorageService, router);

        effect.login.subscribe(() => {
          expect(localStorageService.setItem).toHaveBeenCalledWith(AUTH_KEY, {
            isAuthenticated: true
          });
        });
      });
    });
  });

  describe('logout', () => {
    it('should not dispatch any action', () => {
      const actions = new Actions(EMPTY);
      const effect = new AuthEffects(actions, localStorageService, router);
      const metadata = getEffectsMetadata(effect);

      expect(metadata.logout.dispatch).toEqual(false);
    });

    it('should call setItem on LocalStorageService and navigate to about', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;
        const logoutAction = authLogout();
        const source = cold('a', { a: logoutAction });
        const actions = new Actions(source);
        const effect = new AuthEffects(actions, localStorageService, router);

        effect.login.subscribe(() => {
          expect(localStorageService.setItem).toHaveBeenCalledWith(AUTH_KEY, {
            isAuthenticated: false
          });
          expect(router.navigate).toHaveBeenCalledWith(['']);
        });
      });
    });
  });
});
Example #2
Source File: google-analytics.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 5 votes vote down vote up
describe('GoogleAnalyticsEffects', () => {
  let router: any;
  const ga = (<any>window).ga;

  beforeEach(() => {
    router = {
      routerState: {
        snapshot: {}
      },
      events: {
        pipe() {}
      }
    };

    (<any>window).ga = jasmine.createSpy('ga');
  });

  afterAll(() => {
    (<any>window).ga = ga;
  });

  it('should not dispatch action', function () {
    const effect = new GoogleAnalyticsEffects(router);
    const metadata = getEffectsMetadata(effect);

    expect(metadata.pageView.dispatch).toEqual(false);
  });

  it('should call google analytics', function () {
    scheduler.run((helpers) => {
      const { cold } = helpers;

      const routerEvent = new NavigationEnd(1, '', '');
      router.events = cold('a', { a: routerEvent });
      const effect = new GoogleAnalyticsEffects(router);

      effect.pageView().subscribe(() => {
        expect((<any>window).ga).toHaveBeenCalled();
        expect((<any>window).ga).toHaveBeenCalledWith(
          'set',
          'page',
          routerEvent.urlAfterRedirects
        );
        expect((<any>window).ga).toHaveBeenCalledWith('send', 'pageview');
      });
    });
  });
});
Example #3
Source File: books.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 5 votes vote down vote up
describe('BooksEffects', () => {
  describe('persistBooks', () => {
    const booksState: BookState = {
      entities: {
        '1': {
          author: 'Author',
          description: 'Description',
          id: '1',
          title: 'Title'
        }
      },
      ids: ['1']
    };
    let localStorage: LocalStorageService;
    let store: Store<any>;

    beforeEach(() => {
      localStorage = jasmine.createSpyObj('localStorage', ['setItem']);
      store = of({
        examples: {
          books: booksState
        }
      }) as any;
    });

    it('should not dispatch any actions', () => {
      const actions = new Actions(EMPTY);
      const effects = new BooksEffects(actions, store, localStorage);
      const metadata = getEffectsMetadata(effects);

      expect(metadata.persistBooks.dispatch).toEqual(false);
    });

    it('should call setItem on LocalStorageService for delete one action', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;
        const action = actionBooksDeleteOne({ id: '1' });
        const source = cold('a', { a: action });
        const actions = new Actions(source);
        const effects = new BooksEffects(actions, store, localStorage);

        effects.persistBooks.subscribe(() => {
          expect(localStorage.setItem).toHaveBeenCalledWith(
            BOOKS_KEY,
            booksState
          );
        });
      });
    });

    it('should call setItem on LocalStorageService for upsert one action', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;
        const action = actionBooksUpsertOne({ book: {} as any });
        const source = cold('a', { a: action });
        const actions = new Actions(source);
        const effects = new BooksEffects(actions, store, localStorage);

        effects.persistBooks.subscribe(() => {
          expect(localStorage.setItem).toHaveBeenCalledWith(
            BOOKS_KEY,
            booksState
          );
        });
      });
    });
  });
});
Example #4
Source File: form.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 5 votes vote down vote up
describe('FormEffects', () => {
  let localStorageService: LocalStorageService;

  beforeEach(() => {
    localStorageService = jasmine.createSpyObj('LocalStorageService', [
      'setItem'
    ]);
  });

  describe('persistForm', () => {
    it('should not dispatch any action', () => {
      const actions = new Actions(EMPTY);
      const effect = new FormEffects(actions, localStorageService);
      const metadata = getEffectsMetadata(effect);

      expect(metadata.persistForm.dispatch).toEqual(false);
    });

    it('should call setItem on LocalStorageService for UPDATE action', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;
        const form: Form = {
          autosave: false,
          username: 'test',
          password: 'test',
          email: '[email protected]',
          description: 'It is a test.',
          requestGift: true,
          birthday: new Date(),
          rating: 10
        };
        const action = actionFormUpdate({ form });
        const source = cold('a', { a: action });
        const actions = new Actions(source);
        const effect = new FormEffects(actions, localStorageService);

        effect.persistForm.subscribe(() => {
          expect(localStorageService.setItem).toHaveBeenCalledWith(FORM_KEY, {
            form
          });
        });
      });
    });
  });
});
Example #5
Source File: todos.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 5 votes vote down vote up
describe('TodosEffects', () => {
  let localStorage: jasmine.SpyObj<LocalStorageService>;
  let store: jasmine.SpyObj<Store<State>>;

  beforeEach(() => {
    localStorage = jasmine.createSpyObj('LocalStorageService', ['setItem']);
    store = jasmine.createSpyObj('store', ['pipe']);
  });

  describe('persistTodos', () => {
    it('should not dispatch any action', () => {
      const actions$ = new Actions();
      const effect = new TodosEffects(actions$, store, localStorage);
      const metadata = getEffectsMetadata(effect);

      expect(metadata.persistTodos.dispatch).toEqual(false);
    });

    it('should call setItem on LocalStorageService for any action', () => {
      scheduler.run((helpers) => {
        const { cold } = helpers;

        const todosState: TodosState = {
          items: [{ id: '1', name: 'Test ToDo', done: false }],
          filter: 'ALL'
        };
        store.pipe.and.returnValue(of(todosState));
        const persistAction = actionTodosToggle({ id: 'a' });
        const source = cold('a', { a: persistAction });
        const actions = new Actions(source);
        const effect = new TodosEffects(actions, store, localStorage);

        effect.persistTodos.subscribe(() => {
          expect(localStorage.setItem).toHaveBeenCalledWith(
            TODOS_KEY,
            todosState
          );
        });
      });
    });
  });
});
Example #6
Source File: examples.effects.spec.ts    From enterprise-ng-2020-workshop with MIT License 4 votes vote down vote up
describe('SettingsEffects', () => {
  let router: any;
  let titleService: jasmine.SpyObj<TitleService>;
  let translateService: jasmine.SpyObj<TranslateService>;
  let store: jasmine.SpyObj<Store<State>>;

  beforeEach(() => {
    router = {
      routerState: {
        snapshot: {
          root: {}
        }
      },
      events: {
        pipe() {}
      }
    };

    titleService = jasmine.createSpyObj('TitleService', ['setTitle']);
    translateService = jasmine.createSpyObj('TranslateService', ['use']);
    store = jasmine.createSpyObj('store', ['pipe']);
  });

  describe('setTranslateServiceLanguage', () => {
    it('should not dispatch action', () => {
      const actions = new Actions<any>();
      const effect = new ExamplesEffects(
        actions,
        store,
        translateService,
        router,
        titleService
      );
      const metadata = getEffectsMetadata(effect);
      expect(metadata.setTranslateServiceLanguage.dispatch).toEqual(false);
    });
  });

  describe('setTitle', () => {
    it('should not dispatch action', () => {
      const actions = new Actions<any>();
      const effect = new ExamplesEffects(
        actions,
        store,
        translateService,
        router,
        titleService
      );
      const metadata = getEffectsMetadata(effect);

      expect(metadata.setTitle.dispatch).toEqual(false);
    });

    it('should setTitle', () => {
      scheduler.run((helpers) => {
        const { cold, hot } = helpers;
        const action = actionSettingsChangeLanguage({ language: 'en' });
        const actions = hot('-a', { a: action });

        const routerEvent = new ActivationEnd(router.routerState.snapshot);
        router.events = cold('a', { a: routerEvent });

        const effect = new ExamplesEffects(
          actions,
          store,
          translateService,
          router,
          titleService
        );

        effect.setTitle.subscribe(() => {
          expect(titleService.setTitle).toHaveBeenCalled();
          expect(titleService.setTitle).toHaveBeenCalledWith(
            router.routerState.snapshot.root,
            translateService
          );
        });
      });
    });
  });
});