UNPKG

2.09 kBPlain TextView Raw
1import * as defaultFormatters from "../formatters";
2import { dateLib } from "../lib";
3
4import { getFormatters } from "./getFormatters";
5
6const customFormattersMock = {
7 formatMonthCaption: jest.fn(),
8 formatYearCaption: jest.fn()
9};
10
11test("returns default formatters when custom formatters are not provided", () => {
12 const formatters = getFormatters(undefined);
13 expect(formatters).toEqual(defaultFormatters);
14});
15
16test("merges custom formatters with default formatters", () => {
17 const formatters = getFormatters(customFormattersMock);
18 expect(formatters).toEqual(expect.objectContaining(customFormattersMock));
19});
20
21test("assigns `formatMonthCaption` to `formatCaption` if `formatCaption` is not defined", () => {
22 const result = getFormatters({ formatMonthCaption: () => "customMonth" });
23 expect(result.formatCaption(new Date(), {}, dateLib)).toBe("customMonth");
24 expect(result.formatMonthCaption(new Date(), {}, dateLib)).toBe(
25 "customMonth"
26 );
27});
28
29test("does not overwrite `formatCaption` if already defined", () => {
30 const result = getFormatters({
31 formatMonthCaption: () => "customMonth",
32 formatCaption: () => "customCaption"
33 });
34 expect(result.formatCaption(new Date(), {}, dateLib)).toBe("customCaption");
35 expect(result.formatMonthCaption(new Date(), {}, dateLib)).toBe(
36 "customMonth"
37 );
38});
39
40test("assigns `formatYearCaption` to `formatYearDropdown` if `formatYearDropdown` is not defined", () => {
41 const result = getFormatters({ formatYearCaption: () => "customYear" });
42 expect(result.formatYearDropdown(new Date(0).getFullYear())).toBe(
43 "customYear"
44 );
45 expect(result.formatYearCaption(new Date(0).getFullYear())).toBe(
46 "customYear"
47 );
48});
49
50test("does not overwrite `formatYearDropdown` if already defined", () => {
51 const result = getFormatters({
52 formatYearCaption: () => "customYear",
53 formatYearDropdown: () => "customDropdown"
54 });
55 expect(result.formatYearDropdown(new Date().getFullYear())).toBe(
56 "customDropdown"
57 );
58 expect(result.formatYearCaption(new Date().getFullYear())).toBe("customYear");
59});