import { gql } from 'graphql-request'; import { execute } from 'test/graphql'; import { Page, Product } from '@/entities'; import { ProductSource } from '@/entities/types'; describe('Query:product', () => { const query = gql` query product($input: ProductInput!) { product(input: $input) { product { id } page { id } } } `; test('should return product and its pages', async () => { const exampleUrl = 'www.amazon.com/dp/B07JQQQQQQQ'; const product = await Product.create({ firebaseId: 'product-id', source: ProductSource.GOOGLE_SHOPPING, }).save(); await Page.create({ url: exampleUrl, firebaseId: 'page-id', price: 10, retailer: 'amazon', product, }).save(); const { data, errors } = await execute({ query, variables: { input: { url: exampleUrl, }, }, }); expect(errors).toBeFalsy(); expect(data?.product).toMatchObject({ page: { id: 'page-id' }, product: { id: product.id }, }); }); test('throw if multiple pages match url', async () => { const exampleUrl = 'www.amazon.com/dp/B07JQQQQQQ'; const product = await Product.create({ firebaseId: 'product-id', source: ProductSource.GOOGLE_SHOPPING, }).save(); await Page.create({ url: exampleUrl, firebaseId: 'page-id', price: 10, retailer: 'amazon', product, }).save(); await Page.create({ url: exampleUrl, firebaseId: 'page-id-2', price: 10, retailer: 'amazon', product, }).save(); const { data, errors } = await execute({ query, variables: { input: { url: exampleUrl, }, }, }); expect(errors?.[0].message).toMatchInlineSnapshot( '"Too many pages match this URL, please be more specific."' ); expect(data).toBeFalsy(); }); });