Skip to content

Commit

Permalink
added fragmenet sample
Browse files Browse the repository at this point in the history
  • Loading branch information
sadabnepal committed Jul 13, 2023
1 parent 5ef55ca commit b67b3a5
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
17 changes: 17 additions & 0 deletions test/payload/fragments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export const itemsFragment = `
fragment itemsField on Product {
id
name
}
`
export const getProductsWithFragment = `
${itemsFragment}
query {
products {
totalItems
items {
...itemsField
}
}
}
`;
8 changes: 8 additions & 0 deletions test/specs/axios.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });
Expand Down
8 changes: 8 additions & 0 deletions test/specs/supertest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 });
Expand Down

0 comments on commit b67b3a5

Please sign in to comment.