From c550963fb99cc2ff4acfbd44bf1686fd8606abe7 Mon Sep 17 00:00:00 2001 From: cahrens Date: Fri, 7 Nov 2014 09:52:54 -0500 Subject: [PATCH] Set up validation_messages so that it goes through the RequireJS optimizer. --- cms/static/build.js | 3 +- cms/static/coffee/spec/main.coffee | 2 + cms/static/js/factories/xblock_validation.js | 15 ++++ .../spec/factories/xblock_validation_spec.js | 78 +++++++++++++++++++ cms/templates/studio_xblock_wrapper.html | 33 +++----- 5 files changed, 106 insertions(+), 25 deletions(-) create mode 100644 cms/static/js/factories/xblock_validation.js create mode 100644 cms/static/js/spec/factories/xblock_validation_spec.js diff --git a/cms/static/build.js b/cms/static/build.js index d211b2a0e6..4999c9ab88 100644 --- a/cms/static/build.js +++ b/cms/static/build.js @@ -45,7 +45,8 @@ 'js/factories/settings', 'js/factories/settings_advanced', 'js/factories/settings_graders', - 'js/factories/textbooks' + 'js/factories/textbooks', + 'js/factories/xblock_validation' ]), /** * By default all the configuration for optimization happens from the command diff --git a/cms/static/coffee/spec/main.coffee b/cms/static/coffee/spec/main.coffee index 45ff7269d6..62829c9b7f 100644 --- a/cms/static/coffee/spec/main.coffee +++ b/cms/static/coffee/spec/main.coffee @@ -244,6 +244,8 @@ define([ "js/spec/views/modals/edit_xblock_spec", "js/spec/views/modals/validation_error_modal_spec", + "js/spec/factories/xblock_validation_spec", + "js/spec/xblock/cms.runtime.v1_spec", # these tests are run separately in the cms-squire suite, due to process diff --git a/cms/static/js/factories/xblock_validation.js b/cms/static/js/factories/xblock_validation.js new file mode 100644 index 0000000000..61e0d8b91a --- /dev/null +++ b/cms/static/js/factories/xblock_validation.js @@ -0,0 +1,15 @@ +define(["js/views/xblock_validation", "js/models/xblock_validation"], +function (XBlockValidationView, XBlockValidationModel) { + 'use strict'; + return function (validationMessages, hasEditingUrl, isRoot, validationEle) { + if (hasEditingUrl && !isRoot) { + validationMessages.showSummaryOnly = true; + } + + var model = new XBlockValidationModel(validationMessages, {parse: true}); + + if (!model.get("empty")) { + new XBlockValidationView({el: validationEle, model: model, root: isRoot}).render(); + } + }; +}); diff --git a/cms/static/js/spec/factories/xblock_validation_spec.js b/cms/static/js/spec/factories/xblock_validation_spec.js new file mode 100644 index 0000000000..19dc94d71e --- /dev/null +++ b/cms/static/js/spec/factories/xblock_validation_spec.js @@ -0,0 +1,78 @@ +define(['jquery', 'js/factories/xblock_validation', 'js/common_helpers/template_helpers'], + function($, XBlockValidationFactory, TemplateHelpers) { + + describe('XBlockValidationFactory', function() { + var messageDiv; + + beforeEach(function () { + TemplateHelpers.installTemplate('xblock-validation-messages'); + appendSetFixtures($('
')); + messageDiv = $('.messages'); + }); + + it('Does not attach a view if messages is empty', function() { + XBlockValidationFactory({"empty": true}, false, false, messageDiv); + expect(messageDiv.children().length).toEqual(0); + }); + + it('Does attach a view if messages are not empty', function() { + XBlockValidationFactory({"empty": false}, false, false, messageDiv); + expect(messageDiv.children().length).toEqual(1); + }); + + it('Passes through the root property to the view.', function() { + var noContainerContent = "no-container-content"; + + var notConfiguredMessages = { + "empty": false, + "summary": {"text": "my summary", "type": "not-configured"}, + "messages": [], + "xblock_id": "id" + }; + // Root is false, will not add noContainerContent. + XBlockValidationFactory(notConfiguredMessages, true, false, messageDiv); + expect(messageDiv.find('.validation')).not.toHaveClass(noContainerContent); + + // Root is true, will add noContainerContent. + XBlockValidationFactory(notConfiguredMessages, true, true, messageDiv); + expect(messageDiv.find('.validation')).toHaveClass(noContainerContent); + }); + + describe('Controls display of detailed messages based on url and root property', function() { + var messagesWithSummary, checkDetailedMessages; + + beforeEach(function () { + messagesWithSummary = { + "empty": false, + "summary": {"text": "my summary"}, + "messages": [{"text": "one", "type": "warning"}, {"text": "two", "type": "error"}], + "xblock_id": "id" + }; + }); + + checkDetailedMessages = function (expectedDetailedMessages) { + expect(messageDiv.children().length).toEqual(1); + expect(messageDiv.find('.xblock-message-item').length).toBe(expectedDetailedMessages); + }; + + it('Does not show details if xblock has an editing URL and it is not rendered as root', function() { + XBlockValidationFactory(messagesWithSummary, true, false, messageDiv); + checkDetailedMessages(0); + }); + + it('Shows details if xblock does not have its own editing URL, regardless of root value', function() { + XBlockValidationFactory(messagesWithSummary, false, false, messageDiv); + checkDetailedMessages(2); + + XBlockValidationFactory(messagesWithSummary, false, true, messageDiv); + checkDetailedMessages(2); + }); + + it('Shows details if xblock has its own editing URL and is rendered as root', function() { + XBlockValidationFactory(messagesWithSummary, true, true, messageDiv); + checkDetailedMessages(2); + }); + }); + }); + } +); diff --git a/cms/templates/studio_xblock_wrapper.html b/cms/templates/studio_xblock_wrapper.html index 1573773561..d6c707b661 100644 --- a/cms/templates/studio_xblock_wrapper.html +++ b/cms/templates/studio_xblock_wrapper.html @@ -1,4 +1,3 @@ - <%! from django.utils.translation import ugettext as _ from contentstore.views.helpers import xblock_studio_url @@ -21,31 +20,17 @@ messages = json.dumps(xblock.validate().to_json()) - - % if not is_root: % if is_reorderable: