123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- // Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
- import { fireEvent, waitFor } from '@testing-library/vue'
- import { renderComponent } from '#tests/support/components/index.ts'
- import CommonInlineEdit, {
- type Props,
- } from '#desktop/components/CommonInlineEdit/CommonInlineEdit.vue'
- const renderInlineEdit = (props: Partial<Props> = {}) => {
- return renderComponent(CommonInlineEdit, {
- props: {
- name: 'inlineEditTest',
- value: 'test value',
- label: 'Inline Edit Label',
- submitLabel: 'Submit',
- cancelLabel: 'Cancel',
- ...props,
- },
- form: true,
- router: true,
- })
- }
- describe('CommonInlineEdit', () => {
- it('shows by default non editable node', () => {
- const wrapper = renderInlineEdit()
- expect(wrapper.getByText('test value')).toBeInTheDocument()
- expect(wrapper.queryByDisplayValue('test value')).not.toBeInTheDocument()
- })
- it('supports placeholder on edit input', async () => {
- const wrapper = renderInlineEdit({ placeholder: 'test placeholder' })
- await wrapper.events.click(wrapper.getByRole('button'))
- expect(
- await wrapper.findByPlaceholderText('test placeholder'),
- ).toBeInTheDocument()
- })
- it('submits edit on button click and enter', async () => {
- const submitEditCallbackSpy = vi.fn()
- const wrapper = renderInlineEdit({
- onSubmitEdit: (newValue: string) => submitEditCallbackSpy(newValue),
- })
- await wrapper.events.click(wrapper.getByRole('button'))
- await wrapper.events.type(wrapper.getByRole('textbox'), ' update')
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
- expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update')
- expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
- })
- it('submits edit on enter key', async () => {
- const submitEditCallbackSpy = vi.fn()
- const wrapper = renderInlineEdit({
- onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
- })
- await wrapper.events.click(wrapper.getByRole('button'))
- await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await wrapper.events.keyboard('{enter}')
- expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
- })
- it('submits on background click', async () => {
- const submitEditCallbackSpy = vi.fn()
- const wrapper = renderInlineEdit({
- onSubmitEdit: (value: string) => submitEditCallbackSpy(value),
- })
- await wrapper.events.click(wrapper.getByRole('button'))
- await wrapper.events.type(wrapper.getByRole('textbox'), ' update 2')
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await fireEvent.click(document.body)
- expect(submitEditCallbackSpy).toHaveBeenCalledWith('test value update 2')
- })
- it('do not stop edit mode when submit promise failed', async () => {
- const wrapper = renderInlineEdit({
- onSubmitEdit: (): Promise<void> => {
- return new Promise((resolve, reject) => {
- reject()
- })
- },
- })
- await wrapper.events.click(wrapper.getByRole('button'))
- await wrapper.events.type(wrapper.getByRole('textbox'), ' update')
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await wrapper.events.click(wrapper.getByRole('button', { name: 'Submit' }))
- expect(wrapper.getByRole('textbox')).toBeInTheDocument()
- })
- it('focuses field on edit', async () => {
- const wrapper = renderInlineEdit()
- await wrapper.events.click(wrapper.getByRole('button'))
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- expect(wrapper.getByRole('textbox')).toHaveFocus()
- })
- it('cancels on button click', async () => {
- const wrapper = renderInlineEdit()
- await wrapper.events.click(wrapper.getByRole('button'))
- await wrapper.events.click(wrapper.getByRole('button', { name: 'Cancel' }))
- expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
- expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
- })
- it('cancels on escape key', async () => {
- const wrapper = renderInlineEdit()
- await wrapper.events.click(wrapper.getByRole('button'))
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await wrapper.events.keyboard('{esc}')
- expect(wrapper.emitted()['cancel-edit']).toBeTruthy()
- })
- it('disables submit if input is incorrect and required is true', async () => {
- const wrapper = renderInlineEdit({ required: true })
- await wrapper.events.click(wrapper.getByRole('button'))
- await waitFor(() =>
- expect(wrapper.getByRole('textbox')).toBeInTheDocument(),
- )
- await wrapper.events.clear(wrapper.getByRole('textbox'))
- expect(wrapper.emitted()['submit-edit']).toBeFalsy()
- expect(wrapper.getByRole('button', { name: 'Submit' })).toBeDisabled()
- expect(wrapper.getByRole('textbox')).toBeInTheDocument()
- })
- it('supports adding attributes on label', () => {
- const wrapper = renderInlineEdit({
- labelAttrs: {
- role: 'heading',
- 'aria-level': '1',
- },
- })
- expect(wrapper.getByRole('heading', { level: 1 })).toBeInTheDocument()
- })
- it('allows inline edit to be take up full width if set to block', () => {
- const wrapper = renderInlineEdit({
- block: true,
- })
- expect(wrapper.getByRole('button')).toHaveClass('w-full')
- })
- it('disables input if disabled prop is true', async () => {
- const wrapper = renderInlineEdit({ disabled: true })
- expect(wrapper.getByText('test value')).toBeInTheDocument()
- await wrapper.events.click(wrapper.getByText('test value'))
- expect(wrapper.queryByRole('textbox')).not.toBeInTheDocument()
- })
- it('detects links if set to true and renders it as link only in label', async () => {
- const wrapper = renderInlineEdit({
- detectLinks: true,
- value: 'https://zammad.com/en',
- })
- expect(
- wrapper.getByRole('link', { name: 'https://zammad.com/en' }),
- ).toBeInTheDocument()
- })
- it('displays initial edit value if editing got activated', async () => {
- const wrapper = renderInlineEdit({
- initialEditValue: 'initial Value',
- value: 'default Value',
- })
- await wrapper.events.click(wrapper.getByText('default Value'))
- expect(wrapper.getByRole('textbox')).toHaveValue('initial Value')
- })
- it('support loading', async () => {
- const wrapper = renderInlineEdit({
- initialEditValue: 'initial Value',
- value: 'default Value',
- loading: true,
- })
- await wrapper.events.click(wrapper.getByText('default Value'))
- expect(wrapper.getByRole('textbox')).toBeDisabled()
- })
- it('supports adding alternative background color', async () => {
- const wrapper = renderInlineEdit({
- alternativeBackground: true,
- })
- await wrapper.events.click(wrapper.getByText('test value'))
- expect(wrapper.html()).toContain(
- 'before:bg-neutral-50 before:dark:bg-gray-500',
- )
- await wrapper.rerender({
- alternativeBackground: false,
- })
- expect(wrapper.html()).toContain(
- 'before:bg-blue-200 before:dark:bg-gray-700',
- )
- expect(wrapper.html()).not.toContain(
- 'before:bg-neutral-50 before:dark:bg-gray-500',
- )
- })
- })
|