import { v4 } from 'uuid'; import { AggregateProduct } from '../AggregateProduct'; import { AggregateProductVariant } from '../AggregateProduct'; import { RetailerProduct } from '../RetailerProduct'; import { RetailerProductVariant } from '../RetailerProduct/RetailerProductVariant'; import RetailerProductVariantUpdateHistory from './RetailerProductVariantUpdateHistory'; describe('RetailerProductVariantUpdateHistory trigger', () => { const productId = v4(); let aggregateProducts; let retailerProduct: RetailerProduct; let aggregateProductVariant: AggregateProductVariant; beforeEach(async () => { aggregateProducts = await AggregateProduct.create({ id: v4(), identifiers: ['test'], discoverable: true, asin: 'test', price: 10, }).save({ listeners: false }); retailerProduct = await RetailerProduct.create({ id: productId, name: 'test', price: 1, retailer: 'test', url: 'test', aggregateProductId: aggregateProducts.id, }).save({ listeners: false }); aggregateProductVariant = await AggregateProductVariant.create({ id: v4(), price: 5, productId: aggregateProducts.id, }).save({ listeners: false }); }); test('creating a retailer product variant should create a retailer product variant update history', async () => { const historyCount = await RetailerProductVariantUpdateHistory.count(); expect(historyCount).toBe(0); const retailerProductVariant = await RetailerProductVariant.create({ productId: retailerProduct.id, variantId: aggregateProductVariant.id, price: 10, url: 'https://example.com', retailer: 'amazon', }).save({ listeners: false }); const historyCountAfterSave = await RetailerProductVariantUpdateHistory.count(); expect(historyCountAfterSave).toBe(1); const history = await RetailerProductVariantUpdateHistory.findOneOrFail({ where: { retailerProductVariant }, }); expect(history.price).toBe(retailerProductVariant.price); }); test('update the price of a retailer product variant should create a retailer product variant update history', async () => { const retailerProductVariant = await RetailerProductVariant.create({ productId: retailerProduct.id, variantId: aggregateProductVariant.id, price: 10, url: 'https://example.com', retailer: 'amazon', }).save({ listeners: false }); retailerProductVariant.price = 20; await retailerProductVariant.save(); const historyCount = await RetailerProductVariantUpdateHistory.count(); expect(historyCount).toBe(2); const history = await RetailerProductVariantUpdateHistory.find({ where: { retailerProductVariant }, }); expect(history[0].price).toBe(10); expect(history[1].price).toBe(20); }); test('updating anything else besides price should not create a retailer product variant update history', async () => { const retailerProductVariant = await RetailerProductVariant.create({ productId: retailerProduct.id, variantId: aggregateProductVariant.id, price: 10, url: 'https://example.com', retailer: 'amazon', }).save({ listeners: false }); retailerProductVariant.retailer = 'google'; await retailerProductVariant.save(); const historyCount = await RetailerProductVariantUpdateHistory.count(); expect(historyCount).toBe(1); }); });