Skip to content

Commit

Permalink
fix: transactionAmount NaN when no EL54 present
Browse files Browse the repository at this point in the history
* corrigido testes duplicados
* constantes movidas para arquivo separado
* jest `testMatch` alterado e aceitando somente arquivos .test|spec.
* adicionado testes para emv codes sem valor e sem o elemento do valor (ell54)
  • Loading branch information
thalesog committed Dec 30, 2021
1 parent fd0c273 commit cc662fd
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 17 deletions.
5 changes: 1 addition & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
roots: ['<rootDir>/src'],
testMatch: [
'**/__tests__/**/*.+(ts|tsx|js)',
'**/?(*.)+(spec|test).+(ts|tsx|js)',
],
testMatch: ['**/__tests__/**/?(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
Expand Down
2 changes: 1 addition & 1 deletion src/__tests__/crc.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { computeCRC } from '../crc';

import { DYNAMIC_TEST_EMV, STATIC_TEST_EMV } from './parser.test';
import { DYNAMIC_TEST_EMV, STATIC_TEST_EMV } from './emvCodes';

describe('CRC Calculator and Parser', () => {
it('should be able to calculate crc', () => {
Expand Down
22 changes: 21 additions & 1 deletion src/__tests__/creator.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { createDynamicPix, createStaticPix, hasError } from '..';

import { DYNAMIC_TEST_EMV, STATIC_TEST_EMV } from './parser.test';
import {
DYNAMIC_TEST_EMV,
STATIC_TEST_EMV,
STATIC_TEST_NO_VALUE_EMV,
} from './emvCodes';

describe('EMV Code Creation', () => {
it('should be able to create a static pix from mandatory fields', () => {
Expand All @@ -19,6 +23,22 @@ describe('EMV Code Creation', () => {
expect(staticPixFn.toBRCode()).toBe(STATIC_TEST_EMV);
});

it('should be able to create a static pix from mandatory fields with no value', () => {
const staticPixFn = createStaticPix({
merchantName: 'Thales Ogliari',
merchantCity: 'SAO MIGUEL DO OESTE'.substr(0, 15),
pixKey: 'thalesog@me.com',
infoAdicional: 'Pedido 123',
txid: '',
transactionAmount: 0,
});

expect(hasError(staticPixFn)).toBe(false);
if (hasError(staticPixFn)) return;

expect(staticPixFn.toBRCode()).toBe(STATIC_TEST_NO_VALUE_EMV);
});

it('should be able to create a dynamic pix from mandatory fields', () => {
const dynamicPixFn = createDynamicPix({
merchantName: 'Thales Ogliari',
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/emvCodes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const STATIC_TEST_EMV =
'00020126510014br.gov.bcb.pix0115thalesog@me.com0210Pedido 123520400005303986540510.005802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63044367';

export const STATIC_TEST_NO_VALUE_EMV =
'00020126510014br.gov.bcb.pix0115thalesog@me.com0210Pedido 12352040000530398654040.005802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63044406';

export const STATIC_TEST_NO_VALUE_ELEMENT_EMV =
'00020126510014br.gov.bcb.pix0115thalesog@me.com0210Pedido 1235204000053039865802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63042C6B';

export const DYNAMIC_TEST_EMV =
'00020126740014br.gov.bcb.pix2552payload.psp.com/3ec9d2f9-5f03-4e0e-820d-63a81e769e875204000053039865802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63040C64';
3 changes: 1 addition & 2 deletions src/__tests__/extractor.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { parsePix, PixElementType } from '..';
import { hasError, isDynamicPix, isStaticPix } from '../validate';

import { DYNAMIC_TEST_EMV, STATIC_TEST_EMV } from './parser.test';

import { DYNAMIC_TEST_EMV, STATIC_TEST_EMV } from './emvCodes';
describe('EMV Data Extractor', () => {
it('should be able to extract basic elements from a static pix', () => {
const parsedPix = parsePix(STATIC_TEST_EMV);
Expand Down
41 changes: 36 additions & 5 deletions src/__tests__/parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
ValidTags,
} from '../types/pixEmvSchema';

export const STATIC_TEST_EMV =
'00020126510014br.gov.bcb.pix0115thalesog@me.com0210Pedido 123520400005303986540510.005802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63044367';

export const DYNAMIC_TEST_EMV =
'00020126740014br.gov.bcb.pix2552payload.psp.com/3ec9d2f9-5f03-4e0e-820d-63a81e769e875204000053039865802BR5914Thales Ogliari6015SAO MIGUEL DO O62070503***63040C64';
import {
DYNAMIC_TEST_EMV,
STATIC_TEST_EMV,
STATIC_TEST_NO_VALUE_ELEMENT_EMV,
STATIC_TEST_NO_VALUE_EMV,
} from './emvCodes';

describe('EMV Parser', () => {
it('should be able to parse mandatory elements from a qrcode', () => {
Expand Down Expand Up @@ -66,6 +67,36 @@ describe('EMV Parser', () => {
expect(pix.txid).toBe('***');
});

it('should be able to parse a static pix with no value', () => {
const pix = parsePix(STATIC_TEST_NO_VALUE_EMV) as PixStaticObject;

expect(pix.type).toBe('STATIC');
expect(pix.merchantCategoryCode).toBe('0000');
expect(pix.transactionCurrency).toBe('986');
expect(pix.countryCode).toBe('BR');
expect(pix.merchantName).toBe('Thales Ogliari');
expect(pix.merchantCity).toBe('SAO MIGUEL DO O');
expect(pix.pixKey).toBe('thalesog@me.com');
expect(pix.transactionAmount).toBe(0);
expect(pix.infoAdicional).toBe('Pedido 123');
expect(pix.txid).toBe('***');
});

it('should be able to parse a static pix with no value element', () => {
const pix = parsePix(STATIC_TEST_NO_VALUE_ELEMENT_EMV) as PixStaticObject;

expect(pix.type).toBe('STATIC');
expect(pix.merchantCategoryCode).toBe('0000');
expect(pix.transactionCurrency).toBe('986');
expect(pix.countryCode).toBe('BR');
expect(pix.merchantName).toBe('Thales Ogliari');
expect(pix.merchantCity).toBe('SAO MIGUEL DO O');
expect(pix.pixKey).toBe('thalesog@me.com');
expect(pix.transactionAmount).toBe(0);
expect(pix.infoAdicional).toBe('Pedido 123');
expect(pix.txid).toBe('***');
});

it('should be able to parse a static pix', () => {
const pix = parsePix(DYNAMIC_TEST_EMV) as PixDynamicObject;

Expand Down
7 changes: 3 additions & 4 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,18 +51,17 @@ export function extractElements(
emvElements: ValidTags
): PixElements | PixError {
const basicElements = extractMandatoryElements(emvElements);

if (isPix(emvElements, 'static')) {
const amountNumber = +emvElements.getTag(EmvSchema.TAG_TRANSACTION_AMOUNT);
const transactionAmount = !isNaN(amountNumber) ? amountNumber : 0;
return {
type: PixElementType.STATIC,
...basicElements,
pixKey: emvElements.getSubTag(
EmvMaiSchema.TAG_MAI_PIXKEY,
EmvSchema.TAG_MAI
),
transactionAmount: Number(
emvElements.getTag(EmvSchema.TAG_TRANSACTION_AMOUNT)
),
transactionAmount,
infoAdicional: emvElements.getSubTag(
EmvMaiSchema.TAG_MAI_INFO_ADD,
EmvSchema.TAG_MAI
Expand Down

0 comments on commit cc662fd

Please sign in to comment.