expressCart/test/specs/products.js

180 lines
5.2 KiB
JavaScript
Raw Normal View History

2019-11-09 12:14:56 +10:00
import{ serial as test }from'ava';
const {
runBefore,
g
} = require('../helper');
test.before(async () => {
await runBefore();
});
test('[Success] Get products JSON', async t => {
const res = await g.request
.get('?json=true')
.expect(200);
if(res.body.length < g.config.productsPerPage){
t.is(res.body.length, g.products.length);
}else{
t.is(res.body.length, g.config.productsPerPage);
}
});
test('[Success] Search products', async t => {
const res = await g.request
.get('/category/backpack?json=true')
.expect(200);
// Should be two backpack products
t.deepEqual(res.body.length, 2);
});
2019-11-09 12:59:20 +10:00
test('[Success] Filter products', async t => {
const res = await g.request
.get('/admin/products/filter/backpack')
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Should be two backpack products
t.deepEqual(res.body.length, 2);
});
test('[Success] Edit a product', async t => {
const res = await g.request
.get(`/admin/product/edit/${g.products[0]._id}`)
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Products should match
t.deepEqual(res.body._id.toString(), g.products[0]._id.toString());
t.deepEqual(res.body.productPermalink, g.products[0].productPermalink);
});
test('[Fail] Edit an invalid product', async t => {
const res = await g.request
.get('/admin/product/edit/some_invalid_product')
.set('apiKey', g.users[0].apiKey)
.expect(400);
// Check the returned message
t.deepEqual(res.body.message, 'Product not found');
});
2019-11-09 12:14:56 +10:00
test('[Success] Add a product', async t => {
const product = {
productPermalink: 'test-jacket',
productTitle: 'Test Jacket',
productPrice: '100.00',
productDescription: 'Test product description used to describe the product',
2019-11-09 12:14:56 +10:00
productPublished: true,
productTags: 'organic, jacket',
productOptions: {
Size: {
optName: 'Size',
optLabel: 'Select size',
optType: 'select',
optOptions: ['S', 'M', 'L', 'XL']
}
},
2019-12-16 12:29:20 +10:00
productComment: false,
2019-11-09 12:14:56 +10:00
productStock: 50
};
const res = await g.request
.post('/admin/product/insert')
.send(product)
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Check the returned message
t.deepEqual(res.body.message, 'New product successfully created');
});
2019-12-13 21:06:38 +10:00
test('[Fail] Add a product - Duplicate permalink', async t => {
const product = {
productPermalink: 'test-jacket',
productTitle: 'Test Jacket - blue',
productPrice: '100.00',
productDescription: 'Test product description used to describe the product',
2019-12-13 21:06:38 +10:00
productPublished: true,
productTags: 'organic, jacket, blue',
productOptions: {
Size: {
optName: 'Size',
optLabel: 'Select size',
optType: 'select',
optOptions: ['S', 'M', 'L', 'XL']
}
},
2019-12-16 12:29:20 +10:00
productComment: false,
2019-12-13 21:06:38 +10:00
productStock: 50
};
const res = await g.request
.post('/admin/product/insert')
.send(product)
.set('apiKey', g.users[0].apiKey)
.expect(400);
// Check the returned message
t.deepEqual(res.body.message, 'Permalink already exists. Pick a new one.');
});
2019-11-09 12:14:56 +10:00
test('[Success] Update a product', async t => {
const product = {
productId: g.products[0]._id,
productTitle: 'Test Jacket',
productPrice: '200.00',
productDescription: 'Test product description used to describe the product',
2019-11-09 12:14:56 +10:00
productPublished: true,
productTags: 'organic, jacket',
productOptions: {
Size: {
optName: 'Size',
optLabel: 'Select size',
optType: 'select',
optOptions: ['S', 'M', 'L', 'XL']
}
},
2019-12-16 12:29:20 +10:00
productComment: true,
2019-11-09 12:14:56 +10:00
productStock: 50
};
const res = await g.request
.post('/admin/product/update')
.send(product)
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Check the returned message
t.deepEqual(res.body.message, 'Successfully saved');
t.deepEqual(res.body.product.productTitle, product.productTitle);
t.deepEqual(res.body.product.productPrice, product.productPrice);
});
2019-12-13 21:06:38 +10:00
test('[Fail] Update a product - Duplicate permalink', async t => {
const product = {
productId: g.products[0]._id,
productPermalink: 'test-jacket'
};
const res = await g.request
.post('/admin/product/update')
.send(product)
.set('apiKey', g.users[0].apiKey)
.expect(400);
// Check the returned message
t.deepEqual(res.body.message, 'Permalink already exists. Pick a new one.');
});
test('[Success] Delete a product', async t => {
const res = await g.request
.post('/admin/product/delete')
.send({ productId: g.products[0]._id })
.set('apiKey', g.users[0].apiKey)
.expect(200);
// Check the returned message
t.deepEqual(res.body.message, 'Product successfully deleted');
});