/
addToCart.test.js
52 lines (47 loc) · 1.49 KB
/
addToCart.test.js
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
'use strict';
const { describe, it, before, after } = require('mocha');
const assert = require('assert');
const { handler: addToCart } = require('../functions/addToCart');
const mongoose = require('mongoose');
describe('Add to Cart', function() {
before(async() => {
await mongoose.connect('mongodb://localhost:27017/netlify');
});
after(async() => {
await mongoose.disconnect();
});
it('Should create a cart and add a product to the cart.', async function() {
const params = {
body: {
cartId: null,
product: [
{ productId: '629e5f3686d82fc73b95b396', quantity: 2 },
{ productId: '629e5f3686d82fc73b95b398', quantity: 1 }
]
}
};
const result = await addToCart(params);
assert(result.body);
assert(result.body.items.length);
});
it('Should find the cart and add to it.', async function() {
const params = {
body: {
cartId: null,
product: [
{ productId: '629e5f3686d82fc73b95b396', quantity: 2 },
{ productId: '629e5f3686d82fc73b95b398', quantity: 1 }
]
}
};
const result = await addToCart(params);
assert(result.body);
assert(result.body.items.length);
params.body.cartId = result.body._id;
const findCart = await addToCart(params);
assert(findCart.body);
assert.deepStrictEqual(findCart.body._id, result.body._id);
assert.equal(findCart.body.items[0].quantity, 4);
assert.equal(findCart.body.items[1].quantity, 2);
});
});