Skip to content

Commit 7dbb571

Browse files
MikeRyanDevbrandonroberts
authored andcommitted
feat(Effects): Introduce new Effects testing module (#70)
1 parent d176a11 commit 7dbb571

File tree

14 files changed

+402
-418
lines changed

14 files changed

+402
-418
lines changed

build/builder.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export default createBuilder([
1313
[ 'Removing remaining sourcemap files', tasks.removeRemainingSourceMapFiles ],
1414
[ 'Copying type definition files', tasks.copyTypeDefinitionFiles ],
1515
[ 'Minifying UMD bundles', tasks.minifyUmdBundles ],
16-
[ 'Copying package documents', tasks.copyPackageDocs ],
16+
[ 'Copying documents', tasks.copyDocs ],
17+
[ 'Copying package.json files', tasks.copyPackageJsonFiles ],
1718
[ 'Removing "./dist/packages" Folder', tasks.removePackagesFolder ],
1819
]);

build/config.ts

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,28 @@
1+
export interface PackageDescription {
2+
name: string,
3+
hasTestingModule: boolean,
4+
}
5+
16
export interface Config {
2-
packages: string[];
7+
packages: PackageDescription[];
38
scope: string;
49
}
510

6-
export const packages = [
7-
'store',
8-
'effects',
9-
'router-store',
10-
'store-devtools',
11+
export const packages: PackageDescription[] = [
12+
{
13+
name: 'store',
14+
hasTestingModule: false,
15+
},
16+
{
17+
name: 'effects',
18+
hasTestingModule: true,
19+
},
20+
{
21+
name: 'router-store',
22+
hasTestingModule: false,
23+
},
24+
{
25+
name: 'store-devtools',
26+
hasTestingModule: false,
27+
},
1128
];

build/tasks.ts

Lines changed: 59 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,14 @@ export async function removeDistFolder(config: Config) {
2020
* AOT and Closure compatible JavaScript
2121
*/
2222
export async function compilePackagesWithNgc(config: Config) {
23-
const [storePkg, ...restPkgs] = config.packages;
23+
const pkgs = util.getTopLevelPackages(config);
24+
const storePkg = 'store';
25+
const restPkgs = pkgs.filter(name => name !== storePkg);
26+
const testPkgs = util.getTestingPackages(config);
2427

2528
await _compilePackagesWithNgc(storePkg);
2629
await mapPackages(restPkgs, _compilePackagesWithNgc);
30+
await mapPackages(testPkgs, _compilePackagesWithNgc);
2731
}
2832

2933
async function _compilePackagesWithNgc(pkg: string) {
@@ -43,15 +47,19 @@ async function _compilePackagesWithNgc(pkg: string) {
4347
* Uses Rollup to bundle the JavaScript into a single flat file called
4448
* a FESM (Flat Ecma Script Module)
4549
*/
46-
export async function bundleFesms({packages, scope}: Config) {
47-
await mapPackages(packages, async (pkg) => {
50+
export async function bundleFesms(config: Config) {
51+
const pkgs = util.getAllPackages(config);
52+
53+
await mapPackages(pkgs, async (pkg) => {
54+
const topLevelName = util.getTopLevelName(pkg);
55+
4856
await util.exec('rollup', [
4957
`-i ./dist/packages/${pkg}/index.js`,
50-
`-o ./dist/${pkg}/${scope}/${pkg}.js`,
58+
`-o ./dist/${topLevelName}/${config.scope}/${pkg}.js`,
5159
`--sourcemap`,
5260
]);
5361

54-
await util.mapSources(`./dist/${pkg}/${scope}/${pkg}.js`);
62+
await util.mapSources(`./dist/${topLevelName}/${config.scope}/${pkg}.js`);
5563
});
5664
}
5765

@@ -60,7 +68,8 @@ export async function bundleFesms({packages, scope}: Config) {
6068
* Copies each FESM into a TS file then uses TypeScript to downlevel
6169
* the FESM into ES5 with ESM modules
6270
*/
63-
export async function downLevelFesmsToES5({packages, scope}: Config) {
71+
export async function downLevelFesmsToES5(config: Config) {
72+
const packages = util.getAllPackages(config);
6473
const tscArgs = [
6574
'--target es5',
6675
'--module es2015',
@@ -69,31 +78,37 @@ export async function downLevelFesmsToES5({packages, scope}: Config) {
6978
];
7079

7180
await mapPackages(packages, async (pkg) => {
72-
const file = `./dist/${pkg}/${scope}/${pkg}.js`;
73-
const target = `./dist/${pkg}/${scope}/${pkg}.es5.ts`;
81+
const topLevelName = util.getTopLevelName(pkg);
82+
83+
const file = `./dist/${topLevelName}/${config.scope}/${pkg}.js`;
84+
const target = `./dist/${topLevelName}/${config.scope}/${pkg}.es5.ts`;
7485

7586
util.copy(file, target);
7687

7788
await util.ignoreErrors(util.exec('tsc', [ target, ...tscArgs ]));
7889
await util.mapSources(target.replace('.ts', '.js'));
90+
await util.remove(target);
7991
});
8092

81-
await util.removeRecursively(`./dist/?(${packages.join('|')})/${scope}/*.ts`);
93+
await util.removeRecursively(`./dist/**/*/${config.scope}/*.ts`);
8294
}
8395

8496

8597
/**
8698
* Re-runs Rollup on the downleveled ES5 to produce a UMD bundle
8799
*/
88-
export async function createUmdBundles({packages}: Config) {
89-
await mapPackages(packages, async (pkg) => {
100+
export async function createUmdBundles(config: Config) {
101+
await mapPackages(util.getAllPackages(config), async (pkg) => {
102+
const topLevelName = util.getTopLevelName(pkg);
103+
const destinationName = util.getDestinationName(pkg);
104+
90105
const rollupArgs = [
91106
`-c ./modules/${pkg}/rollup.config.js`,
92107
`--sourcemap`,
93108
];
94109

95110
await util.exec('rollup', rollupArgs);
96-
await util.mapSources(`./dist/${pkg}/bundles/${pkg}.umd.js`);
111+
await util.mapSources(`./dist/${topLevelName}/bundles/${destinationName}.umd.js`);
97112
});
98113
}
99114

@@ -103,7 +118,7 @@ export async function createUmdBundles({packages}: Config) {
103118
* leaving any type definition files in place
104119
*/
105120
export async function cleanTypeScriptFiles(config: Config) {
106-
const tsFilesGlob = './dist/packages/**/*.js';
121+
const tsFilesGlob = './dist/packages/**/*.ts';
107122
const dtsFilesFlob = './dist/packages/**/*.d.ts';
108123
const filesToRemove = await util.getListOfFiles(tsFilesGlob, dtsFilesFlob);
109124

@@ -117,12 +132,14 @@ export async function cleanTypeScriptFiles(config: Config) {
117132
* Renames the index files in each package to the name
118133
* of the package.
119134
*/
120-
export async function renamePackageEntryFiles({packages}: Config) {
121-
await mapPackages(packages, async (pkg) => {
135+
export async function renamePackageEntryFiles(config: Config) {
136+
await mapPackages(util.getAllPackages(config), async (pkg) => {
137+
const bottomLevelName = util.getBottomLevelName(pkg);
138+
122139
const files = await util.getListOfFiles(`./dist/packages/${pkg}/index.**`);
123140

124141
for (let file of files) {
125-
const target = file.replace('index', pkg);
142+
const target = file.replace('index', bottomLevelName);
126143
util.copy(file, target);
127144
util.remove(file);
128145
}
@@ -133,7 +150,9 @@ export async function renamePackageEntryFiles({packages}: Config) {
133150
/**
134151
* Removes any remaining source map files from running NGC
135152
*/
136-
export async function removeRemainingSourceMapFiles({packages}: Config) {
153+
export async function removeRemainingSourceMapFiles(config: Config) {
154+
const packages = util.getTopLevelPackages(config);
155+
137156
await util.removeRecursively(`./dist/packages/?(${packages.join('|')})/**/*.map`);
138157
}
139158

@@ -142,7 +161,8 @@ export async function removeRemainingSourceMapFiles({packages}: Config) {
142161
* Copies the type definition files and NGC metadata files to
143162
* the root of the distribution
144163
*/
145-
export async function copyTypeDefinitionFiles({packages}: Config) {
164+
export async function copyTypeDefinitionFiles(config: Config) {
165+
const packages = util.getTopLevelPackages(config);
146166
const files = await util.getListOfFiles(`./dist/packages/?(${packages.join('|')})/**/*`);
147167

148168
for (let file of files) {
@@ -157,17 +177,19 @@ export async function copyTypeDefinitionFiles({packages}: Config) {
157177
/**
158178
* Creates minified copies of each UMD bundle
159179
*/
160-
export async function minifyUmdBundles({packages}: Config) {
180+
export async function minifyUmdBundles(config: Config) {
161181
const uglifyArgs = [
162182
'-c',
163183
'-m',
164184
'--screw-ie8',
165185
'--comments',
166186
];
167187

168-
await mapPackages(packages, async (pkg) => {
169-
const file = `./dist/${pkg}/bundles/${pkg}.umd.js`;
170-
const out = `./dist/${pkg}/bundles/${pkg}.umd.min.js`;
188+
await mapPackages(util.getAllPackages(config), async (pkg) => {
189+
const topLevelName = util.getTopLevelName(pkg);
190+
const destinationName = util.getDestinationName(pkg);
191+
const file = `./dist/${topLevelName}/bundles/${destinationName}.umd.js`;
192+
const out = `./dist/${topLevelName}/bundles/${destinationName}.umd.min.js`;
171193

172194
return util.exec('uglifyjs', [
173195
...uglifyArgs,
@@ -184,17 +206,29 @@ export async function minifyUmdBundles({packages}: Config) {
184206
* Copies the README.md, LICENSE, and package.json files into
185207
* each package
186208
*/
187-
export async function copyPackageDocs({packages}: Config) {
209+
export async function copyDocs(config: Config) {
210+
const packages = util.getTopLevelPackages(config);
211+
188212
for (let pkg of packages) {
189213
const source = `./modules/${pkg}`;
190214
const target = `./dist/${pkg}`;
191215

192-
util.copy(`${source}/package.json`, `${target}/package.json`);
193216
util.copy(`${source}/README.md`, `${target}/README.md`);
194217
util.copy('./LICENSE', `${target}/LICENSE`);
195218
}
196219
}
197220

221+
export async function copyPackageJsonFiles(config: Config) {
222+
const packages = util.getAllPackages(config);
223+
224+
for (let pkg of packages) {
225+
const source = `./modules/${pkg}`;
226+
const target = `./dist/${pkg}`;
227+
228+
util.copy(`${source}/package.json`, `${target}/package.json`);
229+
}
230+
}
231+
198232

199233
/**
200234
* Removes the packages folder
@@ -208,7 +242,7 @@ export async function removePackagesFolder(config: Config) {
208242
* Deploy build artifacts to repos
209243
*/
210244
export async function publishToRepo(config: Config) {
211-
for (let pkg of config.packages) {
245+
for (let pkg of util.getTopLevelPackages(config)) {
212246
const SOURCE_DIR = `./dist/${pkg}`;
213247
const REPO_URL = `git@github.com:ngrx/${pkg}-builds.git`;
214248
const REPO_DIR = `./tmp/${pkg}`;

build/util.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,48 @@ export function createBuilder(tasks: [ string, (config: Config) => Promise<any>
128128
}
129129
};
130130
}
131+
132+
export function flatMap<K, J>(list: K[], mapFn: (item: K) => J[]): J[] {
133+
return list.reduce(function (newList, nextItem) {
134+
return [ ...newList, ...mapFn(nextItem) ];
135+
}, [] as J[]);
136+
}
137+
138+
export function getTopLevelPackages(config: Config) {
139+
return config.packages.map(packageDescription => packageDescription.name);
140+
}
141+
142+
export function getTestingPackages(config: Config) {
143+
return flatMap(config.packages, ({ name, hasTestingModule }) => {
144+
if (hasTestingModule) {
145+
return [ `${name}/testing` ];
146+
}
147+
148+
return [ ];
149+
});
150+
}
151+
152+
export function getAllPackages(config: Config) {
153+
return flatMap(config.packages, packageDescription => {
154+
if (packageDescription.hasTestingModule) {
155+
return [
156+
packageDescription.name,
157+
`${packageDescription.name}/testing`,
158+
];
159+
}
160+
161+
return [ packageDescription.name ];
162+
});
163+
}
164+
165+
export function getDestinationName(packageName: string) {
166+
return packageName.replace('/testing', '-testing');
167+
}
168+
169+
export function getTopLevelName(packageName: string) {
170+
return packageName.replace('/testing', '');
171+
}
172+
173+
export function getBottomLevelName(packageName: string) {
174+
return packageName.includes('/testing') ? 'testing' : packageName;
175+
}

modules/effects/testing/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@ngrx/effects/testing
2+
=======
3+
4+
The sources for this package are in the main [ngrx/platform](https://github.com/ngrx/platform) repo. Please file issues and pull requests against that repo.
5+
6+
License: MIT

modules/effects/testing/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './src/testing';

modules/effects/testing/package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "@ngrx/effects/testing",
3+
"typings": "../testing.d.ts",
4+
"main": "../bundles/effects-testing.umd.js",
5+
"module": "../@ngrx/effects/testing.es5.js",
6+
"es2015": "../@ngrx/effects/testing.js"
7+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export default {
2+
entry: './dist/effects/@ngrx/effects/testing.es5.js',
3+
dest: './dist/effects/bundles/effects-testing.umd.js',
4+
format: 'umd',
5+
exports: 'named',
6+
moduleName: 'ngrx.effects.testing',
7+
globals: {
8+
'@angular/core': 'ng.core',
9+
'@ngrx/effects': 'ngrx.effects',
10+
'rxjs/Observable': 'Rx',
11+
'rxjs/observable/defer': 'Rx.Observable.defer',
12+
}
13+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { Provider } from '@angular/core';
2+
import { Actions } from '@ngrx/effects';
3+
import { Observable } from 'rxjs/Observable';
4+
import { defer } from 'rxjs/observable/defer';
5+
6+
7+
export function provideMockActions(source: Observable<any>): Provider;
8+
export function provideMockActions(factory: () => Observable<any>): Provider;
9+
export function provideMockActions(factoryOrSource: (() => Observable<any>) | Observable<any>): Provider {
10+
return {
11+
provide: Actions,
12+
useFactory: (): Observable<any> => {
13+
if (typeof factoryOrSource === 'function') {
14+
return defer(factoryOrSource);
15+
}
16+
17+
return factoryOrSource;
18+
},
19+
};
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"extends": "../tsconfig-build",
3+
"compilerOptions": {
4+
"paths": {
5+
"@ngrx/store": ["../../dist/packages/store"],
6+
"@ngrx/effects": ["../../dist/packages/effects"]
7+
}
8+
},
9+
"files": [
10+
"index.ts"
11+
],
12+
"angularCompilerOptions": {
13+
"strictMetadataEmit": true
14+
}
15+
}

0 commit comments

Comments
 (0)