-
Notifications
You must be signed in to change notification settings - Fork 41
/
json-tree-test.js
97 lines (88 loc) · 3.38 KB
/
json-tree-test.js
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
describe('The json-tree directive', function () {
'use strict';
var scope, compile;
beforeEach(function () {
module('angular-json-tree');
inject(function ($compile, $rootScope) {
scope = $rootScope.$new();
compile = $compile;
});
scope.someObject = {
test: 'hello',
array: [1,1,2,3,5,8],
emptyArray: [],
emptyObject: {},
subObj: {
subTest: 'hi',
subArray: [2,1,3,4,7,11],
}
};
});
var unexpandedHtml = '<json-tree object="someObject"><json-tree>';
var expandedHtml = '<json-tree object="someObject" start-expanded="true"><json-tree>';
var recursiveExpandedHtml = '<json-tree object="someObject" start-expanded="\'recursive\'"><json-tree>';
it('should generate an un-expanded tree', function () {
var html = unexpandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
expect(elem.html()).not.toMatch(/.+?<ul .+?>/);
});
it('should generate an expanded tree', function () {
var html = expandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
expect(elem.html()).toMatch(/.+?<ul .+?>/);
expect(elem[0].querySelectorAll('json-node.expanded').length).toEqual(1);
});
it('should generate a recursively expanded tree', function () {
var html = recursiveExpandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
var expanded = elem[0].querySelectorAll('json-node.expanded');
var expandable = elem[0].querySelectorAll('json-node.expandable');
expect(expandable.length).toBeGreaterThan(1);
expect(expanded.length).toEqual(expandable.length);
});
it('should indicate which expandable nodes are empty', function () {
var html = expandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
var empty = elem[0].querySelectorAll('json-node.empty');
expect(empty.length).toEqual(2);
});
it('should have no subnodes until click', function () {
var html = unexpandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
// Shouldn't have loaded subnodes
expect(elem.html()).not.toMatch(/.+?<ul .+?>/);
// Click the 'key' element
var keyElem = elem[0].querySelector('.key');
keyElem.click();
// Should have loaded subnodes
expect(elem.html()).toMatch(/.+?<ul .+?>/);
});
it('should toggle expansion on click', function () {
var html = unexpandedHtml;
var elem = angular.element(html);
compile(elem)(scope);
scope.$digest();
// Get 'key' element
var keyElem = elem[0].querySelector('.key');
// Shouldn't have expanded class
expect(elem.html()).not.toMatch(/.+?class=".+?expanded.+?".+?>/);
// Click the 'key' element
keyElem.click();
// Should have expanded class
expect(elem.html()).toMatch(/.+?class=".+?expanded.+?".+?>/);
// Click the 'key' element
keyElem.click();
// Shouldn't have expanded class
expect(elem.html()).not.toMatch(/.+?class=".+?expanded.+?".+?>/);
});
});