File size: 2,388 Bytes
cf86710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { DateLib } from "../classes/DateLib.js";
import { enUS } from "../locale/en-US.js";
import { fr } from "../locale/fr.js";
import { it } from "../locale/it.js";
import { ja } from "../locale/ja.js";

import { getLabels } from "./getLabels.js";

describe("getLabels", () => {
  test("uses locale string labels when custom labels are not provided", () => {
    const dateLib = new DateLib({
      locale: {
        ...enUS,
        labels: { labelPrevious: "Indietro" },
      },
    });

    const labels = getLabels(undefined, dateLib.options);

    expect(labels.labelPrevious(new Date())).toBe("Indietro");
  });

  test("uses locale label functions when available", () => {
    const dateLib = new DateLib({
      locale: {
        ...enUS,
        labels: {
          labelWeekday: (date) => `weekday-${date.getDay()}`,
        },
      },
    });

    const labels = getLabels(undefined, dateLib.options);

    expect(labels.labelWeekday(new Date(2024, 0, 1))).toBe("weekday-1");
  });

  test("custom labels override locale labels", () => {
    const custom = { labelNext: () => "custom-next" };
    const labels = getLabels(custom, new DateLib({ locale: ja }).options);

    expect(labels.labelNext(new Date())).toBe("custom-next");
  });

  test("falls back to defaults when locale does not define a label", () => {
    const locale = { ...enUS, labels: {} };
    const labels = getLabels(undefined, new DateLib({ locale }).options);

    expect(labels.labelWeekNumber(1)).toBe("Week 1");
  });

  test("uses Italian locale label translations", () => {
    const dateLib = new DateLib({ locale: it });
    const labels = getLabels(undefined, dateLib.options);

    expect(labels.labelPrevious(new Date())).toBe("Vai al mese precedente");
    expect(labels.labelMonthDropdown()).toBe("Scegli il mese");
    expect(labels.labelWeekNumber(3)).toBe("Settimana 3");
    expect(labels.labelYearDropdown()).toBe("Scegli l’anno");
  });

  test("uses French locale label translations", () => {
    const dateLib = new DateLib({ locale: fr });
    const labels = getLabels(undefined, dateLib.options);

    expect(labels.labelPrevious(new Date())).toBe("Aller au mois précédent");
    expect(labels.labelMonthDropdown()).toBe("Choisir le mois");
    expect(labels.labelWeekNumber(2)).toBe("Semaine 2");
    expect(labels.labelYearDropdown()).toBe("Choisir l'année");
  });
});