This repository has been archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
attributes.test.js
110 lines (81 loc) · 3.3 KB
/
attributes.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
98
99
100
101
102
103
104
105
106
107
108
109
110
var expect = require('chai').expect;
var jsdom = require('mocha-jsdom');
var h = require('../../').h;
var diff = require('../../').diff;
var patch = require('../../').patch;
describe('Test attributes', function() {
jsdom();
it('should setup style attribute', function() {
var a = h('div', { style: { backgroundColor: '#fff', left: '20px' } },
h('div', { style: 'right:20px;' })
);
var dom = a.render();
expect(a.props['style']).to.be.exists;
expect(a.props['style']).to.be.deep.equal({ backgroundColor: '#fff', left: '20px' });
expect(dom.getAttribute('style')).to.be.a('string');
expect(dom.getAttribute('style')).to.be.equal('background-color:#fff;left:20px;');
expect(a.children[0].props['style']).to.be.equal('right:20px;');
});
it('should diff and patch style attribute', function() {
var a = h('div', { style: { left: '21px' }}, '');
var b = h('div', { style: { backgroundColor: '#fff', left: '20px' } },
h('div', { style: 'right:20px;' })
);
var dom = a.render();
patch(a, diff(a, b));
expect(a.props['style']).to.be.exists;
expect(a.props['style']).to.be.deep.equal({ backgroundColor: '#fff', left: '20px' });
expect(dom.getAttribute('style')).to.be.a('string');
expect(dom.getAttribute('style')).to.be.equal('background-color:#fff;left:20px;');
expect(a.children[0].props['style']).to.be.equal('right:20px;');
});
it('should patch attrs modified via dom node', function () {
var a = h('div', null, '');
var b = h('div', null, '');
var dom = a.render();
dom.classList.add('active');
patch(a, diff(a, b));
expect(dom.attributes.length).to.be.equal(0);
expect(dom.props).to.be.an('undefined');
});
it('should patch attrs created via dom node', function () {
var a = h('div', { onClick: function () {}}, '');
var b = h('div', { ref: function () {}, onClick: function () {}} , '');
var dom = a.render();
dom.classList.add('active');
expect(dom.attributes.length).to.be.equal(1);
patch(a, diff(a, b));
expect(dom.attributes.length).to.be.equal(0);
expect(Object.keys(a.props).length).to.be.equal(2);
});
it('should patch attrs created via dom node #2', function () {
var a = h('div', { onClick: function () {}}, '');
var b = h('div', { onClick: function () {}, tabindex: 0} , '');
var dom = a.render();
dom.classList.add('active');
expect(dom.attributes.length).to.be.equal(1);
patch(a, diff(a, b));
expect(dom.attributes.length).to.be.equal(1);
expect(dom.attributes[0].name).to.be.equal('tabindex');
expect(Object.keys(a.props).length).to.be.equal(2);
});
it('should patch listeners #1', function () {
var listener1 = function() { return 1; };
var a = h('div', { onClick: listener1}, '');
var b = h('div', { onClick: function () { return 2; }, tabindex: 0} , '');
a.render();
expect(a.props.onClick()).to.be.equal(1);
patch(a, diff(a, b));
expect(a.props.onClick()).to.be.equal(2);
});
it('should patch replace listeners #2', function () {
var fail = false;
var listener1 = function() { fail = true; };
var a = h('div', { onClick: listener1}, '');
var b = h('div', { tabindex: 0} , '');
var dom = a.render();
patch(a, diff(a, b));
dom.click();
expect(fail).to.be.false;
});
});