Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
E
edx-platform-release
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Package Registry
Model registry
Operate
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Hsin-Yu Chien
edx-platform-release
Commits
207453a3
Commit
207453a3
authored
11 years ago
by
David Baumgold
Browse files
Options
Downloads
Patches
Plain Diff
Further basic tests for Backbone models
parent
001cd5d0
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
cms/static/coffee/spec/models/textbook_spec.coffee
+58
-1
58 additions, 1 deletion
cms/static/coffee/spec/models/textbook_spec.coffee
cms/static/js/models/textbook.js
+9
-8
9 additions, 8 deletions
cms/static/js/models/textbook.js
with
67 additions
and
9 deletions
cms/static/coffee/spec/models/textbook_spec.coffee
+
58
−
1
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
)
This diff is collapsed.
Click to expand it.
cms/static/js/models/textbook.js
+
9
−
8
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
re
sponse
))
{
re
sponse
.
chapters
=
{
"
url
"
:
re
sponse
.
url
};
delete
re
sponse
.
url
;
if
(
"
url
"
in
re
t
&&
!
(
"
chapters
"
in
re
t
))
{
re
t
.
chapters
=
{
"
url
"
:
re
t
.
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
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment