Skip to content

Commit be73dce

Browse files
authored
Merge pull request #2213 from pie-framework/fix/PIE-436
fix: made sure extra spacing is added only when needed [PIE-436]
2 parents f0959bd + 1eb4b4c commit be73dce

2 files changed

Lines changed: 202 additions & 4 deletions

File tree

packages/mask-markup/src/__tests__/mask.test.js

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,198 @@ describe('Mask', () => {
184184
expect(container.querySelector('tr')).toBeInTheDocument();
185185
});
186186
});
187+
188+
describe('spacer rendering for DnD components', () => {
189+
it('adds spacers before and after DnD blank components', () => {
190+
const mockRenderChildren = jest.fn((n) => {
191+
if (n.data?.dataset?.component === 'blank') {
192+
return <span data-testid="blank-component">Blank</span>;
193+
}
194+
return null;
195+
});
196+
197+
const { container } = render(
198+
<Mask
199+
{...defaultProps}
200+
renderChildren={mockRenderChildren}
201+
layout={{
202+
nodes: [
203+
{
204+
type: 'div',
205+
data: {
206+
dataset: { component: 'blank' },
207+
attributes: {},
208+
},
209+
nodes: [],
210+
},
211+
],
212+
}}
213+
/>,
214+
);
215+
216+
// Check that renderChildren was called and spacers are present
217+
// Count all children in the container - should be: spacer + blank + spacer = 3 elements
218+
const maskContainer = container.firstChild;
219+
expect(maskContainer.childNodes.length).toBe(3);
220+
expect(screen.getByTestId('blank-component')).toBeInTheDocument();
221+
});
222+
223+
it('does not add spacers for non-DnD components', () => {
224+
const mockRenderChildren = jest.fn((n) => {
225+
return <span data-testid="regular-component">Regular</span>;
226+
});
227+
228+
const { container } = render(
229+
<Mask
230+
{...defaultProps}
231+
renderChildren={mockRenderChildren}
232+
layout={{
233+
nodes: [
234+
{
235+
type: 'div',
236+
data: {
237+
attributes: {},
238+
},
239+
nodes: [],
240+
},
241+
],
242+
}}
243+
/>,
244+
);
245+
246+
// Should not have spacers - only the regular component
247+
const maskContainer = container.firstChild;
248+
expect(maskContainer.childNodes.length).toBe(1);
249+
expect(screen.getByTestId('regular-component')).toBeInTheDocument();
250+
});
251+
252+
it('adds spacers regardless of parent node type', () => {
253+
const mockRenderChildren = jest.fn((n) => {
254+
if (n.data?.dataset?.component === 'blank') {
255+
return <span data-testid="blank-in-td">Blank in TD</span>;
256+
}
257+
return null;
258+
});
259+
260+
const { container } = render(
261+
<Mask
262+
{...defaultProps}
263+
renderChildren={mockRenderChildren}
264+
elementType="drag-in-the-blank"
265+
layout={{
266+
nodes: [
267+
{
268+
type: 'table',
269+
data: { attributes: {} },
270+
nodes: [
271+
{
272+
type: 'tbody',
273+
data: { attributes: {} },
274+
nodes: [
275+
{
276+
type: 'tr',
277+
data: { attributes: {} },
278+
nodes: [
279+
{
280+
type: 'td',
281+
data: { attributes: {} },
282+
nodes: [
283+
{
284+
type: 'div',
285+
data: {
286+
dataset: { component: 'blank' },
287+
attributes: {},
288+
},
289+
nodes: [],
290+
},
291+
],
292+
},
293+
],
294+
},
295+
],
296+
},
297+
],
298+
},
299+
],
300+
}}
301+
/>,
302+
);
303+
304+
// Should have spacers even inside td element
305+
const td = container.querySelector('td');
306+
expect(td.childNodes.length).toBe(3); // spacer + blank + spacer
307+
expect(screen.getByTestId('blank-in-td')).toBeInTheDocument();
308+
});
309+
310+
it('does not add spacers for text content', () => {
311+
const { container } = render(
312+
<Mask
313+
{...defaultProps}
314+
elementType="drag-in-the-blank"
315+
layout={{
316+
nodes: [
317+
{
318+
object: 'text',
319+
leaves: [
320+
{
321+
text: 'Some text',
322+
},
323+
],
324+
},
325+
],
326+
}}
327+
/>,
328+
);
329+
330+
// Should not have spacers for plain text - just text node
331+
const maskContainer = container.firstChild;
332+
expect(maskContainer.childNodes.length).toBe(1);
333+
expect(maskContainer.childNodes[0].nodeType).toBe(Node.TEXT_NODE);
334+
expect(screen.getByText('Some text')).toBeInTheDocument();
335+
});
336+
337+
it('handles multiple DnD components with correct spacer placement', () => {
338+
const mockRenderChildren = jest.fn((n) => {
339+
if (n.data?.dataset?.component === 'blank') {
340+
return <span data-testid={`blank-${n.data.testId}`}>Blank</span>;
341+
}
342+
return null;
343+
});
344+
345+
const { container } = render(
346+
<Mask
347+
{...defaultProps}
348+
renderChildren={mockRenderChildren}
349+
layout={{
350+
nodes: [
351+
{
352+
type: 'div',
353+
data: {
354+
dataset: { component: 'blank' },
355+
attributes: {},
356+
testId: '1',
357+
},
358+
nodes: [],
359+
},
360+
{
361+
type: 'div',
362+
data: {
363+
dataset: { component: 'blank' },
364+
attributes: {},
365+
testId: '2',
366+
},
367+
nodes: [],
368+
},
369+
],
370+
}}
371+
/>,
372+
);
373+
374+
// Should have 2 spacers per component = 4 spacers + 2 blanks = 6 total children
375+
const maskContainer = container.firstChild;
376+
expect(maskContainer.childNodes.length).toBe(6);
377+
expect(screen.getByTestId('blank-1')).toBeInTheDocument();
378+
expect(screen.getByTestId('blank-2')).toBeInTheDocument();
379+
});
380+
});
187381
});

packages/mask-markup/src/mask.jsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,15 @@ export const renderChildren = (layout, value, onChange, rootRenderChildren, pare
6262
if (rootRenderChildren) {
6363
const c = rootRenderChildren(n, value, onChange);
6464
if (c) {
65+
const isDndComponent = n.data?.dataset?.component === 'blank';
66+
67+
if (isDndComponent) {
68+
children.push(<Spacer key={`spacer-${index}`} />);
69+
}
70+
6571
children.push(c);
66-
if (parentNode?.type !== 'td' && elementType === 'drag-in-the-blank') {
72+
73+
if (isDndComponent) {
6774
children.push(<Spacer key={`spacer-${index}`} />);
6875
}
6976
return;
@@ -91,9 +98,6 @@ export const renderChildren = (layout, value, onChange, rootRenderChildren, pare
9198
}
9299
} else if (content.length > 0) {
93100
children.push(content);
94-
if (parentNode?.type !== 'td' && elementType === 'drag-in-the-blank') {
95-
children.push(<Spacer key={`spacer-${index}`} />);
96-
}
97101
}
98102
} else {
99103
const subNodes = renderChildren(n, value, onChange, rootRenderChildren, n, elementType);

0 commit comments

Comments
 (0)