|
| 1 | +/* |
| 2 | + * This file is part of the Symfony package. |
| 3 | + * |
| 4 | + * (c) Fabien Potencier <[email protected]> |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +'use strict'; |
| 11 | + |
| 12 | +import { clearDOM } from '@symfony/stimulus-testing'; |
| 13 | +import { startStimulus } from '../tools'; |
| 14 | +import { createEvent, fireEvent, getByLabelText, getByText, waitFor } from '@testing-library/dom'; |
| 15 | +import userEvent from '@testing-library/user-event'; |
| 16 | +import fetchMock from 'fetch-mock-jest'; |
| 17 | + |
| 18 | +describe('LiveController parent -> child component tests', () => { |
| 19 | + const parentTemplate = (data) => { |
| 20 | + return ` |
| 21 | + <div |
| 22 | + data-controller="live" |
| 23 | + data-live-url-value="http://localhost/_components/parent" |
| 24 | + > |
| 25 | + <span>Title: ${data.title}</span> |
| 26 | + <span>Description in Parent: ${data.description}</span> |
| 27 | +
|
| 28 | + <button |
| 29 | + data-action="live#$render" |
| 30 | + >Parent Re-render</button> |
| 31 | +
|
| 32 | + ${childTemplate({ description: data.description })} |
| 33 | + </div> |
| 34 | + ` |
| 35 | + } |
| 36 | + |
| 37 | + const childTemplate = (data) => ` |
| 38 | + <div |
| 39 | + data-controller="live" |
| 40 | + data-live-url-value="http://localhost/_components/my_component" |
| 41 | + > |
| 42 | + <!-- form field not mapped with data-model --> |
| 43 | + <label> |
| 44 | + Description: |
| 45 | + <input |
| 46 | + value="${data.description}" |
| 47 | + data-model="description" |
| 48 | + data-action="live#update" |
| 49 | + > |
| 50 | + </label> |
| 51 | +
|
| 52 | + <div>Description in child: ${data.description}</div> |
| 53 | +
|
| 54 | + <button |
| 55 | + data-action="live#$render" |
| 56 | + >Child Re-render</button> |
| 57 | + </div> |
| 58 | + `; |
| 59 | + |
| 60 | + const startStimulusForParentChild = async (html, data, childData) => { |
| 61 | + const testData = await startStimulus( |
| 62 | + parentTemplate(data), |
| 63 | + data |
| 64 | + ); |
| 65 | + |
| 66 | + // setup the values on the child element |
| 67 | + testData.element.querySelector('[data-controller="live"]').dataset.liveDataValue = JSON.stringify(childData); |
| 68 | + |
| 69 | + return testData; |
| 70 | + } |
| 71 | + |
| 72 | + afterEach(() => { |
| 73 | + clearDOM(); |
| 74 | + fetchMock.reset(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('renders parent component without affecting child component', async () => { |
| 78 | + const data = { title: 'Parent component', description: 'i love components' }; |
| 79 | + const { element } = await startStimulusForParentChild( |
| 80 | + parentTemplate(data), |
| 81 | + data, |
| 82 | + { description: data.description } |
| 83 | + ); |
| 84 | + |
| 85 | + // on child re-render, use a new description |
| 86 | + fetchMock.get('http://localhost/_components/my_component?description=i+love+components', { |
| 87 | + html: childTemplate({ description: 'i love popcorn' }), |
| 88 | + data: { description: 'i love popcorn' } |
| 89 | + }); |
| 90 | + |
| 91 | + // reload the child template |
| 92 | + getByText(element, 'Child Re-render').click(); |
| 93 | + await waitFor(() => expect(element).toHaveTextContent('Description in child: i love popcorn')); |
| 94 | + |
| 95 | + // on parent re-render, render the child template differently |
| 96 | + fetchMock.get('http://localhost/_components/parent?title=Parent+component&description=i+love+components', { |
| 97 | + html: parentTemplate({ title: 'Changed parent', description: 'changed child description' }), |
| 98 | + data: { title: 'Changed parent', description: 'changed child description' } |
| 99 | + }); |
| 100 | + getByText(element, 'Parent Re-render').click(); |
| 101 | + await waitFor(() => expect(element).toHaveTextContent('Title: Changed parent')); |
| 102 | + |
| 103 | + // the child component should *not* have updated |
| 104 | + expect(element).toHaveTextContent('Description in child: i love popcorn'); |
| 105 | + }); |
| 106 | +}); |
0 commit comments