import gql from 'graphql-tag'; import { startSessionAnonymously } from 'test/graphql'; import { getCustomRepository } from 'typeorm'; import { AggregateProductRepository } from '@/repositories'; describe('Query:productCategories', () => { const query = gql` query { categories { edges { node { name products { nodes { id category } } } } } } `; test('should return aggregate product categories', async () => { const { execute } = await startSessionAnonymously(); const aggregateProductRepo = getCustomRepository( AggregateProductRepository ); 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', category: 'category1', }, }); 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', category: 'category2', }, }); await aggregateProductRepo.upsert({ identifiers: ['test3'], 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: 'test3', }, }); const response = await execute({ query: query, }); expect(response.errors?.length).toBeFalsy(); expect(response.data?.categories?.edges).toHaveLength(2); const names = response.data?.categories?.edges?.map( (e: any) => e.node.name ); expect(names).toContain('category1'); expect(names).toContain('category2'); expect( response.data?.categories?.edges?.[0].node?.products?.nodes ).toHaveLength(1); expect( response.data?.categories?.edges?.[0].node?.products?.nodes?.[0]?.category ).toBe('category1'); expect( response.data?.categories?.edges?.[1].node?.products?.nodes ).toHaveLength(1); expect( response.data?.categories?.edges?.[1].node?.products?.nodes?.[0]?.category ).toBe('category2'); }); });