import { padMoney, moneyFormatter, date } from '../../etc/format.js';
import moment from 'moment';

describe('padMoney', function() {
    it('should handle null', function() {
        expect(padMoney()).to.equal('0.00');
    });

    it('should handle integers', function() {
        expect(padMoney(4)).to.equal('4.00');
    });

    it('should pad short numbers', function() {
        expect(padMoney(4.2)).to.equal('4.20');
    });

    it('should round', function() {
        expect(padMoney(4.231)).to.equal('4.23');
        expect(padMoney(4.2349)).to.equal('4.23');
        expect(padMoney(4.2351)).to.equal('4.24');
        expect(padMoney(4.238)).to.equal('4.24');
    });

    it('should add commas by default', function() {
        expect(padMoney(4000.23)).to.equal('4,000.23');
        expect(padMoney(123456)).to.equal('123,456.00');
        expect(padMoney(1234.00)).to.equal('1,234.00');
    });

    it('should format based on options', function() {
        expect(padMoney(4000.23, '.', ',')).to.equal('4.000,23');
        expect(padMoney(123456, ',', '.', 4)).to.equal('12,3456.00');
        expect(padMoney(1234.00, ' ', '.')).to.equal('1 234.00');
    });

    it('should treat negative numbers as positive ones', function() {
        expect(padMoney(-400)).to.equal('400.00');
    });
});

describe('moneyFormatter', function() {
    it('should return a function', function() {
        expect(moneyFormatter('en-US')).to.be.a('function');
    });

    it('should format us dollars', function() {
        expect(moneyFormatter('en-US')(5200.56)).to.equal('$5,200.56');
        expect(moneyFormatter('en-US')(100.23)).to.equal('$100.23');
        expect(moneyFormatter('en-US')(123456)).to.equal('$123,456.00');
    });
});

describe('date', function() {
    let offset = (new Date()).getTimezoneOffset() * 60000;

    if (moment().isDST()) {
        offset += 60 * 60 * 1000; // add a goddamn hour
    }

    it('should have a default format', function() {
        expect(date(1452857416575 + offset))
            .to.equal('Jan. 15, 2016, 11:30 AM');
    });

    it('should handle the meridian', function() {
        expect(date(1452816000000 + offset))
            .to.equal('Jan. 15, 2016, 12:00 AM');
        expect(date(1452859200000 + offset))
            .to.equal('Jan. 15, 2016, 12:00 PM');
    });

    it('should take a date', function() {
        expect(date(new Date(1452816000000 + offset), '%b. %e'))
            .to.equal('Jan. 15');
    });

    it('should format properly', function() {
        expect(date(new Date(1452816000000 + offset), '%Y-%n-%d'))
            .to.equal('2016-01-15');

        expect(date(new Date(1454486400000 + offset), '%B %c'))
            .to.equal('February 3');
    });
});
