Skip to content

Commit

Permalink
Does nothing when child in removeChild is not found
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas-marcucci committed Jan 3, 2025
1 parent c89427f commit a5b1f4a
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
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
3 changes: 3 additions & 0 deletions packages/core/src/receiver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ export function createRemoteReceiver(): RemoteReceiver {
const {children} = attached;

const [removed] = children.splice(index, 1);
if (!removed) {
return;
}
attached.version += 1;

detach(removed);
Expand Down
52 changes: 51 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', () => {
Expand All @@ -14,4 +19,49 @@ 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: null,
},
]);

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

const child2 = {
id: 'child2',
children: [],
kind: KIND_COMPONENT,
props: {},
type: null,
};

receiver.receive(ACTION_INSERT_CHILD, undefined, 0, child2, '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, child2, 'child2');

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

it('handles a missing child', () => {
const receiver = createRemoteReceiver();
receiver.receive(ACTION_MOUNT, []);
receiver.receive(ACTION_REMOVE_CHILD, undefined, -1);

expect(receiver.attached.root.children).toHaveLength(0);
});
});
});

0 comments on commit a5b1f4a

Please sign in to comment.