1 import { fireEvent, render, waitFor } from '@testing-library/react';
2 import { renderHook } from '@testing-library/react-hooks';
4 import { Autopay } from '@proton/payments';
6 import { getStoreWrapper } from '../contacts/tests/render';
7 import RenewToggle, { useRenewToggle } from './RenewToggle';
9 jest.mock('@proton/components/components/portal/Portal');
10 jest.mock('@proton/components/components/toggle/Toggle');
16 const { Wrapper } = getStoreWrapper();
18 it('should render', () => {
19 const { result } = renderHook(() => useRenewToggle({}), { wrapper: Wrapper });
20 const { container } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
22 expect(container).not.toBeEmptyDOMElement();
25 it('should be checked when RenewState is Active', () => {
26 const { result } = renderHook(() => useRenewToggle({}), { wrapper: Wrapper });
27 const { getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
29 expect(getByTestId('toggle-subscription-renew')).toHaveTextContent('CHECKED');
32 it('should be unchecked when RenewState is Disabled', () => {
33 const { result } = renderHook(() => useRenewToggle({ initialRenewState: Autopay.DISABLE }), { wrapper: Wrapper });
34 const { getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
36 expect(getByTestId('toggle-subscription-renew')).not.toHaveTextContent('CHECKED');
39 it('should show modal when user disables auto renew', () => {
40 const { result } = renderHook(() => useRenewToggle({}), { wrapper: Wrapper });
41 const { container, rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
43 fireEvent.click(getByTestId('toggle-subscription-renew'));
44 rerender(<RenewToggle {...result.current} />);
46 expect(container).toHaveTextContent('Our system will no longer auto-charge you using this payment method');
49 it('should not toggle off if user clicked Keep auto-pay/Cancel', async () => {
50 const { result, rerender: rerenderHook } = renderHook(() => useRenewToggle({}), { wrapper: Wrapper });
51 const { container, rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
53 fireEvent.click(getByTestId('toggle-subscription-renew'));
55 rerender(<RenewToggle {...result.current} />);
56 expect(container).toHaveTextContent('Are you sure?');
58 fireEvent.click(getByTestId('action-keep-autopay'));
59 await waitFor(() => {});
61 rerender(<RenewToggle {...result.current} />);
63 expect(getByTestId('toggle-subscription-renew')).toHaveTextContent('CHECKED');
66 it('should toggle off if user clicked Disable', async () => {
67 const { result, rerender: rerenderHook } = renderHook(() => useRenewToggle({}), { wrapper: Wrapper });
68 const { container, rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
70 fireEvent.click(getByTestId('toggle-subscription-renew'));
72 rerender(<RenewToggle {...result.current} />);
73 expect(container).toHaveTextContent('Are you sure?');
75 fireEvent.click(getByTestId('action-disable-autopay'));
76 await waitFor(() => {});
78 rerender(<RenewToggle {...result.current} />);
81 expect(getByTestId('toggle-subscription-renew')).not.toHaveTextContent('CHECKED');
85 it('should toggle on without modal', async () => {
86 const { result, rerender: rerenderHook } = renderHook(
87 () => useRenewToggle({ initialRenewState: Autopay.DISABLE }),
90 const { container, rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
92 fireEvent.click(getByTestId('toggle-subscription-renew'));
93 await waitFor(() => {});
95 rerender(<RenewToggle {...result.current} />);
97 expect(container).not.toHaveTextContent('Our system will no longer auto-charge you using this payment method');
99 expect(getByTestId('toggle-subscription-renew')).toHaveTextContent('CHECKED');
102 describe('useRenewToggle', () => {
103 it('onChange should return ENABLE if toggled on', async () => {
104 const { result } = renderHook(() => useRenewToggle({ initialRenewState: Autopay.DISABLE }), {
108 expect(await result.current.onChange()).toEqual(Autopay.ENABLE);
111 it('onChange should return DISABLE if toggled off', async () => {
112 const { result } = renderHook(() => useRenewToggle({ initialRenewState: Autopay.ENABLE }), {
115 const { rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
117 const onChangePromise = result.current.onChange();
118 rerender(<RenewToggle {...result.current} />);
120 fireEvent.click(getByTestId('action-disable-autopay'));
122 expect(await onChangePromise).toEqual(Autopay.DISABLE);
125 it('onChange should return null if the checkbox was not toggled off', async () => {
126 const { result } = renderHook(() => useRenewToggle({ initialRenewState: Autopay.ENABLE }), {
129 const { rerender, getByTestId } = render(<RenewToggle {...result.current} />, { wrapper: Wrapper });
131 const onChangePromise = result.current.onChange();
132 rerender(<RenewToggle {...result.current} />);
134 fireEvent.click(getByTestId('action-keep-autopay'));
136 expect(await onChangePromise).toEqual(null);