Server service tests

This commit is contained in:
ziajka 2018-07-05 12:56:46 +02:00
parent 9888b6db74
commit bcc21da585
2 changed files with 106 additions and 7 deletions

View File

@ -1,7 +1,9 @@
import { TestBed, inject } from '@angular/core/testing';
import { TestBed } from '@angular/core/testing';
import { ServerService } from './server.service';
import { Server } from "../models/server";
import { IndexedDbService } from "./indexed-db.service";
import { AngularIndexedDB } from "angular2-indexeddb";
export class MockedServerService {
@ -22,13 +24,110 @@ export class MockedServerService {
describe('ServerService', () => {
let indexedDbService: IndexedDbService;
let db: AngularIndexedDB;
let service: ServerService;
beforeEach(() => {
indexedDbService = new IndexedDbService();
db = indexedDbService.get();
spyOn(db, 'openDatabase').and.returnValue(Promise.resolve(true));
TestBed.configureTestingModule({
providers: [ ServerService ]
providers: [
ServerService,
{ provide: IndexedDbService, useValue: indexedDbService}
]
});
service = TestBed.get(ServerService);
});
it('should be created and create database', () => {
expect(service).toBeTruthy();
expect(db.openDatabase).toHaveBeenCalled();
expect(db.openDatabase.calls.first().args[0]).toEqual(1);
const evnt = {
currentTarget: {
result: {
createObjectStore: function () {}
}
}
};
spyOn(evnt.currentTarget.result, 'createObjectStore');
const upgradeCallback = db.openDatabase.calls.first().args[1];
upgradeCallback(evnt);
expect(evnt.currentTarget.result.createObjectStore).toHaveBeenCalledWith( 'servers', { keyPath: 'id', autoIncrement: true });
});
describe('operations on records', () => {
let record: any;
beforeEach(() => {
record = new Server();
record.name = 'test';
});
it('should get an object', (done) => {
spyOn(db, 'getByKey').and.returnValue(Promise.resolve([record]));
service.get(1).then((result) => {
expect(db.getByKey).toHaveBeenCalledWith('servers', 1);
expect(result).toEqual([record]);
done();
});
});
it('should create an object', (done) => {
const created = {
'key': 99
};
spyOn(db, 'add').and.returnValue(Promise.resolve(created));
service.create(record).then((result) => {
expect(db.add).toHaveBeenCalledWith('servers', record);
expect(result.id).toEqual(99);
done();
});
});
it('should update an object', (done) => {
spyOn(db, 'update').and.returnValue(Promise.resolve(record));
service.update(record).then((result) => {
expect(db.update).toHaveBeenCalledWith('servers', record);
expect(result).toEqual(record);
done();
});
});
it('should delete an object', (done) => {
record.id = 88;
spyOn(db, 'delete').and.returnValue(Promise.resolve());
service.delete(record).then(() => {
expect(db.delete).toHaveBeenCalledWith('servers', record.id);
done();
});
});
});
it('should call findAll', (done) => {
spyOn(db, 'getAll').and.returnValue(Promise.resolve(true));
service.findAll().then((result) => {
expect(result).toEqual(true);
expect(db.getAll).toHaveBeenCalledWith('servers');
done();
});
});
// it('should be created', inject([ServerService], (service: ServerService) => {
// expect(service).toBeTruthy();
// }));
});

View File

@ -12,7 +12,7 @@ export class ServerService {
constructor(private indexedDbService: IndexedDbService) {
this.ready = indexedDbService.get().openDatabase(1, (evt) => {
const store = evt.currentTarget.result.createObjectStore(
evt.currentTarget.result.createObjectStore(
this.tablename, { keyPath: "id", autoIncrement: true });
});
}
@ -81,7 +81,7 @@ export class ServerService {
return promise;
}
private onReady(query) {
protected onReady(query) {
const promise = new Promise((resolve, reject) => {
this.ready.then(() => {
query()