Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
feat(test): cover almost 100% of code
Browse files Browse the repository at this point in the history
  • Loading branch information
rosostolato committed Aug 15, 2018
1 parent 59b28fa commit 993011c
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 81 deletions.
4 changes: 2 additions & 2 deletions src/restModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export class RestModelBase<T> extends RestRoute {
}

delete(params?: HttpParams): Observable<any> {
return this.createModelHttpRequest('DELETE', params)
.pipe(map(response => this.makeRest<T>(HttpMethod.DELETE, response)));
return this.createModelHttpRequest('DELETE', params);
}

getPlain(): T {
Expand All @@ -48,6 +47,7 @@ export class RestModelBase<T> extends RestRoute {
const methods = [
'delete',
'getBaseUrl',
'createModelHttpRequest',
'getDefaultHeaders',
'getFullPath',
'getPlain',
Expand Down
76 changes: 52 additions & 24 deletions test/restApi.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { TestBed, inject } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { TestBed, inject, async } from '@angular/core/testing';
import { HttpClientModule, HttpClient, HttpRequest, HttpEvent, HttpEventType } from '@angular/common/http';
import { RestApi } from '../demo/restApi.service';
import { RestBase, Restful } from '../src';
import { expect } from 'chai';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

describe('RestBase extend', () => {
let restApi: RestApi;
let httpClient: HttpClient;

beforeEach(() => {
TestBed.configureTestingModule({
Expand All @@ -13,37 +17,61 @@ describe('RestBase extend', () => {
});

restApi = TestBed.get(RestApi);
httpClient = TestBed.get(HttpClient);
});

it('should get service', inject([RestApi], (service: RestApi) => {
expect(restApi).to.be.equal(service);
}));

// it('should have prototypes', () => {
// const proto = Object.getPrototypeOf(restApi);
// const protoKeys = Object.getOwnPropertyNames(proto);
// const expected = [
// 'constructor',
// 'getBaseUrl',
// 'getDefaultHeaders',
// 'requestInterceptor',
// 'mapModel',
// 'route'
// ];

// expect(protoKeys).to.include.members(expected);
// });

it('should get URL base', () => {
const url = restApi.getBaseUrl();
expect(url).to.be.equal('https://jsonplaceholder.typicode.com');
});

// it('should say hello world', () => {
// const fixture: ComponentFixture<HelloWorldComponent> = TestBed.createComponent(HelloWorldComponent);
// fixture.detectChanges();
// expect(fixture.nativeElement.innerHTML.trim()).to.equal(
// 'Hello world from the Ngx Restmodel module!'
// );
// });
it('should intercept', async(() => {
@Restful({
baseUrl: 'https://jsonplaceholder.typicode.com/',
headers: { 'content-type': 'application/json' }
})
class MockRestApi extends RestBase {
constructor(http: HttpClient) {
super(http)
}

requestInterceptor(req: HttpRequest<any>) {
expect(req).to.be.an.instanceof(HttpRequest);
expect(req.url).to.be.equal('https://jsonplaceholder.typicode.com/posts');
return req;
}

responseInterceptor(res: Observable<HttpEvent<any>>) {
expect(res).to.be.an.instanceof(Observable);

res.pipe(tap((data) => {
if (data.type === HttpEventType.Response) {
expect(data.body).to.be.an.instanceof(Array);
expect(data.body.length).to.be.greaterThan(0);
}
}));

return res;
}
}

const mock = new MockRestApi(httpClient);
mock.route('posts').getList().subscribe();
}));

it('should test restBase class', async(() => {
const mock = new RestBase(httpClient);

expect(mock.getBaseUrl()).to.be.equal('');

(mock as any).getBaseUrl = () => {
return 'https://jsonplaceholder.typicode.com';
};

mock.route('posts').getList().subscribe();
}));
});
60 changes: 53 additions & 7 deletions test/restModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { TestBed, async } from '@angular/core/testing';
import { HttpClientModule } from '@angular/common/http';
import { RestApi } from '../demo/restApi.service';
import { expect } from 'chai';
import { RestModel } from '../src';
import { RestModel, RestRoute } from '../src';
import { Post } from '../demo/models/Post';

describe('RestModel', () => {
Expand All @@ -28,11 +28,57 @@ describe('RestModel', () => {
});
}));

// it('should delete', async(() => {
// route.getOne<Post>(1)
// .subscribe(data => {
// expect(data).not.to.be.equal(null);
// expect(data.id).not.to.be.equal(null);
// });
it('should delete', async(() => {
post.delete().subscribe(res => {
expect(res).to.be.eql({});
});
}));

it('should route', async(() => {
const route = post.route('comments');

expect(route).to.be.an.instanceof(RestRoute);
expect(route['_path']).to.be.equal('comments');

route.getList().subscribe(data => {
expect(data).to.be.an.instanceof(Array);
expect(data.length).to.be.greaterThan(0);
});
}));

// There is no a third level on this test web api
//
// it('should route twice', async(() => {
// const route = post.route('comments');

// expect(route).to.be.an.instanceof(RestRoute);
// expect(route['_path']).to.be.equal('comments');

// route.getList().subscribe(data => {
// expect(data).to.be.an.instanceof(Array);
// expect(data.length).to.be.greaterThan(0);
// });
// }));

it('should get plain', () => {
const plain = post.getPlain();
const proto = Object.getPrototypeOf(plain);

const methods = [
'delete',
'getBaseUrl',
'createModelHttpRequest',
'getDefaultHeaders',
'getFullPath',
'getPlain',
'put',
'route'
];

for (const pk of proto) {
for (const mk of methods) {
expect(pk).not.to.be.equal(mk);
}
}
});
});
48 changes: 0 additions & 48 deletions test/restRoute.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,6 @@ describe('RestRoute', () => {
route = restApi.route('posts');
});

// it('should have prototypes', () => {
// const proto = Object.getPrototypeOf(route);
// const protoKeys = Object.getOwnPropertyNames(proto);
// const expected = [
// 'constructor',
// 'getList',
// 'getOne',
// 'post',
// 'makeRest',
// 'makeRestCollection',
// 'getFullPath',
// 'getBaseUrl',
// 'getDefaultHeaders',
// 'requestInterceptor',
// 'mapModel'
// ];

// const notExpected = [
// 'route'
// ];

// expect(protoKeys).to.include.members(expected);
// expect(protoKeys).not.to.include.members(notExpected);
// });

it('should get list', async(() => {
route.getList<Post>()
.subscribe(data => {
Expand Down Expand Up @@ -72,27 +47,4 @@ describe('RestRoute', () => {
expect(data.id).not.to.be.equal(null);
});
}));

// it('should have route props', async(() => {
// expect(route).to.haveOwnProperty('_route');

// let mock: Route = {
// path: 'posts',
// parent: { path: '' }
// };
// expect((route as any)._route).to.be.eql(mock);

// // mock again
// mock.id = 1;
// mock = {
// path: 'comments',
// parent: mock
// };

// route.getOne(1).subscribe(post =>
// post.route('comments').getList().subscribe(data => {
// expect(data).to.haveOwnProperty('_route');
// expect((data as any)._route).to.be.eql(mock);
// }));
// }));
});

0 comments on commit 993011c

Please sign in to comment.