test: add parseDate tests

This commit is contained in:
Arthur E. Jones 2022-07-03 10:53:49 -05:00 committed by James Long
parent 2897b647a1
commit dcf5379f49
2 changed files with 111 additions and 1 deletions

View file

@ -43,7 +43,7 @@ const re = {
'dd mm yy': reTwoDig
};
function parseDate(str, order) {
export function parseDate(str, order) {
if (typeof str !== 'string') {
return null;
}

View file

@ -0,0 +1,110 @@
import { parseDate } from './ImportTransactions';
describe('Import transactions', function() {
describe('date parsing', function() {
it('should not parse', function() {
const invalidInputs = [
{ str: '', order: 'yyyy mm dd' },
{ str: null, order: 'yyyy mm dd' },
{ str: 42, order: 'yyyy mm dd' },
{ str: {}, order: 'yyyy mm dd' },
{ str: [], order: 'yyyy mm dd' },
{ str: 'invalid', order: 'yyyy mm dd' },
{ str: '2020 Dec 24', order: 'yyyy mm dd' },
{ str: '12 24 20', order: 'mm dd yyyy' },
{ str: '20 12 24', order: 'yyyy mm dd' },
{ str: '2020 12 24', order: 'yy mm dd' },
{ str: '12 24 2020', order: 'mm dd yy' }
];
for (const { str, order } of invalidInputs) {
expect(parseDate(str, order)).toBe(null);
}
});
it('should parse', function() {
const validInputs = [
{
order: 'yyyy mm dd',
cases: [
'20201224',
'2020 12 24',
'2020-12-24',
'2020/12/24',
' 2020 / 12 / 24',
'2020/12/24 ',
'2020 12-24 '
]
},
{
order: 'yy mm dd',
cases: [
'201224',
'20 12 24',
'20-12-24',
'20/12/24',
'20/12/24',
'20/12/24 ',
'20 12-24 '
]
},
{
order: 'mm dd yyyy',
cases: [
'12242020',
'12 24 2020 ',
'12-24-2020',
'12/24/2020',
' 12/24/2020',
'12/24/2020',
'12 24-2020'
]
},
{
order: 'mm dd yy',
cases: [
'122420',
'12 24 20 ',
'12-24-20',
'12/24/20',
' 12/24/20',
'12/24/20',
'12 24-20'
]
},
{
order: 'dd mm yyyy',
cases: [
'24122020',
'24 12 2020 ',
'24-12-2020',
'24/12/2020',
' 24/12/2020',
'24/12/2020 ',
'24-12 2020'
]
},
{
order: 'dd mm yy',
cases: [
'241220',
'2412 20 ',
'24-12-20',
'24/12/20',
' 24/12/20',
'24/12/20 ',
'24 12-20 '
]
}
];
for (const { order, cases } of validInputs) {
for (const str of cases) {
const parsed = parseDate(str, order);
expect(typeof parsed).toBe('string');
expect(parsed).toBe('2020-12-24');
}
}
});
});
});