Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #854

Merged
merged 3 commits into from
Nov 2, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/nodeElement/folderElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ class FolderElement extends NodeElement {
private triggerEvent: TriggerEvent;

constructor({
$treeElement,
closedIconElement,
getScrollLeft,
node,
openedIconElement,
tabIndex,
treeElement,
triggerEvent,
}: FolderElementParams) {
super({
$treeElement,
getScrollLeft,
node,
tabIndex,
treeElement,
});

this.closedIconElement = closedIconElement;
Expand Down
18 changes: 6 additions & 12 deletions src/nodeElement/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ import BorderDropHint from "./borderDropHint";
import GhostDropHint from "./ghostDropHint";

export interface NodeElementParams {
$treeElement: JQuery;
getScrollLeft: GetScrollLeft;
node: Node;
tabIndex?: number;
treeElement: HTMLElement;
}

class NodeElement {
private $treeElement: JQuery;
private getScrollLeft: GetScrollLeft;
private tabIndex?: number;
private treeElement: HTMLElement;
public element: HTMLElement;
public node: Node;

constructor({
$treeElement,
getScrollLeft,
node,
tabIndex,
treeElement,
}: NodeElementParams) {
this.getScrollLeft = getScrollLeft;
this.tabIndex = tabIndex;
this.$treeElement = $treeElement;
this.treeElement = treeElement;

this.init(node);
}
Expand Down Expand Up @@ -68,16 +68,10 @@ class NodeElement {
this.node = node;

if (!node.element) {
const element = this.$treeElement.get(0);

if (element) {
node.element = element;
}
node.element = this.treeElement;
}

if (node.element) {
this.element = node.element;
}
this.element = node.element;
}

public select(mustSetFocus: boolean): void {
Expand Down
22 changes: 22 additions & 0 deletions src/test/nodeElement/borderDropHint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import BorderDropHint from "../../nodeElement/borderDropHint";

it("creates an element", () => {
const element = document.createElement("div");

const jqTreeElement = document.createElement("div");
jqTreeElement.classList.add("jqtree-element");
element.append(jqTreeElement);

new BorderDropHint(element, 0);

expect(jqTreeElement.children.length).toBe(1);
expect(jqTreeElement.children[0]).toHaveClass("jqtree-border");
});

it("doesn't create an element if the node doesn't have a jqtree-element child", () => {
const element = document.createElement("div");

new BorderDropHint(element, 0);

expect(element.children).toBeEmpty();
});
29 changes: 29 additions & 0 deletions src/test/nodeElement/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { Node } from "../../node";
import NodeElement from "../../nodeElement";

it("sets the element to the element of the node", () => {
const treeElement = document.createElement("div");
document.body.append(treeElement);

const element = document.createElement("div");
document.body.append(element);

const node = new Node();
node.element = element;

const getScrollLeft = () => 0;

const nodeElement = new NodeElement({ getScrollLeft, node, treeElement });
expect(nodeElement.element).toEqual(element);
});

it("sets the element to the tree element when the node doesn't have an element", () => {
const treeElement = document.createElement("div");
document.body.append(treeElement);

const node = new Node();
const getScrollLeft = () => 0;

const nodeElement = new NodeElement({ getScrollLeft, node, treeElement });
expect(nodeElement.element).toEqual(treeElement);
});
8 changes: 4 additions & 4 deletions src/tree.jquery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,16 +250,16 @@ export class JqTreeWidget extends SimpleWidget<JQTreeOptions> {
);
const openedIconElement = this.renderer.openedIconElement;
const tabIndex = this.options.tabIndex;
const $treeElement = this.element;
const treeElement = this.element.get(0) as HTMLElement;
const triggerEvent = this.triggerEvent.bind(this);

return new FolderElement({
$treeElement,
closedIconElement,
getScrollLeft,
node,
openedIconElement,
tabIndex,
treeElement,
triggerEvent,
});
}
Expand All @@ -269,13 +269,13 @@ export class JqTreeWidget extends SimpleWidget<JQTreeOptions> {
this.scrollHandler,
);
const tabIndex = this.options.tabIndex;
const $treeElement = this.element;
const treeElement = this.element.get(0) as HTMLElement;

return new NodeElement({
$treeElement,
getScrollLeft,
node,
tabIndex,
treeElement,
});
}

Expand Down