summaryrefslogtreecommitdiffstats
path: root/public/src/app/rule-engine/target/target.validation.spec.ts
blob: e66235f9c44a7c14af3ad1aeee1bf088543dcb8e (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import { TreeModel } from 'angular-tree-component';
import { fuzzysearch, getBranchRequierds, validation } from './target.util';

export const _nodes = [
  {
    id: 1,
    name: 'North America',
    requiredChildren: ['United States'],
    children: [
      {
        id: 11,
        name: 'United States',
        requiredChildren: ['New York', 'Florida'],
        children: [
          {
            id: 111,
            name: 'New York'
          },
          {
            id: 112,
            name: 'California'
          },
          {
            id: 113,
            name: 'Florida'
          }
        ]
      },
      {
        id: 12,
        name: 'Canada'
      }
    ]
  },
  {
    name: 'South America',
    children: [
      {
        name: 'Argentina',
        children: []
      },
      {
        name: 'Brazil'
      }
    ]
  },
  {
    name: 'Europe',
    children: [
      {
        name: 'England'
      },
      {
        name: 'Germany'
      },
      {
        name: 'France'
      },
      {
        name: 'Italy'
      },
      {
        name: 'Spain'
      }
    ]
  }
];

export const tree = new TreeModel();

describe('treeTest', () => {
  beforeAll(() => {
    tree.setData({ nodes: _nodes, options: null, events: null });
  });

  it('should return node branch requireds toBeGreaterThan 1', () => {
    const selectedNode = tree.getNodeBy(
      node => node.data.name === 'California'
    );
    const result = getBranchRequierds(selectedNode, []);
    const expected = [['New York', 'Florida'], ['United States']];

    expect(result.length).toBeGreaterThan(1);
  });

  it('should return node branch requireds', () => {
    const selectedNode = tree.getNodeBy(
      node => node.data.name === 'California'
    );
    const result = getBranchRequierds(selectedNode, []);
    const expected = [['New York', 'Florida'], ['United States']];
    expect(result).toEqual(expected);
  });

  it('should return empty array - success state', () => {
    const userSelect = ['Florida', 'New York', 'United States'];
    const selectedNode = tree.getNodeBy(node => node.data.name === 'New York');
    const result = validation(selectedNode, userSelect);
    expect(result).toEqual([]);
  });
  it('should return empty array - success state lenght zero', () => {
    const userSelect = ['Florida', 'New York', 'United States'];
    const selectedNode = tree.getNodeBy(node => node.data.name === 'New York');
    const result = validation(selectedNode, userSelect);

    expect(result.length).toEqual(0);
  });

  it('should return validation array - missing required filed', () => {
    const userSelect = ['New York'];
    const selectedNode = tree.getNodeBy(node => node.data.name === 'New York');
    const result = validation(selectedNode, userSelect);
    const expected = ['Florida', 'United States'];

    expect(result).toEqual(expected);
  });

  it('fuzzysearch find match one char', () => {
    const search = fuzzysearch('1', '1');
    expect(search).toBe(true);
  });

  it('fuzzysearch find match string', () => {
    const search = fuzzysearch('liav', 'liavEzar');
    expect(search).toBe(true);
  });

  it('fuzzysearch not find match', () => {
    const search = fuzzysearch('1', '2');
    expect(search).toBe(false);
  });

  it('fuzzysearch not find match', () => {
    const search = fuzzysearch('liavEzar', 'liav');
    expect(search).toBe(false);
  });

  it('fuzzysearch not find match', () => {
    const search = fuzzysearch('var', 'r2f44');
    expect(search).toBe(false);
  });
});