Skip to content

Commit

Permalink
Merge pull request #27 from pagarme/fix-diff-dates
Browse files Browse the repository at this point in the history
Fix dates issues
  • Loading branch information
lucianopf authored Nov 1, 2018
2 parents e2a2dc0 + 3149a5d commit 2c40f4d
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 25 deletions.
17 changes: 11 additions & 6 deletions lib/boleto.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var ejs = require('ejs')
var formatters = require('./formatters')
var barcode = require('./barcode')
var path = require('path')
const ejs = require('ejs')
const formatters = require('./formatters')
const barcode = require('./barcode')
const path = require('path')
const moment = require('moment')

var banks = null

Expand Down Expand Up @@ -31,11 +32,15 @@ var Boleto = function (options) {
}

if (!options['data_emissao']) {
options['data_emissao'] = new Date()
options['data_emissao'] = moment().utc()
} else {
options['data_emissao'] = moment(moment(options['data_emissao']).utc().format('YYYY-MM-DD'))
}

if (!options['data_vencimento']) {
options['data_vencimento'] = new Date(new Date().getTime() + (5 * 24 * 3600 * 1000))
options['data_vencimento'] = moment().utc().add('5', 'days')
} else {
options['data_vencimento'] = moment(moment(options['data_vencimento']).utc().format('YYYY-MM-DD'))
}

for (var key in options) {
Expand Down
3 changes: 1 addition & 2 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ exports.formatAmount = function (amount) {
}

exports.formatDate = function (date) {
return exports.addTrailingZeros(date.getDate(), 2) + '/' +
exports.addTrailingZeros(date.getMonth() + 1, 2) + '/' + date.getFullYear()
return moment(date).format('DD/MM/YYYY')
}

exports.mod11 = function (num, base, r) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-boleto",
"version": "2.0.5",
"version": "2.0.6",
"description": "Boleto generator in Node.js",
"main": "index.js",
"scripts": {
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/boleto-render.spec.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions test/mocks/banks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const R = require('ramda')

module.exports = {
test: {
options: {
logoURL: 'https://pagar.me/img.png',
codigo: '123'
},
barcodeData: R.always('123'),
linhaDigitavel: R.always('123')
}
}

25 changes: 25 additions & 0 deletions test/mocks/boleto-2018-10-31.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const moment = require('moment')
const testBanks = require('./banks')
const Boleto = require('../../lib/boleto')(testBanks)

const dataEmissao = moment(moment('2018-10-25 20:48:01.981+00').utc().format('YYYY-MM-DD')).valueOf()
const dataVencimento = moment(moment('2018-10-31 02:00:00+00').utc().format('YYYY-MM-DD')).valueOf()

const boletoOptions = {
banco: 'test',
cedente: 'SOGNI COMERCIO E SERVICO DIGITAL | Pagar.me Pagamentos S/A',
cedente_cnpj: '18727053000174',
agencia: '1229',
codigo_cedente: '469',
carteira: '26',
data_emissao: new Date(dataEmissao),
data_vencimento: new Date(dataVencimento),
valor: 1000,
nosso_numero: 20615808,
numero_documento: 20615808,
pagador: 'Abilio Cipriano',
local_de_pagamento: 'PAGÁVEL EM QUALQUER BANCO ATÉ O VENCIMENTO.',
instrucoes: 'teste'
}

module.exports = new Boleto(boletoOptions)
25 changes: 25 additions & 0 deletions test/mocks/boleto-2018-11-09.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const moment = require('moment')
const testBanks = require('./banks')
const Boleto = require('../../lib/boleto')(testBanks)

const dataEmissao = moment(moment('2018-10-25 20:48:01.981+00').utc().format('YYYY-MM-DD')).valueOf()
const dataVencimento = moment(moment('2018-11-09 02:00:00+00').utc().format('YYYY-MM-DD')).valueOf()

const boletoOptions = {
banco: 'test',
cedente: 'SOGNI COMERCIO E SERVICO DIGITAL | Pagar.me Pagamentos S/A',
cedente_cnpj: '18727053000174',
agencia: '1229',
codigo_cedente: '469',
carteira: '26',
data_emissao: new Date(dataEmissao),
data_vencimento: new Date(dataVencimento),
valor: 1000,
nosso_numero: 20615808,
numero_documento: 20615808,
pagador: 'Abilio Cipriano',
local_de_pagamento: 'PAGÁVEL EM QUALQUER BANCO ATÉ O VENCIMENTO.',
instrucoes: 'teste'
}

module.exports = new Boleto(boletoOptions)
20 changes: 6 additions & 14 deletions test/unit/boleto-object.spec.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
const R = require('ramda')
const moment = require('moment')

const chai = require('chai')
chai.use(require('chai-subset'))
chai.use(require('chai-datetime'))
const expect = chai.expect

const testBanks = {
test: {
options: {
logoURL: 'https://pagar.me/img.png',
codigo: '123'
},
barcodeData: R.always('123'),
linhaDigitavel: R.always('123')
}
}
const testBanks = require('../mocks/banks')
const Boleto = require('../../lib/boleto')(testBanks)

describe('Boleto Object', () => {
Expand Down Expand Up @@ -52,11 +44,11 @@ describe('Boleto Object', () => {
})

it('has current date as data_emissao', () => {
expect(boleto.data_emissao).to.equalDate(new Date())
expect(moment(boleto.data_emissao).format()).to.be.equal(moment().utc().format())
})

it('has five days past data_emissao as data_vencimento', () => {
expect(boleto.data_vencimento).to.equalDate(new Date(new Date().getTime() + (5 * 24 * 3600 * 1000)))
expect(moment(boleto.data_vencimento).format()).to.be.equal(moment().utc().add(5, 'days').format())
})
})

Expand Down Expand Up @@ -123,8 +115,8 @@ describe('Boleto Object', () => {
})

it('contains dates in options', () => {
expect(boleto.data_emissao).to.equalTime(boletoOptions.data_emissao)
expect(boleto.data_vencimento).to.equalTime(boleto.data_vencimento)
expect(moment(boleto.data_emissao).format()).to.be.equal(moment(boletoOptions.data_emissao).format())
expect(moment(boleto.data_vencimento).format()).to.be.equal(moment(boleto.data_vencimento).format())
})

it('contains formatted nosso_numero_dv', () => {
Expand Down
34 changes: 34 additions & 0 deletions test/unit/boleto-render.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const boletoMock20181109 = require('../mocks/boleto-2018-11-09')
const boletoMock20181031 = require('../mocks/boleto-2018-10-31')
const expect = require('chai').expect

describe('Boleto render', () => {
describe('boleto from 2018-11-09', () => {
let boletoHTML20181109, boletoHTML20181031
before(() => {
return new Promise((resolve, reject) => {
boletoMock20181109.renderHTML(function (html) {
boletoHTML20181109 = html
resolve(html)
})
})
})

before(() => {
return new Promise((resolve, reject) => {
boletoMock20181031.renderHTML(function (html) {
boletoHTML20181031 = html
resolve(html)
})
})
})

it('should have the expiration date as 09/11/2018', () => {
expect(boletoHTML20181109.includes('09/11/2018')).to.be.equal(true)
})

it('should have the expiration date as 31/10/2018', () => {
expect(boletoHTML20181031.includes('31/10/2018')).to.be.equal(true)
})
})
})

0 comments on commit 2c40f4d

Please sign in to comment.