diff --git a/src/Collection/Resources/assets/src/controller.ts b/src/Collection/Resources/assets/src/controller.ts index b5c3ea8d981..c83e9d4eeb5 100644 --- a/src/Collection/Resources/assets/src/controller.ts +++ b/src/Collection/Resources/assets/src/controller.ts @@ -44,7 +44,6 @@ export default class extends Controller { createButton(collectionEl: HTMLElement, buttonType: ButtonType): HTMLElement | null { const attributeName = `${ButtonType[buttonType].toLowerCase()}Button`; const button = collectionEl.dataset[attributeName] ?? (this.element as HTMLElement).dataset[attributeName]; - console.log(button); // Button explicitly disabled through data attribute if ('' === button) return null; diff --git a/src/Collection/Resources/assets/test/controller.test.ts b/src/Collection/Resources/assets/test/controller.test.ts index da9f6ae16ae..67d5a1d139f 100644 --- a/src/Collection/Resources/assets/test/controller.test.ts +++ b/src/Collection/Resources/assets/test/controller.test.ts @@ -10,7 +10,7 @@ 'use strict'; import { Application } from '@hotwired/stimulus'; -import { getByTestId } from '@testing-library/dom'; +import {fireEvent, getByTestId, getByText, waitFor} from '@testing-library/dom'; import { clearDOM, mountDOM } from '@symfony/stimulus-testing'; import CollectionController from '../src/controller'; @@ -19,22 +19,135 @@ const startStimulus = () => { application.register('symfony--ux-collection--collection', CollectionController); }; +const emptyCollection = '
'; + +const simpleCollectionWithoutButtons = '' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '' + +const simpleCollection = '' + + ' ' + + '' + + ' ' + + '' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '' + +const nestedCollection = '' + + ' ' + + '' + + ' ' + + '' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '
' + + '
' + + '
' + + ' ' + + '
' + + '' + /* eslint-disable no-undef */ describe('CollectionController', () => { - let container: any; - - beforeEach(() => { - container = mountDOM('
'); - }); + startStimulus(); afterEach(() => { clearDOM(); }); it('connects', async () => { - startStimulus(); + const container = mountDOM(emptyCollection); // smoke test expect(getByTestId(container, 'collection')).toHaveAttribute('data-controller', 'symfony--ux-collection--collection'); }); + + it('should create a add button when no button given', async () => { + const container = mountDOM(simpleCollectionWithoutButtons); + + const element = await waitFor(() => getByText(container, 'Add')); + + expect(element).toBeInTheDocument(); + }); + + it('should create a add button when button is given', async () => { + const container = mountDOM(simpleCollection); + + const element = await waitFor(() => getByText(container, 'Add button')); + + expect(element).toBeInTheDocument(); + }); + + it('should create new collection on click on add button', async () => { + const container = mountDOM(simpleCollection); + + await waitFor(() => getByText(container, 'Add button')); + + fireEvent( + getByText(container, 'Add button'), + new MouseEvent('click', { + bubbles: true, + cancelable: true, + }), + ) + + expect(container.querySelectorAll('.team-entry').length).toBe(1); + }); + + it('should remove collection on click on delete button', async () => { + const container = mountDOM(simpleCollection); + + await waitFor(() => getByText(container, 'Add button')); + + fireEvent( + getByText(container, 'Add button'), + new MouseEvent('click', { + bubbles: true, + cancelable: true, + }), + ) + + expect(container.querySelectorAll('.team-entry').length).toBe(1); + + await waitFor(() => getByText(container, 'Delete button')); + + fireEvent( + getByText(container, 'Delete button'), + new MouseEvent('click', { + bubbles: true, + cancelable: true, + }), + ) + + expect(container.querySelectorAll('.team-entry').length).toBe(0); + }); });