Skip to content

Instantly share code, notes, and snippets.

@AvocadoVenom
AvocadoVenom / data.reducer.ts
Created February 20, 2020 11:55
A data store reducer definition file
import * as Actions from './data.actions';
export interface State {
data: any;
}
export const initialState: State = {
data: null
};
@AvocadoVenom
AvocadoVenom / data.actions.spec.ts
Last active February 20, 2020 22:31
Unit Tests for Data Actions
import * as Actions from './data.actions';
describe('Store > Data > DataActions', () => {
it('SHOULD create a LoadData action', () => {
const action = new Actions.LoadData();
expect(action.type).toEqual(Actions.LOAD_DATA);
});
it('SHOULD create a SetData action containing a payload', () => {
const payload = { id: 1, value: {} };
@AvocadoVenom
AvocadoVenom / data.effects.spec.ts
Last active February 20, 2020 22:34
Unit Tests for Data Effects
import { Observable, of } from 'rxjs';
import { Action } from '@ngrx/store';
import { TestBed, async } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { provideMockStore } from '@ngrx/store/testing';
import { DataEffect } from './data.effects';
import * as fromActions from './data.actions';
import { DataService } from '@app/data/services/data/data.service';
@AvocadoVenom
AvocadoVenom / data.reducer.spec.ts
Created February 20, 2020 12:11
Unit Tests for Data Reducer
import * as fromReducer from './data.reducer';
import * as fromActions from './data.actions';
describe('Store > Data > DataReducer', () => {
afterEach(() => {
fromReducer.initialState.data = null;
});
it('SHOULD return the default state', () => {
const { initialState } = fromReducer;
@AvocadoVenom
AvocadoVenom / data.selectors.ts
Created February 20, 2020 12:13
A data store selectors definition file
import { createSelector } from '@ngrx/store';
import { AppState } from '@store/app.reducer';
const dataFeature = (state: AppState) => state.data;
export const getData = createSelector(
dataFeature,
state => state.data
);
@AvocadoVenom
AvocadoVenom / data.selectors.spec.ts
Last active February 20, 2020 22:35
Unit Tests for Data Selectors
import * as selectors from './data.selectors';
import { AppState } from '@store/app.reducer';
import { State } from './data.reducer';
const createData = ({
name = '',
value = null
} = {}): any => ({
name, value
});
@AvocadoVenom
AvocadoVenom / tip-scannedActions.spec.ts
Last active May 10, 2020 12:42
A short example about how to use MockStore' scannedActions$ property
// Commented configuration
// import { MockStore, provideMockStore } from '@ngrx/store/testing';
// let store: MockStore<any>;
// beforeEach(async(() => {
// TestBed.configureTestingModule({
// [...]
// providers: [
// provideMockStore({ initialState }),
{
[...]
"projects": {
"my-project": {
"architect": {
"build": {
"configurations": {
"production": {
"fileReplacements": [
{
export const environment = {
production: true,
apiUrl: '%#$WS_ROOT_URL#%'
}
{
"start:dev": "npm run generate-env-file:dev && ng serve --configuration=local-dev",
"start:staging": "npm run generate-env-file:staging && ng serve --configuration=local-staging",
"start:prod": "npm run generate-env-file:prod && ng serve --configuration=local-production",
"generate-env-file:dev": "node .\\path\\to\\script\\replace-env-var.script.js dev",
"generate-env-file:staging": "node .\\path\\to\\script\\replace-env-var.script.js staging",
"generate-env-file:prod": "node .\\path\\to\\script\\replace-env-var.script.js prod",
}