From dcf5379f4990783680f664b8136a660ee5e743c0 Mon Sep 17 00:00:00 2001
From: "Arthur E. Jones" <PartyLich@gmail.com>
Date: Sun, 3 Jul 2022 10:53:49 -0500
Subject: [PATCH] test: add parseDate tests

---
 .../components/modals/ImportTransactions.js   |   2 +-
 .../modals/ImportTransactions.test.js         | 110 ++++++++++++++++++
 2 files changed, 111 insertions(+), 1 deletion(-)
 create mode 100644 packages/loot-design/src/components/modals/ImportTransactions.test.js

diff --git a/packages/loot-design/src/components/modals/ImportTransactions.js b/packages/loot-design/src/components/modals/ImportTransactions.js
index 4d3e474eb..6c6fc91be 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 000000000..28953ddbc
--- /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');
+        }
+      }
+    });
+  });
+});
-- 
GitLab