import gql from 'graphql-tag'; import { startSessionAnonymously } from 'test/graphql'; import { getCustomRepository } from 'typeorm'; import { RetailerProductSource } from '@/entities/types'; import { AggregateProductRepository, RetailerProductRepository, RetailerProductVariantRepository, } from '@/repositories'; import { RetailerKind } from '@/types'; describe('Query:aggregateProducts', () => { const query = gql` query { aggregateProducts { edges { node { id } } } } `; test('should return aggregate products', async () => { const { execute } = await startSessionAnonymously(); const aggregateProductRepo = getCustomRepository( AggregateProductRepository ); const { aggregateProductId: aggregateProductId1 } = await aggregateProductRepo.upsert({ identifiers: ['test'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', }, }); const { aggregateProductId: aggregateProductId2 } = await aggregateProductRepo.upsert({ identifiers: ['test2'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', }, }); const response = await execute({ query, }); expect(response.errors?.length).toBeFalsy(); expect(response.data?.aggregateProducts?.edges).toHaveLength(2); const ids = response.data?.aggregateProducts?.edges?.map( (e: any) => e.node.id ); expect(ids).toContain(aggregateProductId1); expect(ids).toContain(aggregateProductId2); }); }); describe('Query:aggregateProduct', () => { const query = gql` query ($input: AggregateProductInput!) { aggregateProduct(input: $input) { aggregateProduct { id name variants { id name } retailerProducts { id url name variants { id url } } } } } `; test('should return aggregate product', async () => { const { execute } = await startSessionAnonymously(); const aggregateProductRepo = getCustomRepository( AggregateProductRepository ); const { aggregateProductId: aggregateProductId1 } = await aggregateProductRepo.upsert({ identifiers: ['test'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test 1', }, }); await aggregateProductRepo.upsert({ identifiers: ['test2'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test 2', }, }); const response1 = await execute({ query, variables: { input: { id: aggregateProductId1, }, }, }); expect(response1.errors?.length).toBeFalsy(); expect(response1.data?.aggregateProduct.aggregateProduct.id).toBe( aggregateProductId1 ); const retailerProductRepo = getCustomRepository(RetailerProductRepository); const { aggregateProductId } = await aggregateProductRepo.upsert({ identifiers: ['test'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test 1', }, }); const { retailerProductId } = await retailerProductRepo.upsert({ retailer: RetailerKind.PERIGOLD, source: RetailerProductSource.GOOGLE_SHOPPING, productIdentifiers: ['test'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2', }, }); const response2 = await execute({ query, variables: { input: { id: aggregateProductId } }, }); expect(response2.errors?.length).toBeFalsy(); expect( response2.data?.aggregateProduct.aggregateProduct.retailerProducts ).toHaveLength(1); expect( response2.data?.aggregateProduct.aggregateProduct.retailerProducts[0].id ).toBe(retailerProductId); }); it('returns retailer with cleaned url', async () => { const { execute } = await startSessionAnonymously(); const { aggregateProductId } = await getCustomRepository( RetailerProductVariantRepository ).upsert({ source: RetailerProductSource.GOOGLE_SHOPPING, retailer: RetailerKind.PERIGOLD, productIdentifiers: ['test'], variantIdentifiers: ['test1-x'], data: { asin: 'asin', colorCode: 'colorCode', annotations: 'annotation', price: 100, description: 'description', dimensions: 'dimensions', discoverable: true, distributor: 'distributor', distributorSKU: 'distributor', imageUrls: ['http://test.com/test.jpg'], isAvailable: true, name: 'test', url: 'https://www.amazon.com/Haussmann-Twist-Stool-High-Walnut/dp/B00KRY0SY2?locale=en-US&source=shopping', }, }); const response = await execute({ query, variables: { input: { id: aggregateProductId, }, }, }); expect(response.errors).toBeFalsy(); expect(response.data?.aggregateProduct.aggregateProduct.id).toBe( aggregateProductId ); expect( response.data?.aggregateProduct.aggregateProduct.retailerProducts ).toHaveLength(1); expect( response.data?.aggregateProduct.aggregateProduct.retailerProducts[0].url ).toMatchInlineSnapshot(`"https://www.amazon.com/dp/B00KRY0SY2"`); expect( response.data?.aggregateProduct.aggregateProduct.retailerProducts[0] .variants ).toHaveLength(1); expect( response.data?.aggregateProduct.aggregateProduct.retailerProducts[0] .variants[0].url ).toMatchInlineSnapshot(`"https://www.amazon.com/dp/B00KRY0SY2"`); }); });