mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-02 02:11:33 +00:00
Load only specified flatpickr locale
Dynamically import only the requested flatpickr locale. English locale is bundled by default, so passing null triggers flatpickr's built-in English fallback without an explicit import.
This commit is contained in:
43
spec/javascripts/stimulus/flatpickr_controller_test.js
Normal file
43
spec/javascripts/stimulus/flatpickr_controller_test.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* @jest-environment jsdom
|
||||
*/
|
||||
|
||||
import { Application } from "stimulus";
|
||||
import FlatpickrController from "../../../app/webpacker/controllers/flatpickr_controller.js";
|
||||
|
||||
describe("FlatpickrController", () => {
|
||||
beforeAll(() => {
|
||||
const application = Application.start();
|
||||
application.register("flatpickr", FlatpickrController);
|
||||
});
|
||||
|
||||
describe("#importFlatpickrLocale", () => {
|
||||
describe("returns null to trigger flatpickr fallback to english", () => {
|
||||
test.each([
|
||||
["when no base_locale is set", {}],
|
||||
["when base_locale doesn't match a Flatpickr locale", { base_locale: "invalid-locale" }],
|
||||
["when base_locale is 'en'", { base_locale: "en" }],
|
||||
])("%s", async (_description, i18nData) => {
|
||||
const I18n = i18nData;
|
||||
const controller = new FlatpickrController();
|
||||
const locale = await controller.importFlatpickrLocale(I18n.base_locale);
|
||||
expect(locale).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
it("returns locale object for a supported locale (fr)", async () => {
|
||||
const controller = new FlatpickrController();
|
||||
const locale = await controller.importFlatpickrLocale("fr");
|
||||
expect(locale).toBeInstanceOf(Object);
|
||||
expect(locale).toHaveProperty("weekAbbreviation");
|
||||
expect(locale.weekAbbreviation).toBe("Sem");
|
||||
});
|
||||
|
||||
it("caches the locale object for repeated calls", async () => {
|
||||
const controller = new FlatpickrController();
|
||||
const locale1 = await controller.importFlatpickrLocale("fr");
|
||||
const locale2 = await controller.importFlatpickrLocale("fr");
|
||||
expect(locale1).toBe(locale2);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user