UNPKG

@shko.online/componentframework-mock

Version:

Mocking library to help with testing PowerApps Component Framework Components

112 lines (110 loc) 3.67 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FormattingMock = void 0; var _sinon = require("sinon"); /* Copyright (c) 2022 Betim Beja and Shko Online LLC Licensed under the MIT license. */ const symbolToCurrency = { '€': 'EUR', $: 'USD' }; class FormattingMock { constructor() { this.formatCurrency = void 0; this.formatDecimal = void 0; this.formatDateAsFilterStringInUTC = void 0; this.formatDateLong = void 0; this.formatDateLongAbbreviated = void 0; this.formatDateShort = void 0; this.formatDateYearMonth = void 0; this.formatInteger = void 0; this.formatLanguage = void 0; this.formatTime = void 0; this.getWeekOfYear = void 0; this.locale = 'en-US'; this.formatCurrency = (0, _sinon.stub)(); this.formatCurrency.callsFake((value, precision, currencySymbol) => { const currency = currencySymbol ? symbolToCurrency[currencySymbol] : undefined; return (currencySymbol && !currency ? currencySymbol : '') + value.toLocaleString(this.locale, { style: currency ? 'currency' : 'decimal', currency, maximumFractionDigits: precision, minimumFractionDigits: precision }); }); this.formatDecimal = (0, _sinon.stub)(); this.formatDecimal.callsFake((value, precision) => { return value.toLocaleString(this.locale, { maximumFractionDigits: precision, minimumFractionDigits: precision }); }); this.formatDateAsFilterStringInUTC = (0, _sinon.stub)(); this.formatDateAsFilterStringInUTC.callsFake((value, includeTime) => { return value.toLocaleString(this.locale, { day: '2-digit', year: 'numeric', month: '2-digit' }); }); this.formatDateLong = (0, _sinon.stub)(); this.formatDateLong.callsFake(value => { return value.toLocaleString(this.locale, { dateStyle: 'long' }); }); this.formatDateLongAbbreviated = (0, _sinon.stub)(); this.formatDateLongAbbreviated.callsFake(value => { return value.toLocaleString(this.locale, { dateStyle: 'medium' }); }); this.formatDateShort = (0, _sinon.stub)(); this.formatDateShort.callsFake((value, includeTime) => { if (includeTime === true) { return value.toLocaleString(this.locale, { dateStyle: 'short', timeStyle: 'short' }).replace(',', ''); } return value.toLocaleDateString(this.locale, { dateStyle: 'short' }); }); this.formatDateYearMonth = (0, _sinon.stub)(); this.formatDateYearMonth.callsFake(value => { return value.toLocaleString(this.locale, { year: 'numeric', month: 'long' }); }); this.formatInteger = (0, _sinon.stub)(); this.formatInteger.callsFake(value => { return value.toLocaleString(this.locale, {}); }); this.formatLanguage = (0, _sinon.stub)(); this.formatLanguage.callsFake(value => { return `${value}`; }); this.formatTime = (0, _sinon.stub)(); this.formatTime.callsFake((value, behavior) => { throw new Error('Method not implemented.'); }); this.getWeekOfYear = (0, _sinon.stub)(); this.getWeekOfYear.callsFake(value => { const leapYear = value.getFullYear() % 4 === 0; const monthDays = [31, leapYear ? 29 : 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; let dayOfYear = 0; for (let i = 0; i < value.getMonth(); i++) { dayOfYear += monthDays[i]; } dayOfYear += value.getDate(); return Math.ceil(dayOfYear / 7); }); } } exports.FormattingMock = FormattingMock;