diff --git a/src/components/dialog/Dialog.test.tsx b/src/components/dialog/Dialog.test.tsx index b277c67ff..d1e727e98 100644 --- a/src/components/dialog/Dialog.test.tsx +++ b/src/components/dialog/Dialog.test.tsx @@ -1,9 +1,13 @@ import Dialog, { DialogProps, HeaderStyle } from 'components/dialog/Dialog'; import * as React from 'react'; -import { render } from 'test/utils'; +import { render, fireEvent } from 'test/utils'; const baseProps: DialogProps = { title: 'My Dialog', +}; + +const fullProps: DialogProps = { + ...baseProps, subtitle: 'Subtitlf', headerIcon: 'fe-icon', headerClass: 'header-class', @@ -14,6 +18,17 @@ const baseProps: DialogProps = { tertiary: { name: 'Other', onClick: vi.fn() }, }, gutter:
The Gutter
, + noPadding: true, + new: true, + className: 'my-class', +}; + +const fullPropsWithTabs: DialogProps = { + ...fullProps, + tabs: [ + { name: 'Tab 1', body:
Tab 1 Content
, checked: true }, + { name: 'Tab 2', body:
Tab 2 Content
, hasErrors: true }, + ], }; describe(Dialog.name, () => { @@ -21,4 +36,142 @@ describe(Dialog.name, () => { const { baseElement } = render(); expect(baseElement).toMatchSnapshot(); }); + + it('should render with full props', () => { + const { baseElement } = render(); + expect(baseElement).toMatchSnapshot(); + }); + + it('should focus on main tab on primary button click', () => { + vi.useFakeTimers(); + // remove the error tab to allow the main tab focus on primary button click + const tabsProps = [...fullPropsWithTabs.tabs]; + tabsProps.pop(); + + const { baseElement, getByText } = render( + , + ); + expect(baseElement).toMatchSnapshot(); + + // switch to first tab + fireEvent.click(getByText('Tab 1')); + expect(baseElement).toMatchSnapshot(); + + fireEvent.click(getByText('Ok')); + vi.runAllTimers(); + + expect(baseElement).toMatchSnapshot(); + }); + + it('should switch to main tab on title click', () => { + vi.useFakeTimers(); + const { baseElement, getByText } = render( + , + ); + expect(baseElement).toMatchSnapshot(); + + // switch to Tab 2 + fireEvent.click(getByText('Tab 2')); + expect(baseElement).toMatchSnapshot(); + + // switch to main tab + fireEvent.click(getByText('My Dialog')); + vi.runAllTimers(); + + expect(baseElement).toMatchSnapshot(); + }); + + it('should execute passed function on primary button click', () => { + const { getByText } = render(); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + fireEvent.click(getByText('Ok')); + expect(fullProps.buttons.primary.onClick).toHaveBeenCalled(); + }); + + it('should execute passed function shift+enter event', () => { + const { baseElement } = render(); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + fireEvent.keyDown(baseElement, { key: 'Enter', shiftKey: true }); + expect(fullProps.buttons.primary.onClick).toHaveBeenCalled(); + }); + + it('should not execute passed function on primary button shift+enter event if button is not provided', () => { + const buttonsProps = { ...fullProps.buttons }; + delete buttonsProps.primary; + + const { baseElement } = render( + , + ); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + fireEvent.keyDown(baseElement, { key: 'Enter', shiftKey: true }); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + }); + + it('shoul not execute passed function if shift+enter event is not triggered', () => { + const { baseElement } = render(); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + fireEvent.keyDown(baseElement, { key: 'Enter' }); + expect(fullProps.buttons.primary.onClick).not.toHaveBeenCalled(); + }); + + it('should render with undefined tabs', () => { + const { baseElement } = render(); + expect(baseElement).toMatchSnapshot(); + }); + + it('should not switch tabs if there are undefined tabs', () => { + vi.useFakeTimers(); + const { baseElement, getByText } = render( + , + ); + expect(baseElement).toMatchSnapshot(); + + fireEvent.keyDown(baseElement, { key: 'Enter', shiftKey: true }); + + vi.runAllTimers(); + expect(baseElement).toMatchSnapshot(); + }); + + it('should render tabs with errors', () => { + const { baseElement, queryByText, getByText } = render( + , + ); + expect(baseElement).toMatchSnapshot(); + + expect(queryByText('Tab 2 Content')).not.toBeInTheDocument(); + // switch to Tab 2 + fireEvent.click(getByText('Tab 2')); + expect(baseElement).toMatchSnapshot(); + expect(getByText('Tab 2 Content')).toBeInTheDocument(); + }); + + it('should switch to errored tab on primary button click', () => { + vi.useFakeTimers(); + + const { queryByText, getByText } = render( + , + ); + + expect(queryByText('Tab 2 Content')).not.toBeInTheDocument(); + // click on primary button + fireEvent.click(getByText('Ok')); + + vi.runAllTimers(); + + expect(getByText('Tab 2 Content')).toBeInTheDocument(); + }); + + it('should switch tab on instace method call', () => { + const dialog = ; + const { baseElement } = render(dialog); + + expect(baseElement).toMatchSnapshot(); + + dialog; + }); + + it('should render without any props', () => { + const { baseElement } = render(); + expect(baseElement).toMatchSnapshot(); + }); }); diff --git a/src/components/dialog/Dialog.tsx b/src/components/dialog/Dialog.tsx index 09da53530..df891e77a 100644 --- a/src/components/dialog/Dialog.tsx +++ b/src/components/dialog/Dialog.tsx @@ -106,8 +106,6 @@ export default class Dialog extends React.Component { (event.target as any).blur(); this.handlePrimaryButton(this.props.buttons.primary.onClick); (event.target as any).focus(); - } else { - console.log('No primary button!'); } } } @@ -256,7 +254,7 @@ export default class Dialog extends React.Component { initialTab={String(this.state.activeTab + 1)} tabs={[ '0', - ...(this.props.tabs || []).map((tab: Tab, index: number) => + ...this.props.tabs.map((tab: Tab, index: number) => String(index + 1), ), ]} diff --git a/src/components/dialog/__snapshots__/Dialog.test.tsx.snap b/src/components/dialog/__snapshots__/Dialog.test.tsx.snap index f65a2ee60..291f20f7c 100644 --- a/src/components/dialog/__snapshots__/Dialog.test.tsx.snap +++ b/src/components/dialog/__snapshots__/Dialog.test.tsx.snap @@ -1,10 +1,2377 @@ // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html +exports[`Dialog > should focus on main tab on primary button click 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should focus on main tab on primary button click 2`] = ` + +
+
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+
+ Tab 1 Content +
+
+ +
+
+ +`; + +exports[`Dialog > should focus on main tab on primary button click 3`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should not switch tabs if there are undefined tabs 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should not switch tabs if there are undefined tabs 2`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+ +
+
+ +`; + exports[`Dialog > should render 1`] = `
+
+
+
+ My Dialog +
+
+
+
+
+ +
+ +`; + +exports[`Dialog > should render tabs with errors 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should render tabs with errors 2`] = ` + +
+
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+
+ Tab 2 Content +
+
+ +
+
+ +`; + +exports[`Dialog > should render with full props 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should render with undefined tabs 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should render without any props 1`] = ` + +
+
+
+
+
+
+
+
+
+ +
+ +`; + +exports[`Dialog > should switch tab on instace method call 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should switch to main tab on title click 1`] = ` + +
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+ +
+
+ +`; + +exports[`Dialog > should switch to main tab on title click 2`] = ` + +
+
+
+
+ +
+
+ My Dialog + + New + ! + +
+
+ Subtitlf +
+
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+
+
+
+
+ Tab 2 Content +
+
+ +
+
+ +`; + +exports[`Dialog > should switch to main tab on title click 3`] = ` + +
+
should render 1`] = ` class="_title_af1b5d u font secondary title-sm black" > My Dialog + + New + ! +
should render 1`] = ` Subtitlf
+
+
+
+
    + +
  • + +
    + General +
    + + +
  • +
  • + +
    + Tab 1 +
    +
    + + +
  • +
  • + +
    + Tab 2 +
    + + +
  • + +
+
+
+
+ + +
+
+
+