Skip to content
This repository has been archived by the owner on Jan 28, 2020. It is now read-only.

Commit

Permalink
Merge c3a9e34 into 5cd4368
Browse files Browse the repository at this point in the history
  • Loading branch information
amir-qayyum-khan committed Nov 17, 2015
2 parents 5cd4368 + c3a9e34 commit 5308c42
Show file tree
Hide file tree
Showing 8 changed files with 214 additions and 27 deletions.
11 changes: 8 additions & 3 deletions ui/jstests/learningresources/test-learning-resource.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,14 @@ define(
function (assert) {
var div = document.createElement("div");
assert.ok($(div).html().length === 0);
LearningResources.loader("repo", "1", function () {
}, function () {
}, div);
var options = {
"repoSlug": "repo",
"learningResourceId": "1",
"refreshFromAPI": this.refreshFromAPI,
"markDirty": function() {},
"closeLearningResourcePanel": function() {}
};
LearningResources.loader(options, true, div);
assert.ok($(div).html().length > 0);
}
);
Expand Down
6 changes: 6 additions & 0 deletions ui/jstests/learningresources/test_learning_resource_panel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",

React.addons.TestUtils.renderIntoDocument(<LearningResourcePanel
repoSlug="repo"
markDirty={function() {}}
closeLearningResourcePanel={closeLearningResourcePanel}
learningResourceId="1"
ref={afterMount}/>);
Expand Down Expand Up @@ -300,6 +301,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",
React.addons.TestUtils.renderIntoDocument(<LearningResourcePanel
repoSlug="repo"
learningResourceId="1"
markDirty={function() {}}
closeLearningResourcePanel={closeLearningResourcePanel}
refreshFromAPI={refreshFromAPI}
ref={afterMount}/>);
Expand Down Expand Up @@ -353,6 +355,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",

React.addons.TestUtils.renderIntoDocument(<LearningResourcePanel
repoSlug="repo"
markDirty={function() {}}
closeLearningResourcePanel={closeLearningResourcePanel}
learningResourceId="1"
refreshFromAPI={refreshFromAPI}
Expand Down Expand Up @@ -386,6 +389,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",
};
React.addons.TestUtils.renderIntoDocument(<LearningResourcePanel
repoSlug="repo"
markDirty={function() {}}
closeLearningResourcePanel={closeLearningResourcePanel}
learningResourceId="1"
ref={afterMount}/>);
Expand Down Expand Up @@ -418,6 +422,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",
};
React.addons.TestUtils.renderIntoDocument(<LearningResourcePanel
repoSlug="repo"
markDirty={function() {}}
closeLearningResourcePanel={closeLearningResourcePanel}
learningResourceId="1"
ref={afterMount}/>);
Expand Down Expand Up @@ -482,6 +487,7 @@ define(["QUnit", "react", "test_utils", "jquery", "lodash",
<LearningResourcePanel
repoSlug="repo"
closeLearningResourcePanel={function() {}}
markDirty={function() {}}
learningResourceId="1"
ref={afterMount}/>
);
Expand Down
100 changes: 100 additions & 0 deletions ui/jstests/listing/test_listing.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -971,5 +971,105 @@ define(['QUnit', 'jquery', 'react', 'test_utils', 'utils', 'listing'],
);

});

QUnit.test('Open and close learning resource panel', function(assert) {
var done = assert.async();
var learningResourceResponse = {
"id": 1,
"learning_resource_type": "course",
"static_assets": [],
"title": "title",
"materialized_path": "/course",
"content_xml": "<course />",
"url_path": "",
"parent": null,
"copyright": "",
"xa_nr_views": 0,
"xa_nr_attempts": 0,
"xa_avg_grade": 0.0,
"xa_histogram_grade": 0.0,
"terms": ["required"]
};
var termResponseEasy = {
"id": 1,
"slug": "easy",
"label": "easy",
"weight": 1
};
var termResponseHard = {
"id": 2,
"slug": "hard",
"label": "hard",
"weight": 1
};
var vocabularyResponseDifficulty = {
"id": 2,
"slug": "difficulty",
"name": "difficulty",
"description": "Difficulty",
"vocabulary_type": "f",
"required": false,
"weight": 2147483647,
"terms": [termResponseEasy, termResponseHard],
"multi_terms": true
};
var vocabulariesResponseFirst = {
"count": 1,
"next": null,
"previous": null,
"results": [vocabularyResponseDifficulty]
};

TestUtils.initMockjax({
url: '/api/v1/repositories/test/learning_resources/' +
'1/?remove_content_xml=true',
type: 'GET',
responseText: learningResourceResponse
});
TestUtils.initMockjax({
url: '/api/v1/repositories/test/vocabularies/?type_name=course',
type: 'GET',
responseText: vocabulariesResponseFirst
});

var afterMount = function(component) {
waitForAjax(1, function() {
assert.ok(!$('.cd-panel').hasClass("is-visible"));
component.openResourcePanel(1);
waitForAjax(2, function() {
assert.ok($('.cd-panel').hasClass("is-visible"));
React.addons.TestUtils.Simulate.keyUp(component, {keyCode: 27});
component.forceUpdate(function () {
assert.ok($('.cd-panel').hasClass("is-visible"),
"Unable to close panel.");
component.setState({
requestedPanelClose: true,
isLearningResourcePanelDirty: false
}, function() {
assert.notOk($('.cd-panel').hasClass("is-visible"),
"Learning resource panel closed");
done();
});
});
});
});
};

var options = {
allExports: listingOptions.allExports,
sortingOptions: listingOptions.sortingOptions,
imageDir: listingOptions.imageDir,
pageSize: 25,
repoSlug: listingOptions.repoSlug,
loggedInUsername: listingOptions.loggedInUsername,
updateQueryString: updateQueryString,
getQueryString: getQueryString,
ref: afterMount
};

React.addons.TestUtils.renderIntoDocument(
React.createElement(Listing.ListingContainer, options)
);
});
}
);
1 change: 1 addition & 0 deletions ui/jstests/listing/test_listing_resources.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -626,3 +626,4 @@ define(['QUnit', 'jquery', 'listing_resources', 'react',
});
}
);

