Skip to content

Commit e380383

Browse files
committed
rewrite mapreduce upsert tests with async await
1 parent 1d7c935 commit e380383

1 file changed

Lines changed: 67 additions & 49 deletions

File tree

tests/unit/test.mapreduce.js

Lines changed: 67 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,63 +6,81 @@ var upsert = PouchDB.utils.upsert;
66
var utils = PouchDB.utils.mapReduceUtils;
77

88
describe('test.mapreduce.js-upsert', function () {
9-
it('should throw an error if the doc errors', function () {
10-
return upsert({
11-
get: function () {
12-
return Promise.reject(new Error('a fake error!'));
13-
}
14-
}, 'foo')
15-
.then(function () {
9+
it('should throw an error if the doc errors', async function () {
10+
try {
11+
await upsert(
12+
{
13+
get: function () {
14+
return Promise.reject(new Error('a fake error!'));
15+
},
16+
},
17+
'foo',
18+
);
19+
1620
should.fail("Expected promise to be rejected");
17-
})
18-
.catch(function (err) {
21+
} catch (err) {
1922
err.message.should.equal("a fake error!");
20-
});
21-
});
22-
it('should fulfill if the diff returns false', function () {
23-
return upsert({
24-
get: function () {
25-
return Promise.resolve({ _rev: 'xyz' });
26-
}
27-
}, 'foo', function () {
28-
return false;
29-
}).then(function (res) {
30-
res.updated.should.equal(false);
31-
res.rev.should.equal('xyz');
32-
});
23+
}
3324
});
34-
it('should put if get throws 404', function () {
35-
return upsert({
36-
get: function () {
37-
return Promise.reject({ status: 404 });
25+
26+
it('should fulfill if the diff returns false', async function () {
27+
const res = await upsert(
28+
{
29+
get: function () {
30+
return Promise.resolve({ _rev: 'xyz' });
31+
},
3832
},
39-
put: function () {
40-
return Promise.resolve({ rev: 'abc' });
41-
}
42-
}, 'foo', function () {
43-
return { difference: "something" };
44-
}).then(function (res) {
45-
res.updated.should.equal(true);
46-
res.rev.should.equal('abc');
47-
});
33+
'foo',
34+
function () {
35+
return false;
36+
},
37+
);
38+
39+
res.updated.should.equal(false);
40+
res.rev.should.equal('xyz');
4841
});
49-
it('should error if it can\'t put', function () {
50-
return upsert({
51-
get: function () {
52-
return Promise.resolve({ _rev: 'xyz' });
42+
43+
it('should put if get throws 404', async function () {
44+
const res = await upsert(
45+
{
46+
get: function () {
47+
return Promise.reject({ status: 404 });
48+
},
49+
put: function () {
50+
return Promise.resolve({ rev: 'abc' });
51+
},
52+
},
53+
'foo',
54+
function () {
55+
return { difference: "something" };
5356
},
54-
put: function () {
55-
return Promise.reject(new Error('falala'));
56-
}
57-
}, 'foo', function () {
58-
return { difference: "something" };
59-
})
60-
.then(function () {
57+
);
58+
59+
res.updated.should.equal(true);
60+
res.rev.should.equal('abc');
61+
});
62+
63+
it('should error if it can\'t put', async function () {
64+
try {
65+
await upsert(
66+
{
67+
get: function () {
68+
return Promise.resolve({ _rev: 'xyz' });
69+
},
70+
put: function () {
71+
return Promise.reject(new Error('falala'));
72+
},
73+
},
74+
'foo',
75+
function () {
76+
return { difference: "something" };
77+
},
78+
);
79+
6180
should.fail("Expected promise to be rejected");
62-
})
63-
.catch(function (err) {
81+
} catch (err) {
6482
err.message.should.equal("falala");
65-
});
83+
}
6684
});
6785
});
6886

0 commit comments

Comments
 (0)