From 45ce1fa39bce7ab918532ed89d3c431771640fcb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A1r=20=C3=96rlygsson?= Date: Thu, 19 Jan 2023 15:46:53 +0100 Subject: [PATCH] tests: Add failing locale test: bad value is parsed using default locale Parsing `valueEn` as a Portugese date should fail, but accidentally gets parsed using the default/en-US locale, because parseDate internally retries and accidentally omits[^1] the original locale option that time around. [^1]: https://github.com/Hacker0x01/react-datepicker/blob/5c1d6d923931535f105f3dddbb6f3e10fd8dd25c/src/date_utils.js#L121 --- test/date_utils_test.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/date_utils_test.js b/test/date_utils_test.js index 60d0016ac..0a90d33d3 100644 --- a/test/date_utils_test.js +++ b/test/date_utils_test.js @@ -934,6 +934,29 @@ describe("date_utils", function () { assert(isEqual(actual, expected)); }); + it("should parse date based on locale w/o strict", () => { + const valuePt = "26. fev 1995"; + const valueEn = "26. feb 1995"; + + const locale = "pt-BR"; + const dateFormat = "d. MMM yyyy"; + + const expected = new Date(1995, 1, 26); + + assert( + isEqual(parseDate(valuePt, dateFormat, locale, false), expected), + "valuePT with pt-BR" + ); + assert( + isEqual(parseDate(valueEn, dateFormat, null, false), expected), + "valueEn with default (en-US)" + ); + expect( + parseDate(valueEn, dateFormat, locale, false), + "valuePt with default (en-US)" + ).to.be.null; + }); + it("should not parse date based on locale without a given locale", () => { const value = "26/05/1995"; const dateFormat = "P";