Skip to content

Commit

Permalink
chore: Improving unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
m-arrieta-r committed Mar 30, 2024
1 parent d6f6771 commit 085a21d
Show file tree
Hide file tree
Showing 7 changed files with 1,151 additions and 1,903 deletions.
94 changes: 94 additions & 0 deletions __tests__/stubs/commonExpectedXml.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="utf-8"?>
<FacturaElectronica xmlns="https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronica" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://cdn.comprobanteselectronicos.go.cr/xml-schemas/v4.3/facturaElectronica https://www.hacienda.go.cr/ATV/ComprobanteElectronico/docs/esquemas/2016/v4.3/FacturaElectronica_V4.3.xsd">
<Clave>50601042400020692014200100001010000000002100000001</Clave>
<CodigoActividad>000004</CodigoActividad>
<NumeroConsecutivo>00100001010000000002</NumeroConsecutivo>
<FechaEmision>2024-04-01T06:00:00.000Z</FechaEmision>
<Emisor>
<Nombre>Nombre Receptor</Nombre>
<Identificacion>
<Tipo>01</Tipo>
<Numero>206920142</Numero>
</Identificacion>
<NombreComercial>xyz</NombreComercial>
<Ubicacion>
<Provincia>1</Provincia>
<Canton>01</Canton>
<Distrito>01</Distrito>
<Barrio>01</Barrio>
<OtrasSenas>Señas xyz</OtrasSenas>
</Ubicacion>
<Telefono>
<CodigoPais>506</CodigoPais>
<NumTelefono>12341234</NumTelefono>
</Telefono>
<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>12341234</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Emisor>
<Receptor>
<Nombre>SRL</Nombre>
<Identificacion>
<Tipo>02</Tipo>
<Numero>3102759157</Numero>
</Identificacion>
<NombreComercial>CIENCIA DEL SABOR</NombreComercial>
<Ubicacion>
<Provincia>2</Provincia>
<Canton>06</Canton>
<Distrito>04</Distrito>
<Barrio>06</Barrio>
<OtrasSenas>25 norte 500 oeste restaurante El Faro</OtrasSenas>
</Ubicacion>
<Telefono>
<CodigoPais>506</CodigoPais>
<NumTelefono>80808080</NumTelefono>
</Telefono>
<Fax>
<CodigoPais>506</CodigoPais>
<NumTelefono>80808080</NumTelefono>
</Fax>
<CorreoElectronico>[email protected]</CorreoElectronico>
</Receptor>
<CondicionVenta>01</CondicionVenta>
<MedioPago>01</MedioPago>
<DetalleServicio>
<LineaDetalle>
<NumeroLinea>1</NumeroLinea>
<Codigo>7113301000000</Codigo>
<Cantidad>1</Cantidad>
<UnidadMedida>Unid</UnidadMedida>
<Detalle>detalle</Detalle>
<PrecioUnitario>10</PrecioUnitario>
<MontoTotal>10</MontoTotal>
<SubTotal>10</SubTotal>
<Impuesto>
<Codigo>01</Codigo>
<CodigoTarifa>08</CodigoTarifa>
<Tarifa>13</Tarifa>
<Monto>1.3</Monto>
</Impuesto>
<MontoTotalLinea>11.3</MontoTotalLinea>
</LineaDetalle>
</DetalleServicio>
<ResumenFactura>
<CodigoTipoMoneda>
<CodigoMoneda>CRC</CodigoMoneda>
<TipoCambio>585.69</TipoCambio>
</CodigoTipoMoneda>
<TotalServGravados>0</TotalServGravados>
<TotalServExentos>0</TotalServExentos>
<TotalMercanciasGravadas>10</TotalMercanciasGravadas>
<TotalMercanciasExentas>0</TotalMercanciasExentas>
<TotalGravado>10</TotalGravado>
<TotalExento>0</TotalExento>
<TotalExonerado>0</TotalExonerado>
<TotalVenta>10</TotalVenta>
<TotalDescuentos>0</TotalDescuentos>
<TotalVentaNeta>10</TotalVentaNeta>
<TotalImpuesto>1.3</TotalImpuesto>
<TotalComprobante>11.3</TotalComprobante>
</ResumenFactura>
</FacturaElectronica>
3 changes: 3 additions & 0 deletions __tests__/tests/ATV/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Create Document (Invoice) should create document and generate a correct command 1`] = `"PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48RmFjdHVyYUVsZWN0cm9uaWNhIHhtbG5zPSJodHRwczovL2Nkbi5jb21wcm9iYW50ZXNlbGVjdHJvbmljb3MuZ28uY3IveG1sLXNjaGVtYXMvdjQuMy9mYWN0dXJhRWxlY3Ryb25pY2EiIHhtbG5zOmRzPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwLzA5L3htbGRzaWcjIiB4bWxuczp4c2Q9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hIiB4bWxuczp4c2k9Imh0dHA6Ly93d3cudzMub3JnLzIwMDEvWE1MU2NoZW1hLWluc3RhbmNlIiB4c2k6c2NoZW1hTG9jYXRpb249Imh0dHBzOi8vY2RuLmNvbXByb2JhbnRlc2VsZWN0cm9uaWNvcy5nby5jci94bWwtc2NoZW1hcy92NC4zL2ZhY3R1cmFFbGVjdHJvbmljYSBodHRwczovL3d3dy5oYWNpZW5kYS5nby5jci9BVFYvQ29tcHJvYmFudGVFbGVjdHJvbmljby9kb2NzL2VzcXVlbWFzLzIwMTYvdjQuMy9GYWN0dXJhRWxlY3Ryb25pY2FfVjQuMy54c2QiPjxDbGF2ZT41MDYwMTA0MjQwMDAyMDY5MjAxNDIwMDEwMDAwMTAxMDAwMDAwMDAwMjEwMDAwMDAwMTwvQ2xhdmU+PENvZGlnb0FjdGl2aWRhZD4wMDAwMDQ8L0NvZGlnb0FjdGl2aWRhZD48TnVtZXJvQ29uc2VjdXRpdm8+MDAxMDAwMDEwMTAwMDAwMDAwMDI8L051bWVyb0NvbnNlY3V0aXZvPjxGZWNoYUVtaXNpb24+MjAyNC0wNC0wMVQwNjowMDowMC4wMDBaPC9GZWNoYUVtaXNpb24+PEVtaXNvcj48Tm9tYnJlPk5vbWJyZSBSZWNlcHRvcjwvTm9tYnJlPjxJZGVudGlma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TwvVGlwb0NhbWJpbz48L0NvZGlnb1RpcG9Nb25lZGE+PFRvdGFsU2VydkdyYXZhZG9zPjA8L1RvdGFsU2VydkdyYXZhZG9zPjxUb3RhbFNlcnZFeGVudG9zPjA8L1RvdGFsU2VydkV4ZW50b3M+PFRvdGFsTWVyY2FuY2lhc0dyYXZhZGFzPjEwPC9Ub3RhbE1lcmNhbmNpYXNHcmF2YWRhcz48VG90YWxNZXJjYW5jaWFzRXhlbnRhcz4wPC9Ub3RhbE1lcmNhbmNpYXNFeGVudGFzPjxUb3RhbEdyYXZhZG8+MTA8L1RvdGFsR3JhdmFkbz48VG90YWxFeGVudG8+MDwvVG90YWxFeGVudG8+PFRvdGFsRXhvbmVyYWRvPjA8L1RvdGFsRXhvbmVyYWRvPjxUb3RhbFZlbnRhPjEwPC9Ub3RhbFZlbnRhPjxUb3RhbERlc2N1ZW50b3M+MDwvVG90YWxEZXNjdWVudG9zPjxUb3RhbFZlbnRhTmV0YT4xMDwvVG90YWxWZW50YU5ldGE+PFRvdGFsSW1wdWVzdG8+MS4zPC9Ub3RhbEltcHVlc3RvPjxUb3RhbENvbXByb2JhbnRlPjExLjM8L1RvdGFsQ29tcHJvYmFudGU+PC9SZXN1bWVuRmFjdHVyYT48L0ZhY3R1cmFFbGVjdHJvbmljYT4="`;
50 changes: 50 additions & 0 deletions __tests__/tests/ATV/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import fs from 'fs'
import { ATV } from "@src/ATV"
import 'jest-xml-matcher'
import { createDocumentInputStub } from "@test/stubs/createDocument.stub"
const fakePem = fs.readFileSync('__tests__/stubs/dummyKeys/client-identity.p12', 'binary')
const fakePassword = '1234'
const expectXml = fs.readFileSync('__tests__/stubs/commonExpectedXml.xml', 'utf-8')

describe('Create Document (Invoice)', () => {
beforeAll(() => {
jest.useFakeTimers();
jest.setSystemTime(new Date(2024, 3, 1));
})

it('should create document and generate a correct command', async () => {
const atv = new ATV({}, 'stg')
const createdDoc = await atv.createDocumentCommand({
document: createDocumentInputStub,
token: 'fake-token',
signatureOptions: {
buffer: fakePem,
password: fakePassword
}
})
expect(createdDoc.command).toMatchObject({
url: 'https://api-sandbox.comprobanteselectronicos.go.cr/recepcion/v1/recepcion',
method: 'post'
})
expect(createdDoc.command.data).toMatchObject({
clave: '50601042400020692014200100001010000000002100000001',
fecha: '2024-04-01T06:00:00.000Z',
emisor: { tipoIdentificacion: '01', numeroIdentificacion: '206920142' },
receptor: { tipoIdentificacion: '02', numeroIdentificacion: '3102759157' }
})
expect(createdDoc.command.data.comprobanteXml).toBeDefined()
expect(createdDoc.extraData.xml).toBeDefined()
})

it('should create document and generate a correct command', async () => {
const atv = new ATV({}, 'stg')
//@ts-ignore just for testing
const createdDoc = await atv.createDocumentCommand({
document: createDocumentInputStub,
token: 'fake-token',
signatureOptions: undefined
})
expect(createdDoc.command.data.comprobanteXml).toMatchSnapshot()
expect(createdDoc.extraData.xml).toEqualXML(expectXml)
})
})
28 changes: 0 additions & 28 deletions __tests__/tests/electronicBill.test.ts

This file was deleted.

8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ module.exports = {
},
coverageThreshold: {
global: {
statements: 20,
branches: 19,
functions: 20,
lines: 20
statements: 54,
branches: 38,
functions: 65,
lines: 54
}
},
restoreMocks: true,
Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"test": "__test__"
},
"scripts": {
"test": "npx jest",
"test": "npx jest --no-coverage",
"test:cov": "npx jest",
"dev": "dotenv -- nodemon -e ts,graphql -x 'yarn lint & ts-node -r tsconfig-paths/register src/index.ts'",
"build": "yarn lint & tsc",
"lint": "eslint --ext .ts, --ignore-path .gitignore .",
Expand All @@ -32,7 +33,7 @@
},
"homepage": "https://github.com/facturacr/facturar-costa-rica-lib#readme",
"devDependencies": {
"@types/jest": "^26.0.15",
"@types/jest": "^29.5",
"@types/node": "^14.14.39",
"@typescript-eslint/eslint-plugin": "^2.3.0",
"@typescript-eslint/parser": "^2.3.0",
Expand All @@ -43,9 +44,10 @@
"eslint-plugin-node": "^11.1.0",
"eslint-plugin-promise": "^4.3.1",
"eslint-plugin-standard": "^5.0.0",
"jest": "^26.6.3",
"jest": "^29.7",
"jest-xml-matcher": "^1.2.0",
"nodemon": "^2.0.19",
"ts-jest": "^26.5.6",
"ts-jest": "^29.1",
"ts-node": "10.9.1",
"tsc": "^1.20150623.0",
"tsconfig-paths": "^3.9.0",
Expand Down
Loading

0 comments on commit 085a21d

Please sign in to comment.