Skip to content
This repository was archived by the owner on Feb 26, 2024. It is now read-only.

Commit cb4cb82

Browse files
committed
feat: add tests
- refactor to support tests - correct bugs and clarify choices as result of test - add new configuration options - POST commands/resetDb` passes the request to the `resetDb` method - when HTTP method interceptor returns null/undefined, continue with default request processing. - reorganize files into src/app and src/in-mem - adjust gulp tasks accordingly closes #123. closes #128. closes #120.
1 parent 70b412d commit cb4cb82

26 files changed

+959
-156
lines changed

.gitignore

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@ aot
22
in-memory-web-api
33
node_modules
44
typings
5-
src/*.d.ts
6-
src/*.js
7-
src/*.js.map
8-
src/*.metadata.json
9-
examples/*.d.ts
10-
examples/*.js
11-
examples/*.js.map
12-
examples/*.metadata.json
5+
npm-debug.log
6+
src/**/*.d.ts
7+
src/**/*.js
8+
src/**/*.js.map
9+
src/**/*.metadata.json
1310
yarn.lock

.npmignore

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
.editorconfig
2+
.gitignore
3+
.npmignore
4+
.travis.yml
15
aot
2-
examples
6+
gulpfile.js
37
in-memory-web-api
8+
karma-test-shim.js
9+
karma.conf.js
10+
rollup.config.js
411
src
5-
gulpfile.js
6-
.gitignore
7-
.npmignore
812
tsconfig.json
913
tslint.json
10-
.editorconfig
11-
rollup.config.js
12-
.travis.yml
1314
yarn.lock

CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,25 @@ because this is a development tool, not a production product.
88
We do try to tell you about such changes in this `CHANGELOG.md`
99

1010
and we fix bugs as fast as we can.
11+
12+
<a id="0.4.0"></a>
13+
## 0.4.0 (2017-09-04)
14+
See PR #130.
15+
* Added support for `HttpClient`
16+
* Added tests
17+
* refactor existing code to support tests
18+
* correct bugs and clarify choices as result of test
19+
* add some configuration options
20+
- dataEncapsulation (issue #112, pr#123)
21+
- post409
22+
- put404b
23+
* `POST commands/resetDb` passes the request to your `resetDb` method
24+
so you can optionally reset the database dynamically
25+
to arbitrary initial states (issue #128)
26+
* when HTTP method interceptor returns null/undefined, continue with service's default processing (pr #120)
27+
* reorganize files into src/app and src/in-mem
28+
* adjust gulp tasks accordingly
29+
1130
<a id="0.3.2"></a>
1231
## 0.3.2 (2017-05-02)
1332
* Bug fixes PRs #91, 95, 106

README.md

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,18 @@ import { InMemoryDbService } from 'angular-in-memory-web-api';
7474
export class InMemHeroService implements InMemoryDbService {
7575
createDb() {
7676
let heroes = [
77-
{ id: '1', name: 'Windstorm' },
78-
{ id: '2', name: 'Bombasto' },
79-
{ id: '3', name: 'Magneta' },
80-
{ id: '4', name: 'Tornado' }
77+
{ id: 1, name: 'Windstorm' },
78+
{ id: 2, name: 'Bombasto' },
79+
{ id: 3, name: 'Magneta' },
80+
{ id: 4, name: 'Tornado' }
8181
];
8282
return {heroes};
8383
}
8484
}
8585
```
8686

87+
>This library _currently_ assumes that every collection has a primary key called `id`.
88+
8789
Register this module and your service implementation in `AppModule.imports`
8890
calling the `forRoot` static method with this service class and optional configuration object:
8991
```ts
@@ -222,7 +224,15 @@ The `InMemoryDbService` method name must be the same as the HTTP method name but
222224
This service calls it with an `HttpMethodInterceptorArgs` object.
223225
For example, your HTTP GET interceptor would be called like this:
224226
e.g., `yourInMemDbService["get"](interceptorArgs)`.
225-
Your method must **return an `Observable<Response>`** which _should be "cold"_.
227+
228+
Your method must return either:
229+
230+
* `Observable<Response>` - your code has handled the request and the response is available from this
231+
observable. It _should be "cold"_.
232+
233+
* `null`/`undefined` - your code decided not to intervene,
234+
perhaps because you wish to intercept only certain paths for the given HTTP method.
235+
The service continues with its default processing of the HTTP request.
226236

227237
The `HttpMethodInterceptorArgs` (as of this writing) are:
228238
```ts
@@ -233,25 +243,23 @@ passThruBackend: ConnectionBackend; // pass through backend, if it exists
233243
```
234244
## Examples
235245

236-
The file `examples/hero-data.service.ts` is an example of a Hero-oriented `InMemoryDbService`,
237-
derived from the [HTTP Client](https://angular.io/docs/ts/latest/guide/server-communication.html)
238-
sample in the Angular documentation.
246+
The file `src/app/hero-in-mem-data.service.ts` is an example of a Hero-oriented `InMemoryDbService`,
247+
such as you might see in an HTTP sample in the Angular documentation.
239248

240249
To try it, add the following line to `AppModule.imports`
241250
```ts
242-
InMemoryWebApiModule.forRoot(HeroDataService)
251+
InMemoryWebApiModule.forRoot(HeroInMemDataService)
243252
```
244253

245-
That file also has a `HeroDataOverrideService` derived class that demonstrates overriding
246-
the `parseUrl` method and it has a "cold" HTTP GET interceptor.
254+
See the `src/app/hero-in-mem-data-override.service.ts` class that demonstrates overriding
255+
the `parseUrl` method. It also has a "cold" HTTP GET interceptor.
247256

248257
Add the following line to `AppModule.imports` to see this version of the data service in action:
249258
```ts
250-
InMemoryWebApiModule.forRoot(HeroDataOverrideService)
259+
InMemoryWebApiModule.forRoot(HeroInMemDataOverrideService)
251260
```
252261

253-
# To Do
254-
* add tests (shameful omission!)
262+
The tests (see below) exercise these examples.
255263

256264
# Build Instructions
257265

@@ -282,7 +290,7 @@ compiling your application project.
282290

283291
- `npm run tsc` to confirm the project compiles w/o error (sanity check)
284292

285-
-- NO TESTS YET ... BAD --
293+
- `npm test` to build and run tests (see "Testing" below)
286294

287295
- `gulp build`
288296
- commit and push
@@ -297,3 +305,26 @@ compiling your application project.
297305

298306
[travis-badge]: https://travis-ci.org/angular/in-memory-web-api.svg?branch=master
299307
[travis-badge-url]: https://travis-ci.org/angular/in-memory-web-api
308+
309+
## Testing
310+
311+
The "app" for this repo is not a real app.
312+
It's an Angular data service (`HeroService`) and a bunch of tests.
313+
314+
>Note that the `tsconfig.json` produces a `commonjs` module.
315+
That's what _Angular specs require_.
316+
But when building for an app, it should be a `es2015` module,
317+
as is the `tsconfig-ngc.json` for AOT-ready version of this library.
318+
319+
These tests are a work-in-progress, as tests often are.
320+
321+
The `src/` folder is divided into
322+
- `app/` - the test "app" and its tests
323+
- `in-mem/` - the source code for the in-memory web api library
324+
325+
>A real app would reference the in-memory web api node module;
326+
these tests reference the library source files.
327+
328+
The `karma-test-shim.js` add `in-mem` to the list of app folders that SystemJS should resolve.
329+
330+

gulpfile.js

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ var source = require('vinyl-source-stream');
88

99
var path = require("path");
1010

11-
var ngcOutput = './src/';
12-
var jsCopySrc = ['*.js', '*.js.map', '*.d.ts', '*.metadata.json'].map(ext => ngcOutput + ext);
11+
var inMemSrc = './src/in-mem/';
12+
var jsCopySrc = ['*.js', '*.js.map', '*.d.ts', '*.metadata.json'].map(ext => inMemSrc + ext);
1313

1414
gulp.task('default', ['help']);
1515

@@ -28,7 +28,7 @@ gulp.task('build', ['umd'], function(){
2828
});
2929

3030
gulp.task('ngc', ['clean'], function(done) {
31-
runNgc('./', done);
31+
runNgc('src/in-mem/', done);
3232
});
3333

3434
// Uses rollup-stream plugin https://www.npmjs.com/package/rollup-stream
@@ -38,9 +38,20 @@ gulp.task('umd', ['ngc'], function(done) {
3838
.pipe(gulp.dest('./bundles'));
3939
});
4040

41-
gulp.task('clean', function(done) {
42-
clean(['aot/**/*.*']);
43-
clean([ngcOutput+'*.js', '*.js.map', '*.d.ts', '!gulpfile.js', '*.metadata.json', './bundles/in-memory-web-api.umd.js'], done);
41+
gulp.task('clean', function() {
42+
return Promise.all([
43+
clean(['aot/**/*.*']),
44+
clean(['src/app/**.*js','src/**/*.js.map', 'src/**/*.d.ts','src/**/*.metadata.json']),
45+
clean(['src/in-mem/node_modules/**/*.*','src/in-mem/**.*js','src/**/*.ngsummary.json', 'src/**/*.ngfactory.ts']),
46+
clean([
47+
'./http-status-codes.*',
48+
'./in-memory-backend.service.*',
49+
'./in-memory-web-api.module.*',
50+
'./index.*',
51+
'./bundles/in-memory-web-api.umd.js'
52+
])
53+
])
54+
.then(() => console.log('Cleaned successfully'));
4455
});
4556

4657
/**
@@ -73,13 +84,12 @@ gulp.task('bump', function() {
7384
});
7485
//////////
7586

76-
function clean(path, done) {
87+
function clean(path) {
7788
log('Cleaning: ' + $.util.colors.blue(path));
78-
del(path, {dryRun:false})
89+
return del(path, {dryRun:false})
7990
.then(function(paths) {
8091
console.log('Deleted files and folders:\n', paths.join('\n'));
81-
})
82-
.then(done,done);
92+
});
8393
}
8494

8595
function log(msg) {
@@ -98,7 +108,7 @@ function runNgc(directory, done) {
98108
//var ngcjs = path.join(process.cwd(), 'node_modules/typescript/bin/tsc');
99109
//ngcjs = path.join(process.cwd(), 'node_modules/.bin/ngc');
100110
var ngcjs = './node_modules/@angular/compiler-cli/src/main.js';
101-
var childProcess = cp.spawn('node', [ngcjs, '-p', directory], { cwd: process.cwd() });
111+
var childProcess = cp.spawn('node', [ngcjs, '-p', './tsconfig-ngc.json'], { cwd: process.cwd() });
102112
childProcess.stdout.on('data', function (data) {
103113
console.log(data.toString());
104114
});

0 commit comments

Comments
 (0)