Repository: flatpickr/flatpickr
Pull request #2276 (2020-09-15 14:23:59)

Fix prev/next year buttons in monthSelect (#2275)
This commit removes a needless restriction in the click handling logic
on the monthSelect plugin's prev/next year buttons:

* the prev. year button would only decrement the year
  after selecting January, and
* the next year button would only increment the year
  after selecting December.

It also adds a test to protect against regressions of this bug.

### Additional commits

The bugfix is preceded by a commit that rearranges the test files to match the file structure of the source files they test.


Patch:
diff --git a/src/plugins/monthSelect/index.ts b/src/plugins/monthSelect/index.ts
index d72804d5..b66840db 100644
--- a/src/plugins/monthSelect/index.ts
+++ b/src/plugins/monthSelect/index.ts
@@ -50,13 +50,7 @@ function monthSelectPlugin(pluginConfig?: Partial<Config>): Plugin {
         e.preventDefault();
         e.stopPropagation();
 
-        const selectedMonth = fp.rContainer
-          ?.querySelector<ElementDate>(".flatpickr-monthSelect-month.selected")!
-          .dateObj.getMonth();
-
-        if (selectedMonth === 0) {
-          fp.currentYear--;
-        }
+        fp.currentYear--;
         selectYear();
       });
 
@@ -64,13 +58,7 @@ function monthSelectPlugin(pluginConfig?: Partial<Config>): Plugin {
         e.preventDefault();
         e.stopPropagation();
 
-        const selectedMonth = fp.rContainer
-          ?.querySelector<ElementDate>(".flatpickr-monthSelect-month.selected")!
-          .dateObj.getMonth();
-
-        if (selectedMonth === 11) {
-          fp.currentYear++;
-        }
+        fp.currentYear++;
         selectYear();
       });
     }


Test patch:
diff --git a/src/__tests__/build.spec.ts b/__tests__/build.spec.ts
similarity index 100%
rename from src/__tests__/build.spec.ts
rename to __tests__/build.spec.ts
diff --git a/src/__tests__/index.spec.ts b/__tests__/src/index.spec.ts
similarity index 98%
rename from src/__tests__/index.spec.ts
rename to __tests__/src/index.spec.ts
index 4a2d11ec..b79e359d 100644
--- a/src/__tests__/index.spec.ts
+++ b/__tests__/src/index.spec.ts
@@ -1,8 +1,8 @@
-import flatpickr from "../index";
-import { Russian } from "../l10n/ru";
-import { Instance, DayElement } from "../types/instance";
-import { Options, DateRangeLimit } from "../types/options";
-import confirmDatePlugin from "../plugins/confirmDate/confirmDate";
+import flatpickr from "index";
+import { Russian } from "l10n/ru";
+import { Instance, DayElement } from "types/instance";
+import { Options, DateRangeLimit } from "types/options";
+import confirmDatePlugin from "plugins/confirmDate/confirmDate";
 
 flatpickr.defaultConfig.animate = false;
 flatpickr.defaultConfig.closeOnSelect = true;
@@ -1594,4 +1594,25 @@ describe("flatpickr", () => {
       expect((fp.altInput as HTMLInputElement).value).toEqual("December");
     });
   });
+
+  describe("events + hooks", () => {
+    describe("onOpen", () => {
+      it("should fire only once", () => {
+        let timesFired = 0;
+
+        const fp = createInstance({
+          onOpen: () => timesFired++,
+        });
+
+        fp.open();
+        expect(timesFired).toEqual(1);
+      });
+    });
+  });
+
+  describe("server-side rendering", () => {
+    it("can be imported", () => {
+      expect(typeof flatpickr).toEqual("function");
+    });
+  });
 });
diff --git a/src/plugins/monthSelect/tests.spec.ts b/__tests__/src/plugins/monthSelect/index.spec.ts
similarity index 66%
rename from src/plugins/monthSelect/tests.spec.ts
rename to __tests__/src/plugins/monthSelect/index.spec.ts
index bb1f4cb3..c3903615 100644
--- a/src/plugins/monthSelect/tests.spec.ts
+++ b/__tests__/src/plugins/monthSelect/index.spec.ts
@@ -1,5 +1,5 @@
-import flatpickr from "../../index";
-import monthSelectPlugin from "./index";
+import flatpickr from "index";
+import monthSelectPlugin from "plugins/monthSelect/index";
 import { German } from "l10n/de";
 import { Instance } from "types/instance";
 import { Options } from "types/options";
@@ -59,4 +59,27 @@ describe("monthSelect", () => {
 
     expect(fp.altInput.value).toEqual("03 19");
   });
+
+  describe("year nav", () => {
+    describe("next/prev year buttons", () => {
+      it("should increment/decrement year when clicked (#2275)", () => {
+        const initYear = 2020;
+
+        const fp = createInstance({
+          plugins: [monthSelectPlugin()],
+          defaultDate: new Date(`${initYear}-03-20`),
+        }) as Instance;
+
+        const prevButton = fp.monthNav.querySelector(".flatpickr-prev-month")!;
+        prevButton.dispatchEvent(new MouseEvent("click"));
+
+        expect(fp.currentYear).toEqual(initYear - 1);
+
+        const nextButton = fp.monthNav.querySelector(".flatpickr-next-month")!;
+        nextButton.dispatchEvent(new MouseEvent("click"));
+
+        expect(fp.currentYear).toEqual(initYear);
+      });
+    });
+  });
 });
diff --git a/src/__tests__/rangePlugin.spec.ts b/__tests__/src/plugins/rangePlugin.spec.ts
similarity index 93%
rename from src/__tests__/rangePlugin.spec.ts
rename to __tests__/src/plugins/rangePlugin.spec.ts
index a6e782dc..a7e40b85 100644
--- a/src/__tests__/rangePlugin.spec.ts
+++ b/__tests__/src/plugins/rangePlugin.spec.ts
@@ -1,7 +1,7 @@
 import flatpickr from "index";
 import rangePlugin from "plugins/rangePlugin";
-import { Options } from "../types/options";
-import { Instance } from "../types/instance";
+import { Options } from "types/options";
+import { Instance } from "types/instance";
 
 type RangeInstance = Instance & {
   secondInput: HTMLInputElement;
diff --git a/src/__tests__/hooks.spec.ts b/src/__tests__/hooks.spec.ts
deleted file mode 100644
index aa8920b7..00000000
--- a/src/__tests__/hooks.spec.ts
+++ /dev/null
@@ -1,27 +0,0 @@
-import flatpickr from "index";
-import { Options } from "../types/options";
-import { Instance } from "../types/instance";
-
-flatpickr.defaultConfig.animate = false;
-
-jest.useFakeTimers();
-
-const createInstance = (config?: Options): Instance => {
-  return flatpickr(
-    document.createElement("input"),
-    config as Options
-  ) as Instance;
-};
-
-describe("Events + Hooks", () => {
-  it("should fire onOpen only once", () => {
-    let timesFired = 0;
-
-    const fp = createInstance({
-      onOpen: () => timesFired++,
-    });
-
-    fp.open();
-    expect(timesFired).toEqual(1);
-  });
-});
diff --git a/src/__tests__/ssr.spec.ts b/src/__tests__/ssr.spec.ts
deleted file mode 100644
index f09ebcdd..00000000
--- a/src/__tests__/ssr.spec.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-import flatpickr from "../index";
-
-describe("flatpickr SSR", () => {
-  it("can be imported", () => {
-    expect(typeof flatpickr).toEqual("function");
-  });
-});