browser-test.ts
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import PouchDB from 'pouchdb';
import {FileReplicator} from '../src/file-replicator';
import {ServiceLocator, ENV_UNKNOWN} from '../src/service-locator';
import {TestFileHandler} from './file-handler/test-file-handler';
ServiceLocator.addFileHandler(ENV_UNKNOWN, new TestFileHandler());
import '../src/browser-main';
declare var emit:any;
const dbUrl = 'http://admin:admin@localhost:5984/pouch_test_db';
const testDocs = [
{ 'type' : 'asset', 'source' : 'https://upload.wikimedia.org/wikipedia/commons/thumb/e/e1/FullMoon2010.jpg/292px-FullMoon2010.jpg' } ,
{ 'type' : 'asset', 'source' : 'https://upload.wikimedia.org/wikipedia/commons/thumb/2/2b/Jupiter_and_its_shrunken_Great_Red_Spot.jpg/260px-Jupiter_and_its_shrunken_Great_Red_Spot.jpg' } ,
{ 'type' : 'asset', 'source' : 'https://upload.wikimedia.org/wikipedia/commons/c/c7/Saturn_during_Equinox.jpg' }
];
describe("Integration tests with couchdb", () => {
let index = 1;
let remoteDb = new PouchDB(dbUrl);
let localDb;
beforeAll((done) => {
ServiceLocator.getFileReplicator().retryTimeout = 100;
remoteDb.bulkDocs(testDocs).then(() => {
done();
});
});
beforeEach(() => {
localDb = new PouchDB('testdb-' + index);
index++;
localDb.put({
_id : "_design/index_type",
views : {
type : {
map : function(doc) {
if (doc.type) { emit(doc.type); }
}.toString()
}
}
});
});
it("Should successfully download several files", (done) => {
TestFileHandler.setErrorRate(0);
ServiceLocator.getFileReplicator().init();
let index = 0;
localDb.replicate.from(dbUrl)
.on('file-replicator-complete', event => {
index++;
})
.on('complete', () => {
expect(index).toEqual(3);
done();
});
});
it("Should trigger errors, but successfully download with retries", (done) => {
TestFileHandler.setErrorRate(0.8);
ServiceLocator.getFileReplicator().init();
let errors = 0;
localDb.replicate.from(dbUrl)
.on('file-replicator-error', event => {
errors++;
})
.on('complete', () => {
expect(errors).toBeGreaterThanOrEqual(1);
done();
});
});
});