All files / src/app/core/utils/dev ngrx-testing.ts

66.66% Statements 22/33
66.66% Branches 2/3
50% Functions 7/14
65.38% Lines 17/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 8358x 58x 58x           58x           58x                       58x                     58x                                                 58x 574x   2318x 570x 570x 1748x     570x 570x 570x     574x            
import { FactoryProvider, Optional } from '@angular/core';
import { Actions } from '@ngrx/effects';
import { Action, ActionReducer, Store } from '@ngrx/store';
 
import { CoreState } from 'ish-core/store/core/core-store';
 
/* eslint-disable no-console */
 
export const containsActionWithType = (type: string) => (actions: Action[]) =>
  !!actions.filter(a => a.type === type).length;
 
/**
 * meta reducer for logging actions to the console
 */
export function logActionsMeta<T = object>(reducer: ActionReducer<T>): ActionReducer<T> {
  return (state: T, action: Action) => {
    Iif (action.type !== '@ngrx/store-devtools/recompute') {
      console.log('action', action);
    }
    return reducer(state, action);
  };
}
 
/**
 * meta reducer for logging the state after every modification to the console
 */
export function logStateMeta<T = object>(reducer: ActionReducer<T>): ActionReducer<T> {
  return (state: T, action: Action) => {
    const newState = reducer(state, action);
    console.log('state', newState);
    return newState;
  };
}
 
/**
 * Pseudo class for enriching ngrx {@link Store} with additional properties for testing.
 */
export abstract class StoreWithSnapshots {
  /**
   * Provides access to a synchronous snapshot of the state.
   */
  state: CoreState;
 
  abstract dispatch(action: Action): void;
 
  /**
   * Retrieve a list of actions fired since the last reset.
   *
   * @param regex optional filter
   */
  abstract actionsArray(regex?: RegExp): Action[];
 
  /**
   * Reset actions history.
   */
  abstract reset(): void;
}
 
/**
 * Provider wrapping ngrx {@link Store} for testing.
 * use it in combination with {@link StoreWithSnapshots}.
 */
export function provideStoreSnapshots(): FactoryProvider {
  const actionList: Action[] = [];
  function saveStore(store: StoreWithSnapshots & Store, actions: Actions) {
    store.subscribe(state => (store.state = state as CoreState));
    if (actions) {
      actions.subscribe(action => {
        actionList.push(action);
      });
    }
    store.actionsArray = (regex = /.*/) => actionList.filter(action => regex.test(action.type));
    store.reset = () => actionList.splice(0, actionList.length);
    return store;
  }
 
  return {
    provide: StoreWithSnapshots,
    useFactory: saveStore,
    deps: [Store, [new Optional(), Actions]],
  };
}