Skip to content

Commit

Permalink
Add tests (#854)
Browse files Browse the repository at this point in the history
* Add test for NodeElement

* Change parameter to HtmlElement

* Add test
  • Loading branch information
mbraak authored Nov 2, 2024
1 parent fd7fb9c commit c28bc55
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
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

0 comments on commit c28bc55

Please sign in to comment.