-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtext-interpolation.js
More file actions
148 lines (116 loc) · 4.42 KB
/
Copy pathtext-interpolation.js
File metadata and controls
148 lines (116 loc) · 4.42 KB
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
143
144
145
146
147
148
require('reactive-ie8-shims');
var domify = require('domify');
var assert = require('assert');
var reactive = require('../');
describe('text interpolation', function(){
it('should support initialization', function(){
var el = domify('<div><a href="/download/{id}"><strong>Download</strong> {file}</a></div>');
var user = { id: '1234', file: 'tobi.png' };
var view = reactive(el, user);
assert('Download tobi.png' == el.children[0].textContent);
})
it('should ignore whitespace', function(){
var el = domify('<div><a href="/download/{ id }"><strong>Download</strong> { file }</a></div>');
var user = { id: '1234', file: 'tobi.png' };
var view = reactive(el, user);
assert('Download tobi.png' == el.children[0].textContent);
})
it('should ignore null values', function(){
var el = domify('<div>{ id }</div>');
var user = { id: null };
var view = reactive(el, user);
assert('' === el.textContent);
})
it('should react to changes', function(){
var el = domify('<div><a href="/download/{id}"><strong>Download</strong> {file}</a></div>');
var user = { id: '1234', file: 'tobi.png' };
var view = reactive(el, user);
assert('Download tobi.png' == el.children[0].textContent);
view.set('file', 'loki.png');
assert('Download loki.png' == el.children[0].textContent);
})
it('should support multiple properties', function(){
var el = domify('<div><p>{first} {last} is a {species}</p></div>');
var pet = { first: 'tobi', last: 'holowaychuk', species: 'ferret' };
var view = reactive(el, pet);
assert('tobi holowaychuk is a ferret' == el.children[0].textContent);
})
it('should support multiple properties in a single expression', function(){
var el = domify('<p>{first + " " + last}</p>');
var pet = { first: 'tobi', last: 'holowaychuk' };
var view = reactive(el, pet);
assert('tobi holowaychuk' == el.textContent);
view.set('last', 'ferret');
assert('tobi ferret' == el.textContent);
})
it('should support model method calls as properties', function(){
var el = domify('<p>first: {first}</p>');
var pet = {
first: function(){
return 'Loki'
}
};
reactive(el, pet);
assert('first: Loki' == el.textContent);
})
it('should support complex expressions', function(){
var el = domify('<p>first: {siblings[0]}, last: {siblings[siblings.length - 1]}</p>');
var pet = {
siblings: [
'Loki',
'Abby',
'Jane'
]
};
var view = reactive(el, pet);
assert('first: Loki, last: Jane' == el.textContent);
view.set('siblings', ['Loki', 'Abby']);
assert('first: Loki, last: Abby' == el.textContent);
})
it('should support complex model method calls as properties', function(){
var el = domify('<p>name: {casual ? first : first + " " + last}</p>');
var pet = {
casual: function(){ return false },
first: function(){ return 'Loki' },
last: function(){ return 'the Pet' }
};
reactive(el, pet);
assert('name: Loki the Pet' == el.textContent);
})
it('should support method calls as properties on the view', function(){
var el = domify('<p>name: {casual}</p>');
var pet = {
casual: function(){ return undefined },
first: function(){ return 'Loki' },
last: function(){ return 'the Pet' }
};
var view = {
casual: function(){ return pet.first() + ' ' + pet.last() }
}
reactive(el, pet, {
delegate: view
});
assert.equal('name: Loki the Pet', el.textContent);
})
it('should support the root element', function(){
var el = domify('<p>Hello {name}</a>');
var user = { name: 'Tobi' };
reactive(el, user);
assert('Hello Tobi' == el.textContent);
})
it('should support calling a property as a function', function(){
var model = { name: { first: 'Some Really Long Name' } };
var view = reactive('<p>Hello {name.first.slice(0,6)}</p>', model);
assert('Hello Some R' == view.el.textContent);
var model = { name: 'Some Really Long Name' };
var view = reactive('<p>Hello {name.slice(0,4)}</p>', model);
assert('Hello Some' == view.el.textContent);
})
it('should support setting base property', function(){
var model = { name: { first: 'foobar' } };
var view = reactive('<p>{name.first}</p>', model);
assert('foobar' == view.el.textContent);
view.set('name', { first: 'baz' });
assert('baz' == view.el.textContent);
})
})