Skip to content

A local-first personal finance app

test: add parseDate tests

Arthur E. Jonesdcf5379f4990
diff --git a/packages/loot-design/src/components/modals/ImportTransactions.js b/packages/loot-design/src/components/modals/ImportTransactions.js
index 4d3e474ebb..6c6fc91be2 100644
--- a/packages/loot-design/src/components/modals/ImportTransactions.js
+++ b/packages/loot-design/src/components/modals/ImportTransactions.js
@@ -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;
}
diff --git a/packages/loot-design/src/components/modals/ImportTransactions.test.js b/packages/loot-design/src/components/modals/ImportTransactions.test.js
new file mode 100644
index 0000000000..28953ddbcd
--- /dev/null
+++ b/packages/loot-design/src/components/modals/ImportTransactions.test.js
@@ -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');
+ }
+ }
+ });
+ });
+});