File size: 1,244 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
import { act, renderHook } from "@/test/render";

import { defaultDateLib } from "../classes/DateLib";
import type { DayPickerProps } from "../types";

import { useMulti } from "./useMulti";

describe("useMulti", () => {
  it("uses the selected value from props when onSelect is provided", () => {
    const mockOnSelect = jest.fn();
    const selectedDates = [new Date(2023, 9, 1), new Date(2023, 9, 2)];
    const props: DayPickerProps = {
      mode: "multiple",
      selected: selectedDates,
      onSelect: mockOnSelect,
    };

    const { result } = renderHook(() => useMulti(props, defaultDateLib));

    expect(result.current.selected).toBe(selectedDates);
  });

  it("uses the internally selected value when onSelect is not provided", () => {
    const initialSelectedDates = [new Date(2023, 9, 1), new Date(2023, 9, 2)];
    const props: DayPickerProps = {
      mode: "multiple",
      selected: initialSelectedDates,
    };

    const { result } = renderHook(() => useMulti(props, defaultDateLib));

    act(() => {
      result.current.select?.(new Date(2023, 9, 3), {}, {} as React.MouseEvent);
    });

    expect(result.current.selected).toEqual([
      ...initialSelectedDates,
      new Date(2023, 9, 3),
    ]);
  });
});