'How do I instantiate a Dinero object with an amount that I have as a number?

I am currently designing a test that uses Cypress to add up product prices in a confirmation email and compare them with the total price. I read that you use Dinero.js in Typescript. Is that right? Is there anything better? How do I instantiate a Dinero Object with my number variable? I already have this:

import { Dinero, dinero, add } from 'dinero.js'
import { USD } from '@dinero.js/currencies'
import orders from '../fixtures/orders.json'

describe('Theme: Mails', () => {
  const serverId = 'TODO' // Replace SERVER_ID with an actual Mailosaur Server ID
  const testEmail = `tell-whispered@${serverId}.mailosaur.net`
  const orderTotal: Dinero = dinero({ amount: 0, currency: USD })

  it('The body of a confirmation mail shall contain strings', () => {
    cy.mailosaurGetMessage(
      serverId,
      {
        sentTo: testEmail,
      },
      { receivedAfter: new Date('2022-01-01T00:00:00Z') }
    ).then((message) => {

      orders.Products.forEach((aProduct: any) => {
        expect(message!.text!.body!.indexOf(aProduct.price) > -1).to.be.true
        // i want to have aProduct.price as Dinero object now, this fails:
        // const currentPrice = dinero({ amount: aProduct.price, currency: USD })
      })

      expect(message!.text!.body!).to.have.string(orders.OrderTotal)
      expect(orderTotal.equalsTo(Dinero({ amount: 3007400 }))).to.be.true
    })
  })
})

Once I've done that, how do I sum multiple objects then?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source