Hi
Im trying to test a service that uses the SqlStorage (Sqlite). The issue Im having is that the promises that are return by the service never get resolved. There isn’t an issue with the code as the service works as expected when used by the app.
Service
export class StoreService {
private storage: Storage = null;
// Init an empty DB if it does not exist by now!
constructor() {
this.storage = new Storage(SqlStorage, { name: 'l2d3ddatabase' });
}
insertTestResult(testResult: TestResult): Promise<any> {
let sql = `INSERT INTO testResult (navigationKey,resultPercent,testDate)
VALUES (?,?,?)`;
return this.storage.query(sql, [testResult.navigationKey, testResult.resultPercent, new Date().getTime()]);
}
getTestResults(navigationKey: string): Promise<TestResult[]> {
return new Promise(resolve => {
this.storage.query(`SELECT navigationKey,resultPercent,testDate
FROM testResult
WHERE navigationKey = '${navigationKey}'
ORDER BY testDate DESC`)
.then(data => {
let testResults = this.mapTestResults(data);
resolve(testResults);
});
});
}
private mapTestResults(data: any): TestResult[] {
let testResults = [];
if (data.res.rows.length > 0) {
for (var i = 0; i < data.res.rows.length; i++) {
let item = data.res.rows.item(i);
testResults.push(new TestResult(item.navigationKey, item.resultPercent, new Date(parseInt(item.testDate))));
}
}
return testResults;
}
}
Test
it('addTestResult: test result added', inject(
[StoreService],
fakeAsync((service: StoreService) => {
// Given
let testResult = new TestResult('nav.key', 75);
// When
let promise = service.insertTestResult(testResult);
// Then
promise.then(() => {
//!!! Promise never gets resolved ????
console.log('this doesnt get logged');
service.getTestResults(testResult.navigationKey)
.then(testResults => {
console.log(testResults);
// expect(testResults.length).toBe(1);
});
});
})
));