import { getCustomRepository } from 'typeorm'; import { AggregateProduct, AggregateProductVariant, RetailerProduct, RetailerProductVariant, } from '@/entities'; import { RawGoogleShoppingOffersDataV1 } from '@/entities/RawGoogleShoppingOffers/types'; import * as algoliaUtils from '@/utils/algolia'; import RawGoogleShoppingOffersRepository from '../RawGoogleShoppingOffersRepository'; describe('RawGoogleShoppingOffersRepository', () => { let repo: RawGoogleShoppingOffersRepository; beforeEach(async () => { repo = getCustomRepository(RawGoogleShoppingOffersRepository); }); it('should not create RawGoogleShoppingOffers when there are no offers', async () => { const googleShoppingId = '7717270608456740797'; const ingestionID = 'test12'; const googleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [], }; expect( repo.create({ googleShoppingId, data: googleShoppingProduct, ingestionID, }) ).rejects.toMatchInlineSnapshot( `[Error: Cannot store google shopping offers with no offers.]` ); }); it('should not create RawGoogleShoppingOffers when there are invalid urls', async () => { const googleShoppingId = '7717270608456740797'; const ingestionID = 'test12'; const googleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [ { price: 100, url: '', }, ], }; expect( repo.create({ googleShoppingId, data: googleShoppingProduct, ingestionID, }) ).rejects.toMatchInlineSnapshot( `[Error: Could not get retailer from url: ]` ); }); it('should create RawGoogleShoppingOffers', async () => { const googleShoppingId = '7717270608456740797'; const ingestionID = 'test12'; const googleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [ { price: 100, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, ], }; const raw = await repo.create({ googleShoppingId, data: googleShoppingProduct, ingestionID, }); expect(raw).toMatchObject({ googleShoppingId: googleShoppingId, rawData: googleShoppingProduct, }); const retailerProduct = await RetailerProduct.findOneOrFail(); expect(retailerProduct).toEqual({ aggregateProductId: expect.any(String), annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: retailerProduct.id, identifiers: ['amazonid:B00KRY0SY2'], imageUrls: [], isAvailable: null, name: null, price: 100, retailer: 'AMAZON', source: 'GOOGLE_OFFERS', updatedAt: expect.any(Date), url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); }); it('should update RetailerProduct', async () => { const googleShoppingId = '7717270608456740797'; const ingestionID = 'test12'; const googleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [ { price: 100, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, ], }; const updatedGoogleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [ { price: 200, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, ], }; const raw = await repo.create({ googleShoppingId, data: googleShoppingProduct, ingestionID, }); expect(raw).toMatchObject({ googleShoppingId, rawData: googleShoppingProduct, }); const retailerProduct = await RetailerProduct.findOneOrFail(); expect(retailerProduct).toEqual({ aggregateProductId: expect.any(String), annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: expect.any(String), identifiers: ['amazonid:B00KRY0SY2'], imageUrls: [], isAvailable: null, name: null, price: 100, retailer: 'AMAZON', source: 'GOOGLE_OFFERS', updatedAt: expect.any(Date), url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); await repo.create({ googleShoppingId, data: updatedGoogleShoppingProduct, ingestionID, }); const updatedRetailerProduct = await RetailerProduct.findOneOrFail(); expect(updatedRetailerProduct).toEqual({ aggregateProductId: expect.any(String), annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: expect.any(String), identifiers: ['amazonid:B00KRY0SY2'], imageUrls: [], isAvailable: null, name: null, price: 200, retailer: 'AMAZON', source: 'GOOGLE_OFFERS', updatedAt: expect.any(Date), url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); }); it('handles upserting data all the way up to aggregate product', async () => { const googleShoppingId = '7717270608456740797'; const ingestionID = 'test12'; const googleShoppingProduct: RawGoogleShoppingOffersDataV1 = { description: 'some random description', offers: [ { price: 100, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, { price: 200, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, { price: 300, url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, ], }; const spy = jest.spyOn(algoliaUtils, 'syncProductsToAlgolia'); const raw = await repo.create({ googleShoppingId, data: googleShoppingProduct, ingestionID, }); expect(raw).toMatchObject({ googleShoppingId: googleShoppingId, rawData: googleShoppingProduct, }); expect(await AggregateProduct.count()).toBe(1); expect(await AggregateProductVariant.count()).toBe(1); expect(await RetailerProduct.count()).toBe(1); expect(await RetailerProductVariant.count()).toBe(1); const aggregateProduct = await AggregateProduct.findOneOrFail(); expect(aggregateProduct).toEqual({ annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: expect.any(String), identifiers: ['amazonid:B00KRY0SY2'], imageUrls: [], isAvailable: null, name: null, price: 300, updatedAt: expect.any(Date), }); expect(spy).toHaveBeenCalledTimes(1); expect(spy).toHaveBeenCalledWith([aggregateProduct.id]); spy.mockClear(); const variant = await AggregateProductVariant.findOneOrFail(); expect(variant).toEqual({ annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: expect.any(String), identifiers: [ 'googleshoppingid:7717270608456740797', 'ingestionid:test12', 'amazonid:B00KRY0SY2', ], imageUrls: [], isAvailable: null, name: null, price: 300, productId: aggregateProduct.id, updatedAt: expect.any(Date), }); const retailerProduct = await RetailerProduct.findOneOrFail(); expect(retailerProduct).toEqual({ aggregateProductId: aggregateProduct.id, annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: retailerProduct.id, identifiers: ['amazonid:B00KRY0SY2'], imageUrls: [], isAvailable: null, name: null, price: 300, retailer: 'AMAZON', source: 'GOOGLE_OFFERS', updatedAt: expect.any(Date), url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); const retailerProductVariant = await RetailerProductVariant.findOneOrFail(); expect(retailerProductVariant).toEqual({ productId: retailerProduct.id, variantId: variant.id, annotations: null, asin: null, category: null, colorCode: null, createdAt: expect.any(Date), deletedAt: null, description: null, dimensions: null, discoverable: null, distributor: null, distributorSKU: null, featured: false, id: expect.any(String), identifiers: [ 'googleshoppingid:7717270608456740797', 'ingestionid:test12', 'amazonid:B00KRY0SY2', ], imageUrls: [], isAvailable: null, name: null, price: 300, retailer: 'AMAZON', source: 'GOOGLE_OFFERS', updatedAt: expect.any(Date), url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }); }); });