129 lines
3.8 KiB
JavaScript
129 lines
3.8 KiB
JavaScript
const models = require('vn-loopback/server/server').models;
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
describe('upsertFixedPrice()', () => {
|
|
const now = Date.vnNew();
|
|
const fixedPriceId = 1;
|
|
let originalFixedPrice;
|
|
|
|
beforeAll(async() => {
|
|
originalFixedPrice = await models.FixedPrice.findById(fixedPriceId);
|
|
const activeCtx = {
|
|
accessToken: {userId: 9},
|
|
http: {
|
|
req: {
|
|
headers: {origin: 'http://localhost'}
|
|
}
|
|
}
|
|
};
|
|
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
|
|
active: activeCtx
|
|
});
|
|
});
|
|
|
|
it(`should toggle the hasMinPrice boolean if there's a minPrice and update the rest of the data`, async() => {
|
|
const tx = await models.FixedPrice.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
const ctx = {args: {
|
|
id: fixedPriceId,
|
|
itemFk: originalFixedPrice.itemFk,
|
|
warehouseFk: 1,
|
|
rate2: 100,
|
|
rate3: 300,
|
|
started: now,
|
|
ended: now,
|
|
minPrice: 100,
|
|
hasMinPrice: false
|
|
}};
|
|
|
|
const result = await models.FixedPrice.upsertFixedPrice(ctx, options);
|
|
|
|
delete ctx.args.started;
|
|
delete ctx.args.ended;
|
|
ctx.args.hasMinPrice = false;
|
|
|
|
expect(result).toEqual(jasmine.objectContaining(ctx.args));
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it(`should toggle the hasMinPrice boolean if there's no minPrice and update the rest of the data`, async() => {
|
|
const tx = await models.FixedPrice.beginTransaction({});
|
|
|
|
try {
|
|
const options = {transaction: tx};
|
|
const ctx = {args: {
|
|
id: fixedPriceId,
|
|
itemFk: originalFixedPrice.itemFk,
|
|
warehouseFk: 1,
|
|
rate2: 2.5,
|
|
rate3: 2,
|
|
started: now,
|
|
ended: now,
|
|
minPrice: 0,
|
|
hasMinPrice: true
|
|
}};
|
|
|
|
const result = await models.FixedPrice.upsertFixedPrice(ctx, options);
|
|
|
|
delete ctx.args.started;
|
|
delete ctx.args.ended;
|
|
ctx.args.hasMinPrice = true;
|
|
|
|
expect(result).toEqual(jasmine.objectContaining(ctx.args));
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
|
|
it(`should recalculate rate2 if change rate3`, async() => {
|
|
const tx = await models.FixedPrice.beginTransaction({});
|
|
|
|
const tomorrow = new Date(now);
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
|
|
const rate2 = 2;
|
|
const firstRate3 = 1;
|
|
const secondRate3 = 2;
|
|
try {
|
|
const options = {transaction: tx};
|
|
const ctx = {args: {
|
|
id: undefined,
|
|
itemFk: 1,
|
|
warehouseFk: 1,
|
|
started: tomorrow,
|
|
ended: tomorrow,
|
|
rate2: rate2,
|
|
rate3: firstRate3,
|
|
minPrice: 0,
|
|
hasMinPrice: true
|
|
}};
|
|
|
|
// create new fixed price
|
|
const newFixedPrice = await models.FixedPrice.upsertFixedPrice(ctx, options);
|
|
|
|
// change rate3 to same fixed price id
|
|
ctx.args.id = newFixedPrice.id;
|
|
ctx.args.rate3 = secondRate3;
|
|
|
|
const result = await models.FixedPrice.upsertFixedPrice(ctx, options);
|
|
|
|
expect(result.rate2).not.toEqual(rate2);
|
|
|
|
await tx.rollback();
|
|
} catch (e) {
|
|
await tx.rollback();
|
|
throw e;
|
|
}
|
|
});
|
|
});
|