Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .env.local
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
DB_URI=postgres://postgres:root@host.docker.internal:5432/negt
POSTGRES_HOST=host.docker.internal
DB_URI=postgres://postgres:root@localhost:5432/negt
POSTGRES_HOST=localhost
POSTGRES_DB=negt
POSTGRES_USER=postgres
POSTGRES_PASSWORD=root
NODE_ENV=local
ACCESS_TOKEN_SECRET=4cd7234152590dcfe77e1b6fc52e84f4d30c06fddadd0dd2fb42cbc51fa14b1bb195bbe9d72c9599ba0c6b556f9bd1607a8478be87e5a91b697c74032e0ae7af
REDIS_HOST=host.docker.internal
REDIS_HOST=localhost
REDIS_PORT=6379
2 changes: 1 addition & 1 deletion __tests__/server/gql/model/address.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('Integration test for createAddress mutation', () => {
jest.unmock('@database/models');
jest.unmock('ioredis');
process.env = { ...OLD_ENV };
process.env = { ...process.env, ...getMockDBEnv(), REDIS_PORT: 6380 };
process.env = { ...process.env, ...getMockDBEnv() };
});

afterAll(async () => {
Expand Down
4 changes: 1 addition & 3 deletions __tests__/server/gql/model/products.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,12 @@ describe('Integration test for products', () => {
jest.unmock('@database/models');
jest.unmock('ioredis');
process.env = { ...OLD_ENV };
process.env = { ...process.env, ...getMockDBEnv(), REDIS_PORT: 6380 };
process.env = { ...process.env, ...getMockDBEnv() };
});

afterAll(async () => {
process.env = OLD_ENV; // Restore old environment
await closeRedisConnection(); // avoid jest open handle error
const actualRedis = jest.requireActual('@server/services/redis').redis;
await actualRedis.quit();
});

it('should check and get products', async () => {
Expand Down
162 changes: 162 additions & 0 deletions __tests__/server/gql/model/purchasedProducts.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
const { closeRedisConnection } = require('@server/services/redis');
const { getMockDBEnv, getResponse } = require('@server/utils/testUtils');
const { get } = require('lodash');

function getPurchasedProductsQueryWithParams(
limit,
offset,
fields = ['id', 'price', 'discount', 'deliveryDate', 'productId', 'storeId']
) {
return `
query Query {
purchasedProducts(limit: ${limit}, offset: ${offset}) {
edges {
node {
${fields.map(item => item)},
createdAt
updatedAt
deletedAt
products {
edges {
node {
id
name
category
amount
}
}
}
}
}
}
}
`;
}

describe('Integration test for purchasedProduct query', () => {
const OLD_ENV = process.env;

beforeEach(() => {
jest.resetModules();
jest.unmock('@database');
jest.unmock('@database/models');
jest.unmock('ioredis');
process.env = { ...OLD_ENV };
process.env = { ...process.env, ...getMockDBEnv() };
});

afterAll(async () => {
process.env = OLD_ENV; // Restore old environment
await closeRedisConnection(); // avoid jest open handle error
});

it('Should fetch purchased products with given limit and offset', async () => {
const limit = 10;
const offset = 0;
const response = await getResponse(getPurchasedProductsQueryWithParams(limit, offset));

const purchasedProducts = get(response, 'body.data.purchasedProducts.edges');
// Perform assertions based on the response
expect(purchasedProducts).toBeTruthy();
expect(purchasedProducts.length).toBeGreaterThan(0);
expect(purchasedProducts.length).toBe(limit);

const firstProduct = purchasedProducts[0].node;

// Purchase details
expect(firstProduct).toHaveProperty('id');
expect(firstProduct).toHaveProperty('price');
expect(firstProduct).toHaveProperty('discount');
expect(firstProduct).toHaveProperty('deliveryDate');
expect(firstProduct).toHaveProperty('productId');
expect(firstProduct).toHaveProperty('storeId');
expect(firstProduct).toHaveProperty('createdAt');
expect(firstProduct).toHaveProperty('updatedAt');
expect(firstProduct).toHaveProperty('deletedAt');
expect(firstProduct).toHaveProperty('products');
expect(firstProduct.price).toBe(808);
expect(firstProduct.productId).toBe('1');
expect(firstProduct.discount).toBe(100);
expect(firstProduct.storeId).toBe('1');
expect(firstProduct.deliveryDate).toBe('2024-01-12T06:49:10.749Z');
expect(firstProduct.createdAt).toBe('1994-10-24T06:49:10.749Z');

// First product details
const productsDetails = get(firstProduct, 'products.edges');
expect(productsDetails.length).toBe(1);

const firstProductDetails = productsDetails[0].node;
expect(firstProductDetails.id).toBe('1');
expect(firstProductDetails.name).toBe('test1');
expect(firstProductDetails.category).toBe('category1');
expect(firstProductDetails.amount).toBe(10);
});

it('Should fetch purchased products with given updated limit and offset', async () => {
const limit = 5;
const offset = 10;
const response = await getResponse(getPurchasedProductsQueryWithParams(limit, offset));

const purchasedProducts = get(response, 'body.data.purchasedProducts.edges');
// Perform assertions based on the response
expect(purchasedProducts).toBeTruthy();
expect(purchasedProducts.length).toBeGreaterThan(0);
expect(purchasedProducts.length).toBe(limit);

const firstProduct = purchasedProducts[0].node;
expect(firstProduct).toHaveProperty('id');
expect(firstProduct).toHaveProperty('price');
expect(firstProduct).toHaveProperty('discount');
expect(firstProduct).toHaveProperty('deliveryDate');
expect(firstProduct).toHaveProperty('productId');
expect(firstProduct).toHaveProperty('storeId');
expect(firstProduct).toHaveProperty('createdAt');
expect(firstProduct).toHaveProperty('updatedAt');
expect(firstProduct).toHaveProperty('deletedAt');
expect(firstProduct.id).toBe(`${offset + 1}`);

// First product details
const productsDetails = get(firstProduct, 'products.edges');
expect(productsDetails.length).toBe(1);

const firstProductDetails = productsDetails[0].node;
expect(firstProductDetails).toHaveProperty('id');
expect(firstProductDetails).toHaveProperty('name');
expect(firstProductDetails).toHaveProperty('category');
expect(firstProductDetails).toHaveProperty('amount');
});

it('Should fetch purchased products', async () => {
const limit = 5;
const offset = 10;
const response = await getResponse(getPurchasedProductsQueryWithParams(limit, offset, ['id', 'price']));

const purchasedProducts = get(response, 'body.data.purchasedProducts.edges');
// Perform assertions based on the response
expect(purchasedProducts).toBeTruthy();
expect(purchasedProducts.length).toBeGreaterThan(0);
expect(purchasedProducts.length).toBe(limit);

const firstProduct = purchasedProducts[0].node;
expect(firstProduct).toHaveProperty('id');
expect(firstProduct).toHaveProperty('price');
expect(firstProduct).not.toHaveProperty('discount');
expect(firstProduct).not.toHaveProperty('deliveryDate');
expect(firstProduct).not.toHaveProperty('productId');
expect(firstProduct).not.toHaveProperty('storeId');
expect(firstProduct).toHaveProperty('createdAt');
expect(firstProduct).toHaveProperty('updatedAt');
expect(firstProduct).toHaveProperty('deletedAt');
expect(firstProduct.id).toBe(`${offset + 1}`);

// First product details
const productsDetails = get(firstProduct, 'products.edges');
expect(productsDetails.length).toBe(1);

const firstProductDetails = productsDetails[0].node;
expect(firstProductDetails).toHaveProperty('id');
expect(firstProductDetails).toHaveProperty('name');
expect(firstProductDetails).toHaveProperty('category');
expect(firstProductDetails).toHaveProperty('amount');
});
});
Loading