Skip to content

Commit

Permalink
Merge pull request #269 from lucmkz/feature/preference-integration-test
Browse files Browse the repository at this point in the history
IT Preference
  • Loading branch information
lucmkz authored Oct 11, 2023
2 parents 544b3e6 + cbf6a36 commit 0c1fc58
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 9 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# ============= Config e2e =============
/e2e/e2e.config.ts

# ============= Lint =============
# Cache eslint
.eslintcache
Expand Down
4 changes: 0 additions & 4 deletions e2e/demo/demo.spec.ts

This file was deleted.

13 changes: 13 additions & 0 deletions e2e/example.e2e.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* This is an example configuration file for e2e tests, where you must add your credentials and other personal settings.
* Inside the e2e directory, create a file with the name: e2e.config.ts.
* Copy the contents of the example file, add it to the e2e.config.ts file and add your personal settings.
*/

const config = {
access_token: '',
};

export {
config,
};
4 changes: 2 additions & 2 deletions e2e/jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import jestConfig from '../jest.config';

jestConfig.rootDir = '../';
jestConfig.testPathIgnorePatterns = [
'/node_modules/',
'/src/'
'src'
];

export default jestConfig;
50 changes: 50 additions & 0 deletions e2e/preference/create.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import MercadoPago, { Preference } from '@src/index';
import { config } from '../e2e.config';
import type { PreferenceCreateData } from '@src/clients/preference/create/types';

describe('Preference IT, create', () => {
test('should create Preference and match response object', async () => {

const client = new MercadoPago({ accessToken: config.access_token, options: { timeout: 5000 } });
const preference = new Preference(client);

const preferenceRequest: PreferenceCreateData = {
body: {
items: [
{
id: '4567',
category_id: 'car_electronics',
currency_id: 'BRL',
description: 'Dummy create',
picture_url: 'https://http2.mlstatic.com/D_NQ_NP_887467-MLA71526269815_092023-F.jpg',
title: 'Dummy Title',
quantity: 1,
unit_price: 10
}
],
}
};

const response = await preference.create(preferenceRequest);
expect(response.items[0].title).toBe(preferenceRequest.body.items[0].title);
expect(response).toEqual(expect.objectContaining({
init_point: expect.any(String),
client_id: expect.any(String),
collector_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(String),
sandbox_init_point: expect.any(String),
site_id: expect.any(String),
}));
expect(response.items[0]).toEqual(expect.objectContaining({
id: expect.any(String),
category_id: expect.any(String),
currency_id: expect.any(String),
description: expect.any(String),
picture_url: expect.any(String),
title: expect.any(String),
quantity: expect.any(Number),
unit_price: expect.any(Number),
}));
});
});
45 changes: 45 additions & 0 deletions e2e/preference/get.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import MercadoPago, { Preference } from '@src/index';
import { config } from '../e2e.config';
import type { PreferenceCreateData } from '@src/clients/preference/create/types';

describe('Preference IT, get', () => {
test('should get preference and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token, options: { timeout: 5000 } });
const preference = new Preference(client);

const preferenceRequest: PreferenceCreateData = {
body: {
items: [
{
id: '4567',
title: 'Dummy Title Create',
quantity: 1,
unit_price: 10
}
], }
};
const request = await preference.create(preferenceRequest);

const response = await preference.get({ preferenceId: request.id });
expect(response).toHaveProperty('id', request.id);
expect(response).toEqual(expect.objectContaining({
init_point: expect.any(String),
client_id: expect.any(String),
collector_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(String),
sandbox_init_point: expect.any(String),
site_id: expect.any(String),
}));
expect(response.items[0]).toEqual(expect.objectContaining({
id: expect.any(String),
category_id: expect.any(String),
currency_id: expect.any(String),
description: expect.any(String),
title: expect.any(String),
quantity: expect.any(Number),
unit_price: expect.any(Number),
}));
});

});
31 changes: 31 additions & 0 deletions e2e/preference/search.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import MercadoPago, { Preference } from '@src/index';
import { config } from '../e2e.config';

describe('Preference IT, search', () => {
test('should search a request and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token, options: { timeout: 5000 } });
const preference = new Preference(client);

const searched = await preference.search();

expect(searched).toEqual(expect.objectContaining({
elements: expect.any(Array),
next_offset: expect.any(Number),
total: expect.any(Number),
}));
expect(searched.elements.length).toBeGreaterThan(0);
expect(searched.elements[0]).toEqual(expect.objectContaining({
id: expect.any(String),
client_id: expect.any(String),
collector_id: expect.any(Number),
date_created: expect.any(String),
expires: expect.any(Boolean),
live_mode: expect.any(Boolean),
marketplace: expect.any(String),
operation_type: expect.any(String),
site_id: expect.any(String),
sponsor_id: expect.any(Number),
shipping_mode: expect.any(String),
}));
});
});
64 changes: 64 additions & 0 deletions e2e/preference/update.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import MercadoPago, { Preference } from '@src/index';
import { config } from '../e2e.config';
import type { PreferenceCreateData } from '@src/clients/preference/create/types';
import type { PreferenceUpdateData } from '@src/clients/preference/update/types';

describe('Preference IT, update', () => {
test('should update request and match response object', async () => {
const client = new MercadoPago({ accessToken: config.access_token });
const preference = new Preference(client);

const preferenceRequest: PreferenceCreateData = {
body: {
items: [
{
id: '4567',
title: 'Dummy Title Create',
quantity: 1,
unit_price: 10
}
],
}
};
const request = await preference.create(preferenceRequest);

const updateRequest: PreferenceUpdateData = {
id: request.id,
updatePreferenceRequest: {
items: [
{
id: '4567',
title: 'Dummy Title Update',
quantity: 1,
unit_price: 10
}
],
}
};
const response = await preference.update(updateRequest);

expect(response).toEqual(expect.objectContaining({
id: request.id,
}));
expect(response.items[0].title).toBe(updateRequest.updatePreferenceRequest.items[0].title);
expect(response).toEqual(expect.objectContaining({
init_point: expect.any(String),
client_id: expect.any(String),
collector_id: expect.any(Number),
date_created: expect.any(String),
id: expect.any(String),
sandbox_init_point: expect.any(String),
site_id: expect.any(String),
}));
expect(response.items[0]).toEqual(expect.objectContaining({
id: expect.any(String),
category_id: expect.any(String),
currency_id: expect.any(String),
description: expect.any(String),
title: expect.any(String),
quantity: expect.any(Number),
unit_price: expect.any(Number),
}));
});

});
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const jestConfig: JestConfigWithTsJest = {
'text-summary'
],
testMatch: [
'**/?(*.)+(spec|test).[tj]s?(x)'
'**/?(*.)spec.ts'
],
transform: { '^.+\\.(ts|tsx)$': 'ts-jest' },
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>' }),
Expand Down
1 change: 1 addition & 0 deletions src/clients/payment/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export declare type PaymentSearchResult = {
merchant_account_id: string;
acquirer: string;
merchant_number: string;
external_reference: string;
};

export declare type Payer = {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"sourceMap": false,
"outDir": "./dist",
"baseUrl": "./",
"rootDir": "./src",
"rootDir": ".",
"resolveJsonModule": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
Expand All @@ -22,5 +22,5 @@
"@clients/*": ["./src/clients/*"]
}
},
"include": ["src/**/*"]
"include": ["src/**/*", "e2e/**/*"]
}

0 comments on commit 0c1fc58

Please sign in to comment.