UNPKG

@shko.online/componentframework-mock

Version:

Mocking library to help with testing PowerApps Component Framework Components

106 lines (105 loc) 3.42 kB
/* Copyright (c) 2022 Betim Beja and Shko Online LLC Licensed under the MIT license. */ import { stub } from 'sinon'; const symbolToCurrency = { '€': 'EUR', $: 'USD' }; export 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 = void 0; this.locale = 'en-US'; this.formatCurrency = 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 = stub(); this.formatDecimal.callsFake((value, precision) => { return value.toLocaleString(this.locale, { maximumFractionDigits: precision, minimumFractionDigits: precision }); }); this.formatDateAsFilterStringInUTC = stub(); this.formatDateAsFilterStringInUTC.callsFake((value, includeTime) => { return value.toLocaleString(this.locale, { day: '2-digit', year: 'numeric', month: '2-digit' }); }); this.formatDateLong = stub(); this.formatDateLong.callsFake(value => { return value.toLocaleString(this.locale, { dateStyle: 'long' }); }); this.formatDateLongAbbreviated = stub(); this.formatDateLongAbbreviated.callsFake(value => { return value.toLocaleString(this.locale, { dateStyle: 'medium' }); }); this.formatDateShort = 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 = stub(); this.formatDateYearMonth.callsFake(value => { return value.toLocaleString(this.locale, { year: 'numeric', month: 'long' }); }); this.formatInteger = stub(); this.formatInteger.callsFake(value => { return value.toLocaleString(this.locale, {}); }); this.formatLanguage = stub(); this.formatLanguage.callsFake(value => { return `${value}`; }); this.formatTime = stub(); this.formatTime.callsFake((value, behavior) => { throw new Error('Method not implemented.'); }); this.getWeekOfYear = 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); }); } }