From 93cc17cf3be9b4eba0432e2c2ff2b32251d38862 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Wed, 14 Nov 2012 15:03:55 -0500 Subject: [PATCH 1/6] Starting to generate jasmine tests for common/lib --- common/lib/.gitignore | 1 + .../lib/xmodule/jasmine_test_runner.html.erb | 44 ++++++++++++ .../xmodule/js/spec/capa/display_spec.coffee | 1 + .../xmodule/xmodule/js}/src/xmodule.coffee | 0 rakefile | 70 ++++++++++++++----- 5 files changed, 100 insertions(+), 16 deletions(-) create mode 100644 common/lib/.gitignore create mode 100644 common/lib/xmodule/jasmine_test_runner.html.erb rename common/{static/coffee => lib/xmodule/xmodule/js}/src/xmodule.coffee (100%) diff --git a/common/lib/.gitignore b/common/lib/.gitignore new file mode 100644 index 0000000000..bf6b783416 --- /dev/null +++ b/common/lib/.gitignore @@ -0,0 +1 @@ +*/jasmine_test_runner.html diff --git a/common/lib/xmodule/jasmine_test_runner.html.erb b/common/lib/xmodule/jasmine_test_runner.html.erb new file mode 100644 index 0000000000..01d55e50a9 --- /dev/null +++ b/common/lib/xmodule/jasmine_test_runner.html.erb @@ -0,0 +1,44 @@ + + + + Jasmine Test Runner + + + + + + + + + + + + + + + <% for src in js_source %> + + <% end %> + + + <% for src in js_specs %> + + <% end %> + + + + + + + + diff --git a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee index 107930c3b1..9910e8969d 100644 --- a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee @@ -8,6 +8,7 @@ describe 'Problem', -> MathJax.Hub.getAllJax.andReturn [@stubbedJax] window.update_schematics = -> + jasmine.getFixtures().fixturesPath = 'xmodule/js/fixtures' loadFixtures 'problem.html' spyOn Logger, 'log' spyOn($.fn, 'load').andCallFake (url, callback) -> diff --git a/common/static/coffee/src/xmodule.coffee b/common/lib/xmodule/xmodule/js/src/xmodule.coffee similarity index 100% rename from common/static/coffee/src/xmodule.coffee rename to common/lib/xmodule/xmodule/js/src/xmodule.coffee diff --git a/rakefile b/rakefile index 4f1c15321f..a9bcdd8d79 100644 --- a/rakefile +++ b/rakefile @@ -3,6 +3,8 @@ require 'tempfile' require 'net/http' require 'launchy' require 'colorize' +require 'erb' +require 'tempfile' # Build Constants REPO_ROOT = File.dirname(__FILE__) @@ -79,6 +81,25 @@ def django_for_jasmine(system, django_reload) end end +def template_jasmine_runner(lib) + coffee_files = Dir["#{lib}/**/js/**/*.coffee", "common/static/coffee/src/**/*.coffee"] + if !coffee_files.empty? + sh("coffee -c #{coffee_files.join(' ')}") + end + phantom_jasmine_path = File.expand_path("common/test/phantom-jasmine") + common_js_root = File.expand_path("common/static/js") + common_coffee_root = File.expand_path("common/static/coffee/src") + js_specs = Dir["#{lib}/**/js/spec/**/*.js"].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} + js_source = Dir["#{lib}/**/*.js"].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} - js_specs + template = ERB.new(File.read("#{lib}/jasmine_test_runner.html.erb")) + template_output = "#{lib}/jasmine_test_runner.html" + File.open(template_output, 'w') do |f| + f.write(template.result(binding)) + end + yield File.expand_path(template_output) +end + + def report_dir_path(dir) return File.join(REPORT_DIR, dir.to_s) end @@ -126,22 +147,6 @@ end end task :pylint => "pylint_#{system}" - desc "Open jasmine tests in your default browser" - task "browse_jasmine_#{system}" do - django_for_jasmine(system, true) do |jasmine_url| - Launchy.open(jasmine_url) - puts "Press ENTER to terminate".red - $stdin.gets - end - end - - desc "Use phantomjs to run jasmine tests from the console" - task "phantomjs_jasmine_#{system}" do - phantomjs = ENV['PHANTOMJS_PATH'] || 'phantomjs' - django_for_jasmine(system, false) do |jasmine_url| - sh("#{phantomjs} common/test/phantom-jasmine/lib/run_jasmine_test.coffee #{jasmine_url}") - end - end end $failed_tests = 0 @@ -210,6 +215,23 @@ TEST_TASK_DIRS = [] end end end + + desc "Open jasmine tests for #{system} in your default browser" + task "browse_jasmine_#{system}" do + django_for_jasmine(system, true) do |jasmine_url| + Launchy.open(jasmine_url) + puts "Press ENTER to terminate".red + $stdin.gets + end + end + + desc "Use phantomjs to run jasmine tests for #{system} from the console" + task "phantomjs_jasmine_#{system}" do + phantomjs = ENV['PHANTOMJS_PATH'] || 'phantomjs' + django_for_jasmine(system, false) do |jasmine_url| + sh("#{phantomjs} common/test/phantom-jasmine/lib/run_jasmine_test.coffee #{jasmine_url}") + end + end end desc "Reset the relational database used by django. WARNING: this will delete all of your existing users" @@ -245,6 +267,22 @@ Dir["common/lib/*"].select{|lib| File.directory?(lib)}.each do |lib| sh("nosetests #{lib}") end + desc "Open jasmine tests for #{lib} in your default browser" + task "browse_jasmine_#{lib}" do + template_jasmine_runner(lib) do |f| + sh("python -m webbrowser -t 'file://#{f}'") + puts "Press ENTER to terminate".red + $stdin.gets + end + end + + desc "Use phantomjs to run jasmine tests for #{lib} from the console" + task "phantomjs_jasmine_#{lib}" do + phantomjs = ENV['PHANTOMJS_PATH'] || 'phantomjs' + template_jasmine_runner(lib) do |f| + sh("#{phantomjs} common/test/phantom-jasmine/lib/run_jasmine_test.coffee #{f}") + end + end end task :report_dirs From bf6a147c85752fade6cae27b7dc3e3a3cdf07e2e Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Thu, 15 Nov 2012 15:14:33 -0500 Subject: [PATCH 2/6] Modify rake file to read in spec and src files correctly and also add in the lms helper file for stubRequests, etc. --- .../lib/xmodule/jasmine_test_runner.html.erb | 4 ++-- rakefile | 18 ++++++++++++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/common/lib/xmodule/jasmine_test_runner.html.erb b/common/lib/xmodule/jasmine_test_runner.html.erb index 01d55e50a9..edf4b43c2c 100644 --- a/common/lib/xmodule/jasmine_test_runner.html.erb +++ b/common/lib/xmodule/jasmine_test_runner.html.erb @@ -25,9 +25,9 @@ <% end %> - + <% for src in js_specs %> - + <% end %> diff --git a/rakefile b/rakefile index a9bcdd8d79..d2249d2ed8 100644 --- a/rakefile +++ b/rakefile @@ -89,8 +89,22 @@ def template_jasmine_runner(lib) phantom_jasmine_path = File.expand_path("common/test/phantom-jasmine") common_js_root = File.expand_path("common/static/js") common_coffee_root = File.expand_path("common/static/coffee/src") - js_specs = Dir["#{lib}/**/js/spec/**/*.js"].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} - js_source = Dir["#{lib}/**/*.js"].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} - js_specs + + # Get arrays of spec and source files, ordered by how deep they are nested below the library + # (and then alphabetically) and expanded from a relative to an absolute path + spec_glob = File.join("#{lib}", "**", "spec", "**", "*.js") + src_glob = File.join("#{lib}", "**", "src", "**", "*.js") + js_specs = Dir[spec_glob].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} + js_source = Dir[src_glob].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} + + # helper file + helper_coffee = "#{REPO_ROOT}/lms/static/coffee/spec/helper.coffee" + if File.exists?(helper_coffee) + sh("coffee -c #{helper_coffee}") + # Add the compiled helper to the beginning of the specs array + js_specs.unshift("#{REPO_ROOT}/lms/static/coffee/spec/helper.js") + end + template = ERB.new(File.read("#{lib}/jasmine_test_runner.html.erb")) template_output = "#{lib}/jasmine_test_runner.html" File.open(template_output, 'w') do |f| From 718ce4db2874568288f99bed3c464fcb51d5c83f Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Fri, 16 Nov 2012 13:02:38 -0500 Subject: [PATCH 3/6] Get Problem constructor working again, and copy the helper file from lms. --- .../xmodule/js/spec/capa/display_spec.coffee | 18 ++++- .../lib/xmodule/xmodule/js/spec/helper.coffee | 76 +++++++++++++++++++ rakefile | 8 -- 3 files changed, 90 insertions(+), 12 deletions(-) create mode 100644 common/lib/xmodule/xmodule/js/spec/helper.coffee diff --git a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee index 9910e8969d..01587b41c7 100644 --- a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee @@ -8,20 +8,30 @@ describe 'Problem', -> MathJax.Hub.getAllJax.andReturn [@stubbedJax] window.update_schematics = -> - jasmine.getFixtures().fixturesPath = 'xmodule/js/fixtures' + # load this function from spec/helper.coffee + jasmine.stubRequests() + + # note that the fixturesPath is set in spec/helper.coffee loadFixtures 'problem.html' spyOn Logger, 'log' spyOn($.fn, 'load').andCallFake (url, callback) -> $(@).html readFixtures('problem_content.html') callback() - jasmine.stubRequests() describe 'constructor', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem (" +
+
+
+
+ ") it 'set the element', -> - expect(@problem.el).toBe '#problem_1' + expect(@problem.element_id).toBe 'problem_999' describe 'bind', -> beforeEach -> diff --git a/common/lib/xmodule/xmodule/js/spec/helper.coffee b/common/lib/xmodule/xmodule/js/spec/helper.coffee new file mode 100644 index 0000000000..b661bd35c3 --- /dev/null +++ b/common/lib/xmodule/xmodule/js/spec/helper.coffee @@ -0,0 +1,76 @@ +jasmine.getFixtures().fixturesPath = 'xmodule/js/fixtures' + +jasmine.stubbedMetadata = + slowerSpeedYoutubeId: + id: 'slowerSpeedYoutubeId' + duration: 300 + normalSpeedYoutubeId: + id: 'normalSpeedYoutubeId' + duration: 200 + bogus: + duration: 100 + +jasmine.stubbedCaption = + start: [0, 10000, 20000, 30000] + text: ['Caption at 0', 'Caption at 10000', 'Caption at 20000', 'Caption at 30000'] + +jasmine.stubRequests = -> + spyOn($, 'ajax').andCallFake (settings) -> + if match = settings.url.match /youtube\.com\/.+\/videos\/(.+)\?v=2&alt=jsonc/ + settings.success data: jasmine.stubbedMetadata[match[1]] + else if match = settings.url.match /static\/subs\/(.+)\.srt\.sjson/ + settings.success jasmine.stubbedCaption + else if settings.url.match /.+\/problem_get$/ + settings.success html: readFixtures('problem_content.html') + else if settings.url == '/calculate' || + settings.url.match(/modx\/.+\/goto_position$/) || + settings.url.match(/event$/) || + settings.url.match(/modx\/.+\/problem_(check|reset|show|save)$/) + # do nothing + else + throw "External request attempted for #{settings.url}, which is not defined." + +jasmine.stubYoutubePlayer = -> + YT.Player = -> jasmine.createSpyObj 'YT.Player', ['cueVideoById', 'getVideoEmbedCode', + 'getCurrentTime', 'getPlayerState', 'getVolume', 'setVolume', 'loadVideoById', + 'playVideo', 'pauseVideo', 'seekTo'] + +jasmine.stubVideoPlayer = (context, enableParts, createPlayer=true) -> + enableParts = [enableParts] unless $.isArray(enableParts) + + suite = context.suite + currentPartName = suite.description while suite = suite.parentSuite + enableParts.push currentPartName + + for part in ['VideoCaption', 'VideoSpeedControl', 'VideoVolumeControl', 'VideoProgressSlider'] + unless $.inArray(part, enableParts) >= 0 + spyOn window, part + + loadFixtures 'video.html' + jasmine.stubRequests() + YT.Player = undefined + context.video = new Video 'example', '.75:slowerSpeedYoutubeId,1.0:normalSpeedYoutubeId' + jasmine.stubYoutubePlayer() + if createPlayer + return new VideoPlayer(video: context.video) + +spyOn(window, 'onunload') + +# Stub Youtube API +window.YT = + PlayerState: + UNSTARTED: -1 + ENDED: 0 + PLAYING: 1 + PAUSED: 2 + BUFFERING: 3 + CUED: 5 + +# Stub jQuery.cookie +$.cookie = jasmine.createSpy('jQuery.cookie').andReturn '1.0' + +# Stub jQuery.qtip +$.fn.qtip = jasmine.createSpy 'jQuery.qtip' + +# Stub jQuery.scrollTo +$.fn.scrollTo = jasmine.createSpy 'jQuery.scrollTo' diff --git a/rakefile b/rakefile index d2249d2ed8..c05f06b9d6 100644 --- a/rakefile +++ b/rakefile @@ -97,14 +97,6 @@ def template_jasmine_runner(lib) js_specs = Dir[spec_glob].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} js_source = Dir[src_glob].sort_by {|p| [p.split('/').length, p]} .map {|f| File.expand_path(f)} - # helper file - helper_coffee = "#{REPO_ROOT}/lms/static/coffee/spec/helper.coffee" - if File.exists?(helper_coffee) - sh("coffee -c #{helper_coffee}") - # Add the compiled helper to the beginning of the specs array - js_specs.unshift("#{REPO_ROOT}/lms/static/coffee/spec/helper.js") - end - template = ERB.new(File.read("#{lib}/jasmine_test_runner.html.erb")) template_output = "#{lib}/jasmine_test_runner.html" File.open(template_output, 'w') do |f| From 6ae0add931bc2f95bbb05e58b8440f82df51313c Mon Sep 17 00:00:00 2001 From: Jay Zoldak Date: Fri, 16 Nov 2012 13:26:16 -0500 Subject: [PATCH 4/6] Fix the new problem create statements in the describe blocks for the capa display spec. --- .../xmodule/xmodule/js/fixtures/problem.html | 3 +- .../xmodule/xmodule/js/fixtures/xmodule.html | 7 ++++ .../xmodule/js/spec/capa/display_spec.coffee | 32 +++++++++++-------- 3 files changed, 27 insertions(+), 15 deletions(-) create mode 100644 common/lib/xmodule/xmodule/js/fixtures/xmodule.html diff --git a/common/lib/xmodule/xmodule/js/fixtures/problem.html b/common/lib/xmodule/xmodule/js/fixtures/problem.html index f77ece7845..e04dbdb724 100644 --- a/common/lib/xmodule/xmodule/js/fixtures/problem.html +++ b/common/lib/xmodule/xmodule/js/fixtures/problem.html @@ -1 +1,2 @@ -
+
\ No newline at end of file diff --git a/common/lib/xmodule/xmodule/js/fixtures/xmodule.html b/common/lib/xmodule/xmodule/js/fixtures/xmodule.html new file mode 100644 index 0000000000..392a6c9698 --- /dev/null +++ b/common/lib/xmodule/xmodule/js/fixtures/xmodule.html @@ -0,0 +1,7 @@ +
+
+
+
\ No newline at end of file diff --git a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee index 01587b41c7..ebe9eacfc9 100644 --- a/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee +++ b/common/lib/xmodule/xmodule/js/spec/capa/display_spec.coffee @@ -13,14 +13,16 @@ describe 'Problem', -> # note that the fixturesPath is set in spec/helper.coffee loadFixtures 'problem.html' + loadFixtures 'xmodule.html' spyOn Logger, 'log' spyOn($.fn, 'load').andCallFake (url, callback) -> $(@).html readFixtures('problem_content.html') callback() describe 'constructor', -> - beforeEach -> - @problem = new Problem (" + + it 'set the element from html', -> + @problem999 = new Problem ("
data-url='/problem/quiz/'>
- ") + ") + expect(@problem999.element_id).toBe 'problem_999' - it 'set the element', -> - expect(@problem.element_id).toBe 'problem_999' + it 'set the element from loadFixtures', -> + @problem101 = new Problem($('.xmodule_display')) + expect(@problem101.element_id).toBe 'problem_101' describe 'bind', -> beforeEach -> spyOn window, 'update_schematics' MathJax.Hub.getAllJax.andReturn [@stubbedJax] - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) it 'set mathjax typeset', -> expect(MathJax.Hub.Queue).toHaveBeenCalled() @@ -71,7 +75,7 @@ describe 'Problem', -> describe 'render', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @bind = @problem.bind spyOn @problem, 'bind' @@ -99,7 +103,7 @@ describe 'Problem', -> describe 'check', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @problem.answers = 'foo=1&bar=2' it 'log the problem_check event', -> @@ -132,7 +136,7 @@ describe 'Problem', -> describe 'reset', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) it 'log the problem_reset event', -> @problem.answers = 'foo=1&bar=2' @@ -152,7 +156,7 @@ describe 'Problem', -> describe 'show', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @problem.el.prepend '
' describe 'when the answer has not yet shown', -> @@ -231,7 +235,7 @@ describe 'Problem', -> describe 'save', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @problem.answers = 'foo=1&bar=2' it 'log the problem_save event', -> @@ -251,7 +255,7 @@ describe 'Problem', -> describe 'refreshMath', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) $('#input_example_1').val 'E=mc^2' @problem.refreshMath target: $('#input_example_1').get(0) @@ -261,7 +265,7 @@ describe 'Problem', -> describe 'updateMathML', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @stubbedJax.root.toMathML.andReturn '' describe 'when there is no exception', -> @@ -281,7 +285,7 @@ describe 'Problem', -> describe 'refreshAnswers', -> beforeEach -> - @problem = new Problem 1, "problem_1", "/problem/url/" + @problem = new Problem($('.xmodule_display')) @problem.el.html '''