Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevents removeChild from removing any nodes when child is not found
Browse files Browse the repository at this point in the history
thomas-marcucci committed Jan 15, 2025
1 parent c89427f commit e9a7bc9
Showing 5 changed files with 73 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/red-waves-flash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@remote-ui/core': patch
---

Fixes an issue when removeChild does not find a child node
1 change: 0 additions & 1 deletion packages/core/src/receiver.ts
Original file line number Diff line number Diff line change
@@ -202,7 +202,6 @@ export function createRemoteReceiver(): RemoteReceiver {
) as RemoteReceiverAttachableRoot;

const {children} = attached;

const [removed] = children.splice(index, 1);
attached.version += 1;

12 changes: 6 additions & 6 deletions packages/core/src/root.ts
Original file line number Diff line number Diff line change
@@ -841,16 +841,16 @@ function removeChild(
) {
const {strict} = rootInternals;

const childIndex = container.children.indexOf(child as any);
if (childIndex === -1) {
return undefined;
}

return perform(container, rootInternals, {
remote: (channel) =>
channel(
ACTION_REMOVE_CHILD,
(container as any).id,
container.children.indexOf(child as any),
),
channel(ACTION_REMOVE_CHILD, (container as any).id, childIndex),
local: () => {
removeNodeFromContainer(child, rootInternals);

const newChildren = [...internals.children];
newChildren.splice(newChildren.indexOf(child), 1);
internals.children = strict ? Object.freeze(newChildren) : newChildren;
48 changes: 47 additions & 1 deletion packages/core/src/tests/receiver.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import {createRemoteReceiver} from '../receiver';
import {ACTION_MOUNT} from '../types';
import {
ACTION_MOUNT,
ACTION_INSERT_CHILD,
ACTION_REMOVE_CHILD,
KIND_COMPONENT,
} from '../types';

describe('createRemoteReceiver()', () => {
describe('state', () => {
@@ -14,4 +19,45 @@ describe('createRemoteReceiver()', () => {
expect(receiver).toHaveProperty('state', 'mounted');
});
});

describe('removeChild', () => {
it('removes a given child node', () => {
const receiver = createRemoteReceiver();
receiver.receive(ACTION_MOUNT, [
{
id: 'child1',
children: [],
kind: KIND_COMPONENT,
props: {},
type: '',
},
]);

expect(receiver.attached.root.children).toHaveLength(1);
expect(receiver.attached.root.children[0].id).toBe('child1');

receiver.receive(
ACTION_INSERT_CHILD,
undefined,
0,
{
id: 'child2',
children: [],
kind: KIND_COMPONENT,
props: {},
type: '',
},
'child2',
);

expect(receiver.attached.root.children).toHaveLength(2);
expect(receiver.attached.root.children[0].id).toBe('child2');
expect(receiver.attached.root.children[1].id).toBe('child1');

receiver.receive(ACTION_REMOVE_CHILD, undefined, 0);

expect(receiver.attached.root.children).toHaveLength(1);
expect(receiver.attached.root.children[0].id).toBe('child1');
});
});
});
15 changes: 15 additions & 0 deletions packages/core/src/tests/root.test.ts
Original file line number Diff line number Diff line change
@@ -125,6 +125,21 @@ describe('root', () => {
});
});

describe('removeChild', () => {
it('does not call channel when child is not found', () => {
const channelMock = jest.fn();
const root = createRemoteRoot(channelMock);
root.mount();
// clear channel call from mount
channelMock.mockClear();

const card = root.createComponent('Card');
root.removeChild(card);

expect(channelMock).not.toHaveBeenCalled();
});
});

describe('hot-swapping', () => {
it('hot-swaps function props', () => {
const funcOne = jest.fn();

0 comments on commit e9a7bc9

Please sign in to comment.