/
third-party-decorators.ts
169 lines (142 loc) · 5.49 KB
/
third-party-decorators.ts
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { writeMultipleFiles } from '../../utils/fs';
import { installWorkspacePackages } from '../../utils/packages';
import { ng } from '../../utils/process';
import { updateJsonFile } from '../../utils/project';
export default async function () {
await updateJsonFile('package.json', (packageJson) => {
// Install ngrx
packageJson['dependencies']['@ngrx/effects'] = '^13.2.0';
packageJson['dependencies']['@ngrx/schematics'] = '^13.2.0';
packageJson['dependencies']['@ngrx/store'] = '^13.2.0';
packageJson['dependencies']['@ngrx/store-devtools'] = '^13.2.0';
// TODO(crisbeto): ngrx hasn't been updated for TS 4.7 yet.
packageJson['devDependencies']['typescript'] = '~4.6.2';
});
// Force is need to prevent npm 7+ from failing due to potential peer dependency resolution range errors.
// This is especially common when testing snapshot builds for new prereleases.
await installWorkspacePackages({ force: true });
// Create an app that uses ngrx decorators and has e2e tests.
await writeMultipleFiles({
'./e2e/src/app.po.ts': `
import { browser, by, element } from 'protractor';
export class AppPage {
async navigateTo() { return browser.get('/'); }
getIncrementButton() { return element(by.buttonText('Increment')); }
getDecrementButton() { return element(by.buttonText('Decrement')); }
getResetButton() { return element(by.buttonText('Reset Counter')); }
async getCounter() { return element(by.xpath('/html/body/app-root/div/span')).getText(); }
}
`,
'./e2e/src/app.e2e-spec.ts': `
import { AppPage } from './app.po';
describe('workspace-project App', () => {
let page: AppPage;
beforeEach(() => {
page = new AppPage();
});
it('should operate counter', async () => {
await page.navigateTo();
await page.getIncrementButton().click();
await page.getIncrementButton().click();
expect(await page.getCounter()).toEqual('2');
await page.getDecrementButton().click();
expect(await page.getCounter()).toEqual('1');
await page.getResetButton().click();
expect(await page.getCounter()).toEqual('0');
});
});
`,
'./src/app/app.component.ts': `
import { Component } from '@angular/core';
import { Store, select } from '@ngrx/store';
import { Observable } from 'rxjs';
import { INCREMENT, DECREMENT, RESET } from './counter.reducer';
interface AppState {
count: number;
}
@Component({
selector: 'app-root',
template: \`
<button (click)="increment()">Increment</button>
<div>Current Count: <span>{{ count$ | async }}</span></div>
<button (click)="decrement()">Decrement</button>
<button (click)="reset()">Reset Counter</button>
\`,
})
export class AppComponent {
count$: Observable<number>;
constructor(private store: Store<AppState>) {
this.count$ = store.pipe(select(state => state.count));
}
increment() {
this.store.dispatch({ type: INCREMENT });
}
decrement() {
this.store.dispatch({ type: DECREMENT });
}
reset() {
this.store.dispatch({ type: RESET });
}
}
`,
'./src/app/app.effects.ts': `
import { Injectable } from '@angular/core';
import { Actions, Effect } from '@ngrx/effects';
import { filter, map, tap } from 'rxjs/operators';
@Injectable()
export class AppEffects {
@Effect()
mapper$ = this.actions$.pipe(map(() => ({ type: 'ANOTHER'})), filter(() => false));
@Effect({ dispatch: false })
logger$ = this.actions$.pipe(tap(console.log));
constructor(private actions$: Actions) {}
}
`,
'./src/app/app.module.ts': `
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { StoreModule } from '@ngrx/store';
import { StoreDevtoolsModule } from '@ngrx/store-devtools';
import { environment } from '../environments/environment';
import { EffectsModule } from '@ngrx/effects';
import { AppEffects } from './app.effects';
import { counterReducer } from './counter.reducer';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
StoreModule.forRoot({ count: counterReducer }),
!environment.production ? StoreDevtoolsModule.instrument() : [],
EffectsModule.forRoot([AppEffects])
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
`,
'./src/app/counter.reducer.ts': `
import { Action } from '@ngrx/store';
export const INCREMENT = 'INCREMENT';
export const DECREMENT = 'DECREMENT';
export const RESET = 'RESET';
const initialState = 0;
export function counterReducer(state: number = initialState, action: Action) {
switch (action.type) {
case INCREMENT:
return state + 1;
case DECREMENT:
return state - 1;
case RESET:
return 0;
default:
return state;
}
}
`,
});
// Run the e2e tests against a production build.
await ng('e2e', '--port=0', '--configuration=production');
}