phylogram-layout.test.ts 1.77 KB
Newer Older
Remi  PLANEL's avatar
Remi PLANEL committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import Phylogram from "../src/scripts/layout/phylogram";
import { RawPhyloTreeNode } from "../src/scripts/types";

describe("Test layout", () => {
  const data: RawPhyloTreeNode = {
    "name": "root",
    branchLength: 0,
    nodes: {
      r: 2,
      fill: "green"
    },
    "children": [
      {
        "name": "child0",
        branchLength: 0.5,
        nodes: {
          fill: "blue"
        }
      },
      {
        "name": "child1",
        branchLength: 0.6,
        nodes: {
          r: 8,
          fill: "red"
        },
        "children": [
          {
            "name": "child10",
            branchLength: 0.4
          },
          {
            "name": "child11",
            branchLength: 0.1
          }
        ]
      }
    ]
  };
  const height = 900;
  const width = 1000;
  const phylogramLayout = Phylogram().size([height, width]);
  const pointData = phylogramLayout(data);
  const child0 = (pointData.children) ? pointData.children[0] : { y: false };
  const child10 = pointData.leaves().reduce((acc, curr) => {
    return (curr.data.name === "child10") ? curr : acc;
  }, pointData)
  test("Root node middle height", () => {
Remi  PLANEL's avatar
Remi PLANEL committed
49
    expect(pointData.x).toBe(375);
Remi  PLANEL's avatar
Remi PLANEL committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  })

  // Test X
  if (child0) {
    test("Child 0", () => {
      expect(child0.y).toBe(500);
    });
  }

  // Test y
  test("child10", () => {
    expect(child10.y).toBe(width);
  })

  //test nodes object
  test("root", () => {
    const nodes = pointData.data.nodes;
    if (nodes) {
      expect(nodes.r).toBe(2);
    }
  })
Remi  PLANEL's avatar
Remi PLANEL committed
71
72
73
74
75
76
77
78
79
80
81
82

  // Change how define size
  test("nodeSize", () => {
    const xStep = 50;
    const newData = phylogramLayout.nodeSize([xStep, 100])(data);
    let yExpected = 0;
    newData.leaves().forEach(leaf => {
      expect(leaf.x).toBe(yExpected);
      yExpected += xStep;
    });
  });

Remi  PLANEL's avatar
Remi PLANEL committed
83
})