From cc662fd3ce62ba5fa069aeee93b8fe43d5dc8aee Mon Sep 17 00:00:00 2001 From: Thales Ogliari Date: Thu, 30 Dec 2021 14:12:43 -0300 Subject: [PATCH] fix: transactionAmount NaN when no EL54 present * 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) --- jest.config.js | 5 +--- src/__tests__/crc.test.ts | 2 +- src/__tests__/creator.test.ts | 22 +++++++++++++++++- src/__tests__/emvCodes.ts | 11 +++++++++ src/__tests__/extractor.test.ts | 3 +-- src/__tests__/parser.test.ts | 41 +++++++++++++++++++++++++++++---- src/parser.ts | 7 +++--- 7 files changed, 74 insertions(+), 17 deletions(-) create mode 100644 src/__tests__/emvCodes.ts diff --git a/jest.config.js b/jest.config.js index 62dd762..f04778a 100644 --- a/jest.config.js +++ b/jest.config.js @@ -3,10 +3,7 @@ module.exports = { preset: 'ts-jest', testEnvironment: 'node', roots: ['/src'], - testMatch: [ - '**/__tests__/**/*.+(ts|tsx|js)', - '**/?(*.)+(spec|test).+(ts|tsx|js)', - ], + testMatch: ['**/__tests__/**/?(*.)+(spec|test).+(ts|tsx|js)'], transform: { '^.+\\.(ts|tsx)$': 'ts-jest', }, diff --git a/src/__tests__/crc.test.ts b/src/__tests__/crc.test.ts index 68c548b..73ced44 100644 --- a/src/__tests__/crc.test.ts +++ b/src/__tests__/crc.test.ts @@ -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', () => { diff --git a/src/__tests__/creator.test.ts b/src/__tests__/creator.test.ts index 8cf54bb..1e4ee08 100644 --- a/src/__tests__/creator.test.ts +++ b/src/__tests__/creator.test.ts @@ -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', () => { @@ -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', diff --git a/src/__tests__/emvCodes.ts b/src/__tests__/emvCodes.ts new file mode 100644 index 0000000..5913d52 --- /dev/null +++ b/src/__tests__/emvCodes.ts @@ -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'; diff --git a/src/__tests__/extractor.test.ts b/src/__tests__/extractor.test.ts index f1a7e3f..b2c39ab 100644 --- a/src/__tests__/extractor.test.ts +++ b/src/__tests__/extractor.test.ts @@ -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); diff --git a/src/__tests__/parser.test.ts b/src/__tests__/parser.test.ts index b70330c..8f15eb2 100644 --- a/src/__tests__/parser.test.ts +++ b/src/__tests__/parser.test.ts @@ -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', () => { @@ -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; diff --git a/src/parser.ts b/src/parser.ts index 706fe05..9719ccd 100644 --- a/src/parser.ts +++ b/src/parser.ts @@ -51,8 +51,9 @@ 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, @@ -60,9 +61,7 @@ export function extractElements( 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