unit testing filter

This commit is contained in:
Carlos Jimenez Ruiz 2020-08-13 10:03:50 +02:00
parent de1a70aee7
commit f38701bb43
12 changed files with 108 additions and 13 deletions

View File

@ -70,7 +70,8 @@ module.exports = {
`front`,
`modules`,
`front/node_modules`,
`node_modules`
`node_modules`,
`print`
],
// An array of file extensions your modules use
@ -153,6 +154,7 @@ module.exports = {
// The glob patterns Jest uses to detect test files
testMatch: [
'**/front/**/*.spec.js',
'**/print/**/*.spec.js',
// 'loopback/**/*.spec.js',
// 'modules/*/back/**/*.spec.js'
// "**/__tests__/**/*.[jt]s?(x)",

View File

@ -4,8 +4,8 @@ let env = process.env.NODE_ENV ? process.env.NODE_ENV : 'development';
let configPath = `/etc/salix`;
let config = require('../config/print.json');
let configFiles = [
`${appPath}/config/print.local.json`,
`${appPath}/config/print.${env}.json`,
`../config/print.local.json`,
`../config/print.${env}.json`,
`${configPath}/print.json`,
`${configPath}/print.local.json`,
`${configPath}/print.${env}.json`

View File

@ -2,9 +2,13 @@ const Vue = require('vue');
const config = require('../config');
const defaultLocale = config.i18n.locale;
Vue.filter('currency', function(value, currency = 'EUR', locale = defaultLocale) {
const currency = function(value, currency = 'EUR', locale = defaultLocale) {
if (!locale) locale = defaultLocale;
return new Intl.NumberFormat(locale, {
style: 'currency', currency
}).format(parseFloat(value));
});
};
Vue.filter('currency', currency);
module.exports = currency;

View File

@ -1,7 +1,11 @@
const Vue = require('vue');
const strftime = require('strftime');
Vue.filter('date', function(value, specifiers = '%d-%m-%Y') {
const date = function(value, specifiers = '%d-%m-%Y') {
if (!(value instanceof Date)) value = new Date(value);
return strftime(specifiers, value);
});
};
Vue.filter('date', date);
module.exports = date;

View File

@ -2,9 +2,13 @@ const Vue = require('vue');
const config = require('../config');
const defaultLocale = config.i18n.locale;
Vue.filter('number', function(value, locale = defaultLocale) {
const number = function(value, locale = defaultLocale) {
if (!locale) locale = defaultLocale;
return new Intl.NumberFormat(locale, {
style: 'decimal'
}).format(parseFloat(value));
});
};
Vue.filter('number', number);
module.exports = number;

View File

@ -2,11 +2,15 @@ const Vue = require('vue');
const config = require('../config');
const defaultLocale = config.i18n.locale;
Vue.filter('percentage', function(value, minFraction = 2, maxFraction = 2, locale = defaultLocale) {
const percentage = function(value, minFraction = 2, maxFraction = 2, locale = defaultLocale) {
if (!locale) locale = defaultLocale;
return new Intl.NumberFormat(locale, {
style: 'percent',
minimumFractionDigits: minFraction,
maximumFractionDigits: maxFraction
}).format(parseFloat(value));
});
};
Vue.filter('percentage', percentage);
module.exports = percentage;

View File

@ -0,0 +1,16 @@
// Extended locale intl polyfill
const IntlPolyfill = require('intl');
Intl.NumberFormat = IntlPolyfill.NumberFormat;
Intl.DateTimeFormat = IntlPolyfill.DateTimeFormat;
import currency from '../currency.js';
describe('currency filter', () => {
it('should filter the currency in spanish as default', () => {
expect(currency(999, 'EUR')).toEqual('999,00 €');
});
it('should filter the currency in english', () => {
expect(currency(999, 'EUR', 'en')).toEqual('€999.00');
});
});

View File

@ -0,0 +1,27 @@
import date from '../date.js';
describe('date filter', () => {
const superDuperDate = new Date('February 18, 1984 @ 11:30:00 am');
it('should filter the date as %d-%m-%Y by default', () => {
expect(date(superDuperDate)).toEqual('18-02-1984');
});
it('should filter the date as %m-%d-%Y', () => {
const dateFormat = '%m-%d-%Y';
expect(date(superDuperDate, dateFormat)).toEqual('02-18-1984');
});
it('should filter the date as %y-%d-%m', () => {
const dateFormat = '%y-%d-%m';
expect(date(superDuperDate, dateFormat)).toEqual('84-18-02');
});
it('should filter the date as %Y-%d-%m', () => {
const dateFormat = '%Y-%d-%m';
expect(date(superDuperDate, dateFormat)).toEqual('1984-18-02');
});
});

View File

@ -0,0 +1,9 @@
import number from '../number.js';
describe('number filter', () => {
const superDuperNumber = 18021984;
it('should filter the number with commas by default', () => {
expect(number(superDuperNumber)).toEqual('18,021,984');
});
});

View File

@ -0,0 +1,11 @@
import percentage from '../percentage.js';
describe('percentage filter', () => {
it('should filter the percentage also round it correctly', () => {
expect(percentage(99.9999999999999999 / 100)).toEqual('100.00%');
});
it('should filter the percentage and round it correctly', () => {
expect(percentage(1.25444444444444444 / 100)).toEqual('1.25%');
});
});

View File

@ -0,0 +1,10 @@
import uppercase from '../uppercase.js';
describe('uppercase filter', () => {
it('should filter the string to uppercase', () => {
let lowerCase = 'text';
let upperCase = 'TEXT';
expect(uppercase(lowerCase)).toEqual(upperCase);
});
});

View File

@ -1,5 +1,9 @@
const Vue = require('vue');
Vue.filter('uppercase', function(value) {
const uppercase = function(value) {
return value.toUpperCase();
});
};
Vue.filter('uppercase', uppercase);
module.exports = uppercase;