WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/util/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function insertNodeAt(fatherNode, node, position) {
const refNode =
position === 0
? fatherNode.children[0]
: fatherNode.children[position - 1].nextSibling;
: fatherNode.children[position - 1]?.nextSibling;
fatherNode.insertBefore(node, refNode);
}

Expand Down
27 changes: 25 additions & 2 deletions tests/unit/util/helper.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { camelize, console } from "@/util/helper";
import { camelize, console, insertNodeAt } from "@/util/helper";

describe("camelize", () => {
test.each([
Expand Down Expand Up @@ -30,4 +30,27 @@ describe("console", () => {
expect(typeof actual).toEqual("function");
}
)
});
});

describe('insertNodeAt', () => {
const node = document.createElement('div');
const nextSibling = document.createElement('div');
const child = { nextSibling };
const mockInsertBefore = jest.fn();
const fatherNode = {
insertBefore: mockInsertBefore,
children: [child]
}
test('Inserts node at 0 position', () => {
insertNodeAt(fatherNode, node, 0);
expect(mockInsertBefore).toHaveBeenCalledWith(node, child);
})
test('Inserts node at 1 position', () => {
insertNodeAt(fatherNode, node, 1);
expect(mockInsertBefore).toHaveBeenCalledWith(node, nextSibling);
})
test("Inserts node at the end of node's child nodes", () => {
insertNodeAt(fatherNode, node, 2);
expect(mockInsertBefore).toHaveBeenCalledWith(node, undefined);
})
});