|
| 1 | +import { writeMultipleFiles } from '../../utils/fs'; |
| 2 | +import { ng, npm } from '../../utils/process'; |
| 3 | +import { updateJsonFile } from '../../utils/project'; |
| 4 | + |
| 5 | +export default function () { |
| 6 | + return updateJsonFile('package.json', packageJson => { |
| 7 | + // Install ngrx |
| 8 | + packageJson['dependencies']['@ngrx/effects'] = '^6.1.0'; |
| 9 | + packageJson['dependencies']['@ngrx/schematics'] = '^6.1.0'; |
| 10 | + packageJson['dependencies']['@ngrx/store'] = '^6.1.0'; |
| 11 | + packageJson['dependencies']['@ngrx/store-devtools'] = '^6.1.0'; |
| 12 | + }) |
| 13 | + .then(() => npm('install')) |
| 14 | + // Create an app that uses ngrx decorators and has e2e tests. |
| 15 | + .then(_ => writeMultipleFiles({ |
| 16 | + './e2e/src/app.po.ts': ` |
| 17 | + import { browser, by, element } from 'protractor'; |
| 18 | + export class AppPage { |
| 19 | + navigateTo() { return browser.get('/'); } |
| 20 | + getIncrementButton() { return element(by.buttonText('Increment')); } |
| 21 | + getDecrementButton() { return element(by.buttonText('Decrement')); } |
| 22 | + getResetButton() { return element(by.buttonText('Reset Counter')); } |
| 23 | + getCounter() { return element(by.xpath('/html/body/app-root/div/span')).getText(); } |
| 24 | + } |
| 25 | + `, |
| 26 | + './e2e/src/app.e2e-spec.ts': ` |
| 27 | + import { AppPage } from './app.po'; |
| 28 | +
|
| 29 | + describe('workspace-project App', () => { |
| 30 | + let page: AppPage; |
| 31 | +
|
| 32 | + beforeEach(() => { |
| 33 | + page = new AppPage(); |
| 34 | + }); |
| 35 | +
|
| 36 | + it('should operate counter', () => { |
| 37 | + page.navigateTo(); |
| 38 | + page.getIncrementButton().click(); |
| 39 | + page.getIncrementButton().click(); |
| 40 | + expect(page.getCounter()).toEqual('2'); |
| 41 | + page.getDecrementButton().click(); |
| 42 | + expect(page.getCounter()).toEqual('1'); |
| 43 | + page.getResetButton().click(); |
| 44 | + expect(page.getCounter()).toEqual('0'); |
| 45 | + }); |
| 46 | + }); |
| 47 | + `, |
| 48 | + './src/app/app.component.ts': ` |
| 49 | + import { Component } from '@angular/core'; |
| 50 | + import { Store, select } from '@ngrx/store'; |
| 51 | + import { Observable } from 'rxjs'; |
| 52 | + import { INCREMENT, DECREMENT, RESET } from './counter.reducer'; |
| 53 | +
|
| 54 | + interface AppState { |
| 55 | + count: number; |
| 56 | + } |
| 57 | +
|
| 58 | + @Component({ |
| 59 | + selector: 'app-root', |
| 60 | + template: \` |
| 61 | + <button (click)="increment()">Increment</button> |
| 62 | + <div>Current Count: <span>{{ count$ | async }}</span></div> |
| 63 | + <button (click)="decrement()">Decrement</button> |
| 64 | +
|
| 65 | + <button (click)="reset()">Reset Counter</button> |
| 66 | + \`, |
| 67 | + }) |
| 68 | + export class AppComponent { |
| 69 | + count$: Observable<number>; |
| 70 | +
|
| 71 | + constructor(private store: Store<AppState>) { |
| 72 | + this.count$ = store.pipe(select(state => state.count)); |
| 73 | + } |
| 74 | +
|
| 75 | + increment() { |
| 76 | + this.store.dispatch({ type: INCREMENT }); |
| 77 | + } |
| 78 | +
|
| 79 | + decrement() { |
| 80 | + this.store.dispatch({ type: DECREMENT }); |
| 81 | + } |
| 82 | +
|
| 83 | + reset() { |
| 84 | + this.store.dispatch({ type: RESET }); |
| 85 | + } |
| 86 | + } |
| 87 | + `, |
| 88 | + './src/app/app.effects.ts': ` |
| 89 | + import { Injectable } from '@angular/core'; |
| 90 | + import { Actions, Effect } from '@ngrx/effects'; |
| 91 | + import { filter, map, tap } from 'rxjs/operators'; |
| 92 | +
|
| 93 | + @Injectable() |
| 94 | + export class AppEffects { |
| 95 | +
|
| 96 | + @Effect() |
| 97 | + mapper$ = this.actions$.pipe(map(() => ({ type: 'ANOTHER'})), filter(() => false)); |
| 98 | +
|
| 99 | + @Effect({ dispatch: false }) |
| 100 | + logger$ = this.actions$.pipe(tap(console.log)); |
| 101 | +
|
| 102 | + constructor(private actions$: Actions) {} |
| 103 | + } |
| 104 | + `, |
| 105 | + './src/app/app.module.ts': ` |
| 106 | + import { BrowserModule } from '@angular/platform-browser'; |
| 107 | + import { NgModule } from '@angular/core'; |
| 108 | +
|
| 109 | + import { AppComponent } from './app.component'; |
| 110 | + import { StoreModule } from '@ngrx/store'; |
| 111 | + import { StoreDevtoolsModule } from '@ngrx/store-devtools'; |
| 112 | + import { environment } from '../environments/environment'; |
| 113 | + import { EffectsModule } from '@ngrx/effects'; |
| 114 | + import { AppEffects } from './app.effects'; |
| 115 | + import { counterReducer } from './counter.reducer'; |
| 116 | +
|
| 117 | + @NgModule({ |
| 118 | + declarations: [ |
| 119 | + AppComponent |
| 120 | + ], |
| 121 | + imports: [ |
| 122 | + BrowserModule, |
| 123 | + StoreModule.forRoot({ count: counterReducer }), |
| 124 | + !environment.production ? StoreDevtoolsModule.instrument() : [], |
| 125 | + EffectsModule.forRoot([AppEffects]) |
| 126 | + ], |
| 127 | + providers: [], |
| 128 | + bootstrap: [AppComponent] |
| 129 | + }) |
| 130 | + export class AppModule { } |
| 131 | + `, |
| 132 | + './src/app/counter.reducer.ts': ` |
| 133 | + import { Action } from '@ngrx/store'; |
| 134 | +
|
| 135 | + export const INCREMENT = 'INCREMENT'; |
| 136 | + export const DECREMENT = 'DECREMENT'; |
| 137 | + export const RESET = 'RESET'; |
| 138 | +
|
| 139 | + const initialState = 0; |
| 140 | +
|
| 141 | + export function counterReducer(state: number = initialState, action: Action) { |
| 142 | + switch (action.type) { |
| 143 | + case INCREMENT: |
| 144 | + return state + 1; |
| 145 | +
|
| 146 | + case DECREMENT: |
| 147 | + return state - 1; |
| 148 | +
|
| 149 | + case RESET: |
| 150 | + return 0; |
| 151 | +
|
| 152 | + default: |
| 153 | + return state; |
| 154 | + } |
| 155 | + } |
| 156 | + `, |
| 157 | + })) |
| 158 | + // Run the e2e tests against a prod build. |
| 159 | + .then(() => ng('e2e', '--prod')); |
| 160 | +} |
0 commit comments