diff --git a/test/payload/fragments.ts b/test/payload/fragments.ts new file mode 100644 index 0000000..d4d1a97 --- /dev/null +++ b/test/payload/fragments.ts @@ -0,0 +1,17 @@ +export const itemsFragment = ` +fragment itemsField on Product { + id + name +} +` +export const getProductsWithFragment = ` +${itemsFragment} +query { + products { + totalItems + items { + ...itemsField + } + } +} +`; \ No newline at end of file diff --git a/test/specs/axios.spec.ts b/test/specs/axios.spec.ts index 5905e08..55c3e15 100644 --- a/test/specs/axios.spec.ts +++ b/test/specs/axios.spec.ts @@ -2,6 +2,7 @@ import { faker } from '@faker-js/faker'; import { expect } from 'chai'; import { URL } from '../env/manager'; import { callGraphQlAPIUsingAxios } from '../helper/apiUtils'; +import { getProductsWithFragment } from '../payload/fragments'; import { registerCustomerAccount } from '../payload/mutation'; import { getProductByName, getProducts } from '../payload/queries'; import { CustomerDetailsType } from '../types/customer'; @@ -16,6 +17,13 @@ describe('test graphql api using axios', () => { expect(response.data.data.products.items).to.have.length.greaterThan(0); }); + it('should fetch all products using fragments', async function () { + const response = await callGraphQlAPIUsingAxios(URL, getProductsWithFragment, { logRequest: true, logResponse: true, mochaContext: this }); + + expect(response.status).equal(200); + expect(response.data.data.products.items).to.have.length.greaterThan(0); + }); + it('should fetch products by name', async function () { let productName = "Laptop"; const response = await callGraphQlAPIUsingAxios(URL, getProductByName(productName), { logRequest: true, logResponse: true, mochaContext: this }); diff --git a/test/specs/supertest.spec.ts b/test/specs/supertest.spec.ts index 0d4e53a..b460372 100644 --- a/test/specs/supertest.spec.ts +++ b/test/specs/supertest.spec.ts @@ -3,6 +3,7 @@ import { expect } from 'chai'; import { describe, it } from 'mocha'; import { URL } from '../env/manager'; import { callGraphQlAPIUsingSuperTest } from '../helper/apiUtils'; +import { getProductsWithFragment } from '../payload/fragments'; import { registerCustomerAccount } from '../payload/mutation'; import { getProductByName, getProducts } from '../payload/queries'; import { CustomerDetailsType } from '../types/customer'; @@ -18,6 +19,13 @@ describe('test graphql api using supertest', function () { expect(response.body.data.products.items).to.have.length.greaterThan(0); }); + it('should fetch all products using fragments', async function () { + const response = await callGraphQlAPIUsingSuperTest(URL, getProductsWithFragment, { logRequest: true, logResponse: true, mochaContext: this }); + + expect(response.statusCode).equal(200); + expect(response.body.data.products.items).to.have.length.greaterThan(0); + }); + it('should fetch product using query parameter', async function () { const productName = "Laptop"; const response = await callGraphQlAPIUsingSuperTest(URL, getProductByName(productName), { logRequest: true, logResponse: true, mochaContext: this });