github.com/freiheit-com/kuberpult@v1.24.2-0.20240328135542-315d5630abe6/services/frontend-service/src/ui/components/dropdown/checkbox.test.tsx (about)

     1  /*This file is part of kuberpult.
     2  
     3  Kuberpult is free software: you can redistribute it and/or modify
     4  it under the terms of the Expat(MIT) License as published by
     5  the Free Software Foundation.
     6  
     7  Kuberpult is distributed in the hope that it will be useful,
     8  but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    10  MIT License for more details.
    11  
    12  You should have received a copy of the MIT License
    13  along with kuberpult. If not, see <https://directory.fsf.org/wiki/License:Expat>.
    14  
    15  Copyright 2023 freiheit.com*/
    16  
    17  import { act, render } from '@testing-library/react';
    18  import { Checkbox, CheckboxProps } from './checkbox';
    19  import { documentQuerySelectorSafe } from '../../../setupTests';
    20  
    21  const getNode = (props: CheckboxProps): JSX.Element => <Checkbox {...props} />;
    22  
    23  const getWrapper = (input: CheckboxProps) => render(getNode(input));
    24  
    25  describe('Checkbox', () => {
    26      interface dataT {
    27          name: string;
    28          input: CheckboxProps;
    29          expectedText: string;
    30      }
    31      const mySubmitSpy = jest.fn();
    32  
    33      const data: dataT[] = [
    34          {
    35              name: 'Test onClick',
    36              input: { id: 'id1', enabled: true, label: 'alpha label', onClick: mySubmitSpy },
    37              expectedText: 'alpha label',
    38          },
    39      ];
    40  
    41      describe.each(data)(`Renders a navigation item with selected`, (testcase) => {
    42          it(testcase.name, () => {
    43              // given
    44              mySubmitSpy.mockReset();
    45              // when
    46              getWrapper(testcase.input);
    47              // then
    48              const result = documentQuerySelectorSafe('.id-' + testcase.input.id);
    49              expect(mySubmitSpy).toHaveBeenCalledTimes(0);
    50              act(() => {
    51                  result.click();
    52              });
    53              expect(mySubmitSpy).toHaveBeenCalledTimes(1);
    54              expect(mySubmitSpy).toHaveBeenCalledWith(testcase.input.id);
    55  
    56              expect(document.querySelectorAll('.checkbox-wrapper').length).toEqual(1);
    57              expect(document.querySelectorAll('.checkbox-wrapper')[0]).toHaveTextContent(testcase.expectedText);
    58          });
    59      });
    60  });