Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
brainfood
/
rivets
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Graphs
Network
Create a new issue
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
8739f9af
authored
2012-07-08 23:24:11 +0200
by
Nicklas Ansman Giertz
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
Implement a few tests
1 parent
a348cee6
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
125 additions
and
33 deletions
build/rivets.js
spec/matchers.js
spec/mock.data.js
spec/rivets.js
test.html
build/rivets.js
View file @
8739f9a
...
...
@@ -88,19 +88,16 @@
};
View
.
prototype
.
build
=
function
()
{
var
attribute
,
bindingRegExp
,
keypath
,
model
,
node
,
path
,
pipes
,
type
,
_i
,
_len
,
_ref
,
_results
;
var
attribute
,
bindingRegExp
,
elements
,
keypath
,
model
,
node
,
path
,
pipes
,
type
,
_i
,
_j
,
_len
,
_len1
,
_ref
;
this
.
bindings
=
[];
bindingRegExp
=
this
.
bindingRegExp
();
_ref
=
this
.
el
.
getElementsByTagName
(
'*'
);
_results
=
[];
for
(
_i
=
0
,
_len
=
_ref
.
length
;
_i
<
_len
;
_i
++
)
{
node
=
_ref
[
_i
];
_results
.
push
((
function
()
{
var
_j
,
_len1
,
_ref1
,
_results1
;
_ref1
=
node
.
attributes
;
_results1
=
[];
for
(
_j
=
0
,
_len1
=
_ref1
.
length
;
_j
<
_len1
;
_j
++
)
{
attribute
=
_ref1
[
_j
];
elements
=
[
this
.
el
];
elements
.
concat
(
Array
.
prototype
.
slice
.
call
(
this
.
el
.
getElementsByTagName
(
'*'
)));
for
(
_i
=
0
,
_len
=
elements
.
length
;
_i
<
_len
;
_i
++
)
{
node
=
elements
[
_i
];
_ref
=
node
.
attributes
;
for
(
_j
=
0
,
_len1
=
_ref
.
length
;
_j
<
_len1
;
_j
++
)
{
attribute
=
_ref
[
_j
];
if
(
bindingRegExp
.
test
(
attribute
.
name
))
{
type
=
attribute
.
name
.
replace
(
bindingRegExp
,
''
);
pipes
=
attribute
.
value
.
split
(
'|'
).
map
(
function
(
pipe
)
{
...
...
@@ -109,15 +106,10 @@
path
=
pipes
.
shift
().
split
(
'.'
);
model
=
this
.
models
[
path
.
shift
()];
keypath
=
path
.
join
(
'.'
);
_results1
.
push
(
this
.
bindings
.
push
(
new
Rivets
.
Binding
(
node
,
type
,
model
,
keypath
,
pipes
)));
}
else
{
_results1
.
push
(
void
0
);
this
.
bindings
.
push
(
new
Rivets
.
Binding
(
node
,
type
,
model
,
keypath
,
pipes
));
}
}
return
_results1
;
}).
call
(
this
));
}
return
_results
;
};
View
.
prototype
.
bind
=
function
()
{
...
...
spec/matchers.js
0 → 100644
View file @
8739f9a
beforeEach
(
function
()
{
this
.
addMatchers
({
toHaveTheTextContent
:
function
(
expected
)
{
var
el
=
this
.
actual
;
actual
=
el
.
textContent
||
el
.
innerText
;
this
.
message
=
function
()
{
return
"Expected '"
+
actual
+
"' to be '"
+
expected
+
"'"
;
}
return
actual
===
expected
;
}
});
});
spec/mock.data.js
View file @
8739f9a
...
...
@@ -4,11 +4,32 @@ function Data(attributes) {
}
Data
.
prototype
.
on
=
function
(
key
,
callback
)
{
this
.
change
[
key
][
callback
]
=
true
;
if
(
this
.
hasCallback
(
key
,
callback
))
return
;
var
ref
=
this
.
change
[
key
]
||
(
this
.
change
[
key
]
=
[]);
this
.
change
[
key
].
push
(
callback
);
}
Data
.
prototype
.
hasCallback
=
function
(
key
,
callback
)
{
return
indexOf
(
this
.
change
[
key
],
callback
)
!==
-
1
;
}
indexOf
=
function
(
array
,
value
)
{
array
||
(
array
=
[])
if
(
array
.
indexOf
)
return
array
.
indexOf
(
value
);
for
(
i
in
array
||
{})
{
if
(
array
[
i
]
===
value
)
return
i
;
}
return
-
1
;
}
Data
.
prototype
.
off
=
function
(
key
,
callback
)
{
delete
this
.
change
[
key
][
callback
];
var
index
=
indexOf
(
this
.
change
[
key
],
callback
);
if
(
index
!==
-
1
)
this
.
change
[
key
].
splice
(
index
,
1
);
}
Data
.
prototype
.
set
=
function
(
attributes
)
{
...
...
@@ -23,15 +44,17 @@ Data.prototype.set = function(attributes) {
}
Data
.
prototype
.
get
=
function
(
key
)
{
this
.
attributes
[
key
];
return
this
.
attributes
[
key
];
}
Data
.
prototype
.
alertCallbacks
=
function
(
key
)
{
if
(
!
this
.
change
[
key
])
return
;
for
(
callback
in
this
.
change
[
key
])
callback
(
this
.
get
(
key
));
var
key
,
callbacks
;
for
(
i
in
this
.
change
[
key
])
{
this
.
change
[
key
][
i
](
this
.
get
(
key
));
}
}
window
.
Data
=
Data
;
...
...
spec/rivets.js
View file @
8739f9a
rivets
.
configure
({
describe
(
'Rivets'
,
function
()
{
var
data
,
bindData
,
el
;
beforeEach
(
function
()
{
data
=
new
Data
({
foo
:
'bar'
});
bindData
=
{
data
:
data
};
el
=
document
.
createElement
(
'div'
);
rivets
.
configure
({
preloadData
:
true
,
adapter
:
{
subscribe
:
function
(
obj
,
keypath
,
callback
)
{
obj
.
on
(
keypath
,
callback
);
},
read
:
function
(
obj
,
keypath
)
{
obj
.
get
(
keypath
);
return
obj
.
get
(
keypath
);
},
publish
:
function
(
obj
,
keypath
,
value
)
{
attributes
=
{};
...
...
@@ -12,20 +23,73 @@ rivets.configure({
obj
.
set
(
attributes
);
}
}
});
});
});
describe
(
'Adapter'
,
function
()
{
it
(
'should read the initial value'
,
function
()
{
spyOn
(
data
,
'get'
);
el
.
setAttribute
(
'data-text'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
expect
(
data
.
get
).
toHaveBeenCalledWith
(
'foo'
);
});
describe
(
'Rivets: '
,
function
()
{
var
data
;
it
(
'should read the initial value unless preloadData is false'
,
function
()
{
rivets
.
configure
({
preloadData
:
false
});
spyOn
(
data
,
'get'
);
el
.
setAttribute
(
'data-value'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
expect
(
data
.
get
).
not
.
toHaveBeenCalled
();
});
beforeEach
(
function
()
{
data
=
new
Data
();
it
(
'should subscribe to updates'
,
function
()
{
spyOn
(
data
,
'on'
);
el
.
setAttribute
(
'data-value'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
expect
(
data
.
on
).
toHaveBeenCalled
();
});
});
describe
(
'Stuff'
,
function
()
{
it
(
'should work'
,
function
()
{
expect
(
data
.
get
(
'foo'
)).
toBe
(
undefined
);
expect
(
data
.
get
(
'bar'
)).
toBe
(
'not undefined'
);
describe
(
'Binds'
,
function
()
{
describe
(
'Text'
,
function
()
{
it
(
'should set the text content of the element'
,
function
()
{
el
.
setAttribute
(
'data-text'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
expect
(
el
.
textContent
||
el
.
innerText
).
toBe
(
data
.
get
(
'foo'
));
});
it
(
'should correctly handle HTML in the content'
,
function
()
{
el
.
setAttribute
(
'data-text'
,
'data.foo'
);
value
=
'<b>Fail</b>'
;
data
.
set
({
foo
:
value
});
rivets
.
bind
(
el
,
bindData
);
expect
(
el
.
textContent
||
el
.
innerText
).
toBe
(
value
);
});
});
describe
(
'HTML'
,
function
()
{
it
(
'should set the html content of the element'
,
function
()
{
el
.
setAttribute
(
'data-html'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
expect
(
el
).
toHaveTheTextContent
(
data
.
get
(
'foo'
));
});
it
(
'should correctly handle HTML in the content'
,
function
()
{
el
.
setAttribute
(
'data-html'
,
'data.foo'
);
value
=
'<b>Fail</b>'
;
data
.
set
({
foo
:
value
});
rivets
.
bind
(
el
,
bindData
);
expect
(
el
.
innerHTML
).
toBe
(
value
);
});
});
describe
(
'Updates'
,
function
()
{
it
(
'should change the value'
,
function
()
{
el
.
setAttribute
(
'data-text'
,
'data.foo'
);
rivets
.
bind
(
el
,
bindData
);
data
.
set
({
foo
:
'some new value'
});
expect
(
el
).
toHaveTheTextContent
(
data
.
get
(
'foo'
));
});
});
});
});
...
...
test.html
View file @
8739f9a
...
...
@@ -11,6 +11,7 @@
<script
type=
"text/javascript"
src=
"build/rivets.js"
></script>
<script
type=
"text/javascript"
src=
"spec/matchers.js"
></script>
<script
type=
"text/javascript"
src=
"spec/mock.data.js"
></script>
<script
type=
"text/javascript"
src=
"spec/rivets.js"
></script>
...
...
Please
register
or
sign in
to post a comment