Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
207453a3
Commit
207453a3
authored
Jun 14, 2013
by
David Baumgold
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Further basic tests for Backbone models
parent
001cd5d0
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
67 additions
and
9 deletions
+67
-9
cms/static/coffee/spec/models/textbook_spec.coffee
+58
-1
cms/static/js/models/textbook.js
+9
-8
No files found.
cms/static/coffee/spec/models/textbook_spec.coffee
View file @
207453a3
beforeEach
->
@
addMatchers
toBeInstanceOf
:
(
expected
)
->
return
@
actual
instanceof
expected
describe
"CMS.Models.Textbook"
,
->
beforeEach
->
@
model
=
new
CMS
.
Models
.
Textbook
()
...
...
@@ -9,4 +15,55 @@ describe "CMS.Models.Textbook", ->
expect
(
@
model
.
get
(
"showChapters"
)).
toBeFalsy
()
it
"should have a ChapterSet with one chapter by default"
,
->
expect
(
@
model
.
get
(
"chapters"
).
length
).
toEqual
(
1
)
chapters
=
@
model
.
get
(
"chapters"
)
expect
(
chapters
).
toBeInstanceOf
(
CMS
.
Collections
.
ChapterSet
)
expect
(
chapters
.
length
).
toEqual
(
1
)
it
"should be empty by default"
,
->
expect
(
@
model
.
isEmpty
()).
toBeTruthy
()
describe
"CMS.Models.Textbook input/output"
,
->
# replace with Backbone.Assocations.deepAttributes when
# https://github.com/dhruvaray/backbone-associations/pull/43 is merged
deepAttributes
=
(
obj
)
->
if
obj
instanceof
Backbone
.
Model
deepAttributes
(
obj
.
attributes
)
else
if
obj
instanceof
Backbone
.
Collection
obj
.
map
(
deepAttributes
);
else
if
_
.
isArray
(
obj
)
_
.
map
(
obj
,
deepAttributes
);
else
if
_
.
isObject
(
obj
)
attributes
=
{};
for
own
prop
,
val
of
obj
attributes
[
prop
]
=
deepAttributes
(
val
)
attributes
else
obj
it
"should match server model to client model"
,
->
serverModelSpec
=
{
"tab_title"
:
"My Textbook"
,
"chapters"
:
[
{
"title"
:
"Chapter 1"
,
"url"
:
"/ch1.pdf"
},
{
"title"
:
"Chapter 2"
,
"url"
:
"/ch2.pdf"
},
]
}
clientModelSpec
=
{
"name"
:
"My Textbook"
,
"showChapters"
:
false
,
"chapters"
:
[{
"name"
:
"Chapter 1"
,
"asset_path"
:
"/ch1.pdf"
,
"order"
:
1
},
{
"name"
:
"Chapter 2"
,
"asset_path"
:
"/ch2.pdf"
,
"order"
:
2
}
]
}
model
=
new
CMS
.
Models
.
Textbook
(
serverModelSpec
,
{
parse
:
true
})
expect
(
deepAttributes
(
model
)).
toEqual
(
clientModelSpec
)
expect
(
model
.
toJSON
()).
toEqual
(
serverModelSpec
)
cms/static/js/models/textbook.js
View file @
207453a3
...
...
@@ -16,18 +16,19 @@ CMS.Models.Textbook = Backbone.AssociatedModel.extend({
return
!
this
.
get
(
'name'
)
&&
this
.
get
(
'chapters'
).
isEmpty
();
},
parse
:
function
(
response
)
{
if
(
"tab_title"
in
response
&&
!
(
"name"
in
response
))
{
response
.
name
=
response
.
tab_title
;
delete
response
.
tab_title
;
var
ret
=
$
.
extend
(
true
,
{},
response
);
if
(
"tab_title"
in
ret
&&
!
(
"name"
in
ret
))
{
ret
.
name
=
ret
.
tab_title
;
delete
ret
.
tab_title
;
}
if
(
"url"
in
re
sponse
&&
!
(
"chapters"
in
response
))
{
re
sponse
.
chapters
=
{
"url"
:
response
.
url
};
delete
re
sponse
.
url
;
if
(
"url"
in
re
t
&&
!
(
"chapters"
in
ret
))
{
re
t
.
chapters
=
{
"url"
:
ret
.
url
};
delete
re
t
.
url
;
}
_
.
each
(
re
sponse
.
chapters
,
function
(
chapter
,
i
)
{
_
.
each
(
re
t
.
chapters
,
function
(
chapter
,
i
)
{
chapter
.
order
=
chapter
.
order
||
i
+
1
;
});
return
re
sponse
;
return
re
t
;
},
toJSON
:
function
()
{
return
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment