Commit 3cf2481e by Greg Price

Merge pull request #5077 from edx/gprice/forum-cohort-selector-test

Test cohort selector in NewPostView
parents 47a412de 1de17fb7
......@@ -9,6 +9,13 @@ class @DiscussionSpecHelper
@makeModerator = () ->
DiscussionUtil.roleIds["Moderator"].push(parseInt(window.user.id))
@makeAjaxSpy = (fakeAjax) ->
spyOn($, "ajax").andCallFake(
(params) ->
fakeAjax(params)
{always: ->}
)
@setUnderscoreFixtures = ->
for templateName in ['thread-show']
templateFixture = readFixtures('templates/discussion/' + templateName + '.underscore')
......
......@@ -2,75 +2,8 @@ describe "DiscussionThreadListView", ->
beforeEach ->
DiscussionSpecHelper.setUpGlobals()
setFixtures """
<script type="text/template" id="thread-list-item-template">
<li data-id="<%- id %>" class="forum-nav-thread<% if (typeof(read) != "undefined" && !read) { %> is-unread<% } %>">
<a href="#" class="forum-nav-thread-link">
<div class="forum-nav-thread-wrapper-0">
<%
var icon_class, sr_text;
if (thread_type == "discussion") {
icon_class = "icon-comments";
sr_text = "discussion";
} else if (endorsed) {
icon_class = "icon-ok";
sr_text = "answered question";
} else {
icon_class = "icon-question";
sr_text = "unanswered question";
}
%>
<span class="sr"><%= sr_text %></span>
<i class="icon <%= icon_class %>"></i>
</div><div class="forum-nav-thread-wrapper-1">
<span class="forum-nav-thread-title"><%- title %></span>
<%
var labels = "";
if (pinned) {
labels += '<li class="forum-nav-thread-label-pinned"><i class="icon icon-pushpin"></i>Pinned</li> ';
}
if (typeof(subscribed) != "undefined" && subscribed) {
labels += '<li class="forum-nav-thread-label-following"><i class="icon icon-star"></i>Following</li> ';
}
if (staff_authored) {
labels += '<li class="forum-nav-thread-label-staff"><i class="icon icon-user"></i>By: Staff</li> ';
}
if (community_ta_authored) {
labels += '<li class="forum-nav-thread-label-community-ta"><i class="icon icon-user"></i>By: Community TA</li> ';
}
if (labels != "") {
print('<ul class="forum-nav-thread-labels">' + labels + '</ul>');
}
%>
</div><div class="forum-nav-thread-wrapper-2">
<span class="forum-nav-thread-votes-count">+<%=
interpolate(
'%(votes_up_count)s%(span_sr_open)s votes %(span_close)s',
{'span_sr_open': '<span class="sr">', 'span_close': '</span>', 'votes_up_count': votes['up_count']},
true
)
%></span>
<span class="forum-nav-thread-comments-count <% if (unread_comments_count > 0) { %>is-unread<% } %>">
<%
var fmt;
var data = {
'span_sr_open': '<span class="sr">',
'span_close': '</span>',
'unread_comments_count': unread_comments_count,
'comments_count': comments_count
};
if (unread_comments_count > 0) {
fmt = '%(comments_count)s %(span_sr_open)scomments (%(unread_comments_count)s unread comments)%(span_close)s';
} else {
fmt = '%(comments_count)s %(span_sr_open)scomments %(span_close)s';
}
print(interpolate(fmt, data, true));
%>
</span>
</div>
</a>
</li>
</script>
DiscussionSpecHelper.setUnderscoreFixtures()
appendSetFixtures("""
<script type="text/template" id="thread-list-template">
<div class="forum-nav-header">
<a href="#" class="forum-nav-browse" aria-haspopup="true">
......@@ -126,7 +59,7 @@ describe "DiscussionThreadListView", ->
<li
class="forum-nav-browse-menu-item"
data-discussion-id="other"
data-cohorted="false"
data-cohorted="true"
>
<a href="#" class="forum-nav-browse-title">Other Category</a>
</li>
......@@ -142,6 +75,16 @@ describe "DiscussionThreadListView", ->
<option value="flagged">Flagged</option>
</select>
</label>
<% if (isCohorted && isPrivilegedUser) { %>
<label class="forum-nav-filter-cohort">
<span class="sr">Cohort:</span>
<select class="forum-nav-filter-cohort-control">
<option value="">in all cohorts</option>
<option value="1">Cohort1</option>
<option value="2">Cohort2</option>
</select>
</label>
<% } %>
<label class="forum-nav-sort">
<select class="forum-nav-sort-control">
<option value="date">by recent activity</option>
......@@ -154,19 +97,7 @@ describe "DiscussionThreadListView", ->
<div class="search-alerts"></div>
<ul class="forum-nav-thread-list"></ul>
</script>
<script aria-hidden="true" type="text/template" id="search-alert-template">
<div class="search-alert" id="search-alert-<%- cid %>">
<div class="search-alert-content">
<p class="message"><%- message %></p>
</div>
<div class="search-alert-controls">
<a href="#" class="dismiss control control-dismiss"><i class="icon icon-remove"></i></a>
</div>
</div>
</script>
<div class="forum-nav"></div>
"""
""")
@threads = [
DiscussionViewSpecHelper.makeThreadWithProps({
id: "1",
......@@ -194,16 +125,30 @@ describe "DiscussionThreadListView", ->
spyOn($, "ajax")
@discussion = new Discussion([])
@view = new DiscussionThreadListView({collection: @discussion, el: $(".forum-nav")})
@view = new DiscussionThreadListView(
collection: @discussion,
el: $("#fixture-element"),
courseSettings: new DiscussionCourseSettings({is_cohorted: true})
)
@view.render()
setupAjax = (callback) ->
$.ajax.andCallFake(
(params) =>
if callback
callback(params)
params.success({discussion_data: [], page: 1, num_pages: 1})
{always: ->}
)
renderSingleThreadWithProps = (props) ->
makeView(new Discussion([new Thread(DiscussionViewSpecHelper.makeThreadWithProps(props))])).render()
makeView = (discussion) ->
return new DiscussionThreadListView(
el: $(".forum-nav"),
collection: discussion
el: $("#fixture-element"),
collection: discussion,
courseSettings: new DiscussionCourseSettings({is_cohorted: true})
)
expectFilter = (filterVal) ->
......@@ -225,6 +170,28 @@ describe "DiscussionThreadListView", ->
expect($.ajax).toHaveBeenCalled()
)
describe "cohort selector", ->
it "should not be visible to students", ->
expect(@view.$(".forum-nav-filter-cohort-control:visible")).not.toExist()
it "should allow moderators to select visibility", ->
DiscussionSpecHelper.makeModerator()
@view.render()
expectedGroupId = null
setupAjax((params) => expect(params.data.group_id).toEqual(expectedGroupId))
_.each(
[
{val: "", expectedGroupId: undefined},
{val: "1", expectedGroupId: "1"},
{val: "2", expectedGroupId: "2"}
],
(optionInfo) =>
expectedGroupId = optionInfo.expectedGroupId
@view.$(".forum-nav-filter-cohort-control").val(optionInfo.val).change()
expect($.ajax).toHaveBeenCalled()
$.ajax.reset()
)
it "search should clear filter", ->
expectFilter(null)
@view.$(".forum-nav-filter-main-control").val("flagged")
......@@ -444,38 +411,29 @@ describe "DiscussionThreadListView", ->
it "for pinned", ->
renderSingleThreadWithProps({pinned: true})
expect($(".forum-nav-thread-label-pinned").length).toEqual(1)
expect($(".post-label-pinned").length).toEqual(1)
it "for following", ->
renderSingleThreadWithProps({subscribed: true})
expect($(".forum-nav-thread-label-following").length).toEqual(1)
expect($(".post-label-following").length).toEqual(1)
it "for moderator", ->
renderSingleThreadWithProps({user_id: @moderatorId})
expect($(".forum-nav-thread-label-staff").length).toEqual(1)
expect($(".post-label-by-staff").length).toEqual(1)
it "for administrator", ->
renderSingleThreadWithProps({user_id: @administratorId})
expect($(".forum-nav-thread-label-staff").length).toEqual(1)
expect($(".post-label-by-staff").length).toEqual(1)
it "for community TA", ->
renderSingleThreadWithProps({user_id: @communityTaId})
expect($(".forum-nav-thread-label-community-ta").length).toEqual(1)
expect($(".post-label-by-community-ta").length).toEqual(1)
it "when none should be present", ->
renderSingleThreadWithProps({})
expect($(".forum-nav-thread-labels").length).toEqual(0)
describe "browse menu", ->
setupAjax = (callback) ->
$.ajax.andCallFake(
(params) =>
if callback
callback(params)
params.success({discussion_data: [], page: 1, num_pages: 1})
{always: ->}
)
afterEach ->
# Remove handler added to make browse menu disappear
$("body").unbind("click")
......@@ -551,9 +509,30 @@ describe "DiscussionThreadListView", ->
$(".forum-nav-browse-menu-following .forum-nav-browse-title").click()
expect($(".forum-nav-browse-current").text()).toEqual("Posts I'm Following")
it "should show/hide the cohort selector", ->
DiscussionSpecHelper.makeModerator()
@view.render()
setupAjax()
_.each(
[
{selector: ".forum-nav-browse-menu-all", cohortVisibility: true},
{selector: ".forum-nav-browse-menu-following", cohortVisibility: false},
{
selector: ".forum-nav-browse-menu-item:has(.forum-nav-browse-menu-item .forum-nav-browse-menu-item)",
cohortVisibility: false
},
{selector: "[data-discussion-id=child]", cohortVisibility: false},
{selector: "[data-discussion-id=other]", cohortVisibility: true}
],
(itemInfo) =>
@view.$("#{itemInfo.selector} > .forum-nav-browse-title").click()
expect(@view.$(".forum-nav-filter-cohort").is(":visible")).toEqual(itemInfo.cohortVisibility)
)
testSelectionRequest = (callback, itemText) ->
setupAjax(callback)
$(".forum-nav-browse-title:contains(#{itemText})").click()
expect($.ajax).toHaveBeenCalled()
it "should get all discussions", ->
testSelectionRequest(
......@@ -561,15 +540,6 @@ describe "DiscussionThreadListView", ->
"All"
)
it "should get flagged threads", ->
testSelectionRequest(
(params) ->
expect(params.url.path()).toEqual(DiscussionUtil.urlFor("search"))
expect(params.data.flagged).toEqual(true)
,
"Flagged"
)
it "should get followed threads", ->
testSelectionRequest(
(params) ->
......
......@@ -7,6 +7,7 @@ class @DiscussionViewSpecHelper
pinned: false,
endorsed: false,
votes: {up_count: '0'},
read: false,
unread_comments_count: 0,
comments_count: 0,
abuse_flaggers: [],
......
# -*- coding: utf-8 -*-
describe "NewPostView", ->
beforeEach ->
setFixtures(
"""
<div class="discussion-body">
<div class="discussion-column">
<article class="new-post-article" style="display: block;"></article>
</div>
</div>
<script aria-hidden="true" type="text/template" id="new-post-template">
<form class="forum-new-post-form">
<% if (mode=="tab") { %>
<div class="post-field">
<div class="field-label">
<span class="field-label-text">
Topic Area:
</span>
<div class="field-input post-topic">
<a href="#" class="post-topic-button">
<span class="sr">${_("Discussion topics; current selection is: ")}</span>
<span class="js-selected-topic"></span>
<span class="drop-arrow" aria-hidden="true">▾</span>
</a>
<div class="topic-menu-wrapper">
<ul class="topic-menu" role="menu"><%= topics_html %></ul>
</div>
</div>
</div>
</div>
<% } %>
<select class="js-group-select">
<option value="">All Groups</option>
<option value="1">Group 1</option>
<option value="2">Group 2</option>
</select>
</form>
</script>
<script aria-hidden="true" type="text/template" id="new-post-menu-entry-template">
<li role="menuitem">
<a href="#" class="topic-title" data-discussion-id="<%- id %>" data-cohorted="<%- is_cohorted %>"><%- text %></a>
</li>
</script>
<script aria-hidden="true" type="text/template" id="new-post-menu-category-template">
<li role="menuitem">
<span class="topic-title"><%- text %></span>
<ul role="menu" class="topic-submenu"><%= entries %></ul>
</li>
</script>
"""
)
DiscussionSpecHelper.setUpGlobals()
DiscussionSpecHelper.setUnderscoreFixtures()
window.$$course_id = "edX/999/test"
spyOn(DiscussionUtil, "makeWmdEditor")
@discussion = new Discussion([], {pages: 1})
......@@ -77,10 +28,11 @@ describe "NewPostView", ->
"entries": {}
},
"allow_anonymous": true,
"allow_anonymous_to_peers": true
"allow_anonymous_to_peers": true,
"is_cohorted": true
})
@view = new NewPostView(
el: $(".new-post-article"),
el: $("#fixture-element"),
collection: @discussion,
course_settings: @course_settings,
mode: "tab"
......@@ -144,6 +96,7 @@ describe "NewPostView", ->
}
}
)
DiscussionSpecHelper.makeModerator()
view.render()
expectCohortSelectorEnabled = (view, enabled) ->
......@@ -163,6 +116,58 @@ describe "NewPostView", ->
@view.$("a.topic-title[data-discussion-id=non-cohorted]").click()
expectCohortSelectorEnabled(@view, false)
describe "cohort selector", ->
beforeEach ->
@course_settings = new DiscussionCourseSettings({
"category_map": {
"children": ["Topic"],
"entries": {"Topic": {"is_cohorted": true, "id": "topic"}}
},
"allow_anonymous": false,
"allow_anonymous_to_peers": false,
"is_cohorted": true,
"cohorts": [
{"id": 1, "name": "Cohort1"},
{"id": 2, "name": "Cohort2"}
]
})
@view = new NewPostView(
el: $("#fixture-element"),
collection: @discussion,
course_settings: @course_settings,
mode: "tab"
)
expectCohortSelectorVisible = (view, visible) ->
expect(view.$(".js-group-select").is(":visible")).toEqual(visible)
it "is not visible to students", ->
@view.render()
expectCohortSelectorVisible(@view, false)
it "allows moderators to select visibility", ->
DiscussionSpecHelper.makeModerator()
@view.render()
expectCohortSelectorVisible(@view, true)
expect(@view.$(".js-group-select").prop("disabled")).toEqual(false)
expectedGroupId = null
DiscussionSpecHelper.makeAjaxSpy(
(params) -> expect(params.data.group_id).toEqual(expectedGroupId)
)
_.each(
["1", "2", ""],
(groupIdStr) =>
expectedGroupId = groupIdStr
@view.$(".js-group-select").val(groupIdStr)
@view.$(".js-post-title").val("dummy title")
@view.$(".js-post-body textarea").val("dummy body")
@view.$(".forum-new-post-form").submit()
expect($.ajax).toHaveBeenCalled()
$.ajax.reset()
)
it "posts to the correct URL", ->
topicId = "test_topic"
spyOn($, "ajax").andCallFake(
......@@ -171,7 +176,7 @@ describe "NewPostView", ->
{always: ->}
)
view = new NewPostView(
el: $(".new-post-article"),
el: $("#fixture-element"),
collection: @discussion,
course_settings: new DiscussionCourseSettings({
allow_anonymous: false,
......
......@@ -8,7 +8,11 @@ if Backbone?
@discussion = options['discussion']
@course_settings = options['course_settings']
@nav = new DiscussionThreadListView(collection: @discussion, el: $(".forum-nav"))
@nav = new DiscussionThreadListView(
collection: @discussion,
el: $(".forum-nav"),
courseSettings: @course_settings
)
@nav.on "thread:selected", @navigateToThread
@nav.on "thread:removed", @navigateToAllThreads
@nav.on "threads:rendered", @setActiveThread
......
......@@ -13,7 +13,8 @@ if Backbone?
"change .forum-nav-filter-main-control": "chooseFilter"
"change .forum-nav-filter-cohort-control": "chooseCohort"
initialize: ->
initialize: (options) ->
@courseSettings = options.courseSettings
@displayedCollection = new Discussion(@collection.models, pages: @collection.pages)
@collection.on "change", @reloadDisplayedCollection
@discussionIds=""
......@@ -121,7 +122,12 @@ if Backbone?
render: ->
@timer = 0
@$el.html(@template())
@$el.html(
@template({
isCohorted: @courseSettings.get("is_cohorted"),
isPrivilegedUser: DiscussionUtil.isPrivilegedUser()
})
)
@$(".forum-nav-sort-control").val(@collection.sort_preference)
$(window).bind "load", @updateSidebar
......
......@@ -37,22 +37,16 @@
<option value="flagged">${_("Flagged")}</option>
%endif
</select>
</label>\
%if is_course_cohorted and is_moderator:
## Lack of indentation is intentional to avoid whitespace between this and siblings
<label class="forum-nav-filter-cohort">
</label>${"<% if (isCohorted && isPrivilegedUser) { %>"}<label class="forum-nav-filter-cohort">
## Translators: This labels a cohort menu in forum navigation
<span class="sr">${_("Cohort:")}</span>
<select class="forum-nav-filter-cohort-control">
<option value="all">${_("in all cohorts")}</option>
<option value="">${_("in all cohorts")}</option>
%for c in cohorts:
<option value="${c['id']}">${c['name']}</option>
%endfor
</select>
</label>\
%endif
## Lack of indentation is intentional to avoid whitespace between this and siblings
<label class="forum-nav-sort">
</label>${"<% } %>"}<label class="forum-nav-sort">
## Translators: This labels a sort menu in forum navigation
<span class="sr">${_("Sort:")}</span>
<select class="forum-nav-sort-control">
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment