expressCart/test/helper.js

118 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-11-09 12:14:56 +10:00
const fs = require('fs');
const _ = require('lodash');
const session = require('supertest-session');
const app = require('../app.js');
const { getId, newId } = require('../lib/common');
2019-11-09 15:08:05 +10:00
const { runIndexing } = require('../lib/indexing');
2019-11-09 12:14:56 +10:00
// Get test data to compare in tests
const rawTestData = fs.readFileSync('./bin/testdata.json', 'utf-8');
const jsonData = JSON.parse(rawTestData);
// Setup some global DB objects for comparison
const g = {
db: {},
config: {},
products: {},
customers: {},
users: {},
request: null,
jsonData
};
const setup = (db) => {
return Promise.all([
db.cart.deleteMany({}, {}),
db.users.deleteMany({}, {}),
db.customers.deleteMany({}, {}),
db.products.deleteMany({}, {}),
db.orders.deleteMany({}, {})
])
.then(() => {
return Promise.all([
2019-11-09 15:08:05 +10:00
db.users.insertMany(addApiKey(jsonData.users)),
2019-11-09 12:14:56 +10:00
db.customers.insertMany(jsonData.customers),
db.products.insertMany(fixProductDates(jsonData.products))
]);
});
};
const runBefore = async () => {
// Create a session
g.request = session(app);
await new Promise(resolve => {
app.on('appStarted', async () => {
// Set some stuff now we have the app started
g.config = app.config;
g.db = app.db;
await setup(g.db);
// Get some data from DB to use in compares
g.products = await g.db.products.find({}).toArray();
g.customers = await g.db.customers.find({}).toArray();
// Insert orders using product ID's
_(jsonData.orders).each(async (order) => {
order.orderProducts.push({
productId: g.products[0]._id,
title: g.products[0].productTitle,
quantity: 1,
totalItemPrice: g.products[0].productPrice,
options: {
size: '7.5'
},
productImage: g.products[0].productImage,
productComment: null
});
order.orderDate = new Date();
await g.db.orders.insertOne(order);
});
// Add API keys for users
_(jsonData.users).each(async (user) => {
await g.db.users.updateOne({
_id: getId(user._id)
}, {
$set: {
apiKey: newId()
}
});
});
// Get the updated users
g.users = await g.db.users.find({}).toArray();
// Index everything
await runIndexing(app);
resolve();
});
});
};
2019-11-09 15:08:05 +10:00
const fixProductDates = (products) => {
let index = 0;
products.forEach(() => {
products[index].productAddedDate = new Date();
index++;
});
return products;
};
const addApiKey = (users) => {
let index = 0;
users.forEach(() => {
users[index].apiKey = newId();
index++;
});
return users;
};
2019-11-09 12:14:56 +10:00
module.exports = {
runBefore,
setup,
2019-11-09 15:08:05 +10:00
g,
fixProductDates
2019-11-09 12:14:56 +10:00
};