27 changes: 24 additions & 3 deletions ui/static/ui/js/learningresources/learning_resource_panel.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,7 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
<button className="btn btn-lg btn-primary"
onClick={this.saveLearningResourcePanel}>
Save
</button>
<button className="btn btn-lg btn-success"
</button> <button className="btn btn-lg btn-success"
onClick={this.saveAndCloseLearningResourcePanel}>
Save and Close
</button>
Expand All @@ -135,6 +134,10 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
message: undefined
});
},
closeLearningResourcePanel: function(event) {
event.preventDefault();
this.props.closeLearningResourcePanel();
},
saveLearningResourcePanel: function (event) {
event.preventDefault();
this.saveForm(false);
Expand Down Expand Up @@ -170,6 +173,8 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
if (closePanel) {
thiz.props.closeLearningResourcePanel();
}
// user can close panel
thiz.props.markDirty(false);
thiz.props.refreshFromAPI();
}).fail(function () {
thiz.setState({
Expand All @@ -179,6 +184,16 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
thiz.setState({loaded: true});
});
},
componentWillReceiveProps: function(nextProps) {
if (!_.eq(this.state.description, this.state.descriptionOriginal)) {
nextProps.markDirty(true);
} else if (!_.eq(this.state.vocabulariesAndTerms,
this.state.vocabulariesAndTermsOriginal)) {
nextProps.markDirty(true);
} else {
nextProps.markDirty(false);
}
},
componentDidMount: function () {
var thiz = this;

Expand All @@ -204,6 +219,7 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
message: undefined,
description: description,
previewUrl: previewUrl,
descriptionOriginal: _.cloneDeep(description),
});
return Utils.getVocabulariesAndTerms(
thiz.props.repoSlug, learningResourceType)
Expand All @@ -230,6 +246,9 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',

thiz.setState({
vocabulariesAndTerms: vocabulariesAndTerms,
vocabulariesAndTermsOriginal: _.cloneDeep(
vocabulariesAndTerms
)
});

if (vocabulariesAndTerms.length) {
Expand All @@ -255,7 +274,9 @@ define("learning_resource_panel", ['react', 'jquery', 'lodash',
return {
description: "",
vocabulariesAndTerms: [],
selectedVocabulary: {}
selectedVocabulary: {},
descriptionOriginal: "",
vocabulariesAndTermsOriginal: []
};
}
});
Expand Down
25 changes: 14 additions & 11 deletions ui/static/ui/js/learningresources/learning_resources.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ define('learning_resources',
'use strict';

return {
loader: function (repoSlug, learningResourceId, refreshFromAPI,
closeLearningResourcePanel, container) {
// Unmount and remount the component to ensure that its state
// is always up to date with the rest of the app.
React.unmountComponentAtNode(container);
React.render(<LearningResourcePanel
repoSlug={repoSlug}
learningResourceId={learningResourceId}
refreshFromAPI={refreshFromAPI}
closeLearningResourcePanel={closeLearningResourcePanel}
/>, container);
/** Current list of options are:
repoSlug
learningResourceId
refreshFromAPI
markDirty
closeLearningResourcePanel
*/
loader: function (options, isNewResource, container) {
if (isNewResource) {
// Unmount and remount the component to ensure that its state
// is always up to date with the rest of the app.
React.unmountComponentAtNode(container);
}
React.render(<LearningResourcePanel {...options} />, container);
}
};
});

0 comments on commit 5308c42

Please sign in to comment.