salix/modules/item/back/methods/fixed-price/specs/upsertFixedPrice.spec.js

129 lines
3.8 KiB
JavaScript
Raw Normal View History

2021-07-12 10:54:59 +00:00
const models = require('vn-loopback/server/server').models;
2023-03-23 09:05:02 +00:00
const LoopBackContext = require('loopback-context');
2021-01-18 13:16:39 +00:00
describe('upsertFixedPrice()', () => {
2023-01-16 14:18:24 +00:00
const now = Date.vnNew();
2021-01-18 13:16:39 +00:00
const fixedPriceId = 1;
let originalFixedPrice;
beforeAll(async() => {
2021-07-12 10:54:59 +00:00
originalFixedPrice = await models.FixedPrice.findById(fixedPriceId);
2023-03-23 09:05:02 +00:00
const activeCtx = {
accessToken: {userId: 9},
http: {
req: {
headers: {origin: 'http://localhost'}
}
}
};
spyOn(LoopBackContext, 'getCurrentContext').and.returnValue({
active: activeCtx
});
2021-01-18 13:16:39 +00:00
});
it(`should toggle the hasMinPrice boolean if there's a minPrice and update the rest of the data`, async() => {
2021-07-12 10:54:59 +00:00
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;
2023-04-05 05:54:56 +00:00
ctx.args.hasMinPrice = false;
2021-07-12 10:54:59 +00:00
expect(result).toEqual(jasmine.objectContaining(ctx.args));
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2021-01-18 13:16:39 +00:00
});
it(`should toggle the hasMinPrice boolean if there's no minPrice and update the rest of the data`, async() => {
2021-07-12 10:54:59 +00:00
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;
2023-04-05 05:54:56 +00:00
ctx.args.hasMinPrice = true;
2021-07-12 10:54:59 +00:00
expect(result).toEqual(jasmine.objectContaining(ctx.args));
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
2021-01-18 13:16:39 +00:00
});
2023-01-31 10:17:31 +00:00
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,
2023-04-05 05:54:56 +00:00
hasMinPrice: true
2023-01-31 10:17:31 +00:00
}};
// 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;
}
});
2021-01-18 13:16:39 +00:00
});