browser-test.ts
3.55 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
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());
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);
let replicator = ServiceLocator.getFileReplicator();
let index = 0;
replicator.clear();
replicator
.on('file-complete', () => {
index++;
})
.once('complete', () => {
expect(index).toEqual(3);
done();
});
localDb.replicate.from(dbUrl)
.once('complete', () => {
localDb.query('index_type/type',{
include_docs : true,
key : replicator.itemValue
}).then((res) => {
let docs = [];
for (let r of res.rows) {
docs.push(r.doc);
}
replicator.pushChanges(docs);
replicator.start();
});
});
});
it("Should trigger errors, but successfully download with retries", (done) => {
TestFileHandler.setErrorRate(0.8);
let replicator = ServiceLocator.getFileReplicator();
let errors = 0;
replicator.clear();
replicator
.on('file-error', () => {
errors++;
})
.once('complete', () => {
expect(errors).toBeGreaterThanOrEqual(1);
done();
});
localDb.replicate.from(dbUrl)
.once('complete', () => {
localDb.query('index_type/type',{
include_docs : true,
key : replicator.itemValue
}).then((res) => {
let docs = [];
for (let r of res.rows) {
docs.push(r.doc);
}
replicator.pushChanges(docs);
replicator.start();
});
});
});
});