From c5b5db48cfd8bcd1b3d3737ad9732981fd6cadc7 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Thu, 31 Jul 2014 13:24:07 -0400 Subject: [PATCH] TESTS: Reset the preload store for testing --- .../javascripts/discourse/mixins/singleton.js | 10 +++++++++- app/assets/javascripts/preload_store.js | 8 ++++++++ test/javascripts/helpers/qunit_helpers.js | 4 ++++ .../javascripts/integration/header-test.js.es6 | 18 ++++-------------- test/javascripts/lib/utilities_test.js | 4 ++++ test/javascripts/test_helper.js | 2 ++ 6 files changed, 31 insertions(+), 15 deletions(-) diff --git a/app/assets/javascripts/discourse/mixins/singleton.js b/app/assets/javascripts/discourse/mixins/singleton.js index 0c81dc17538..73ce7a33e74 100644 --- a/app/assets/javascripts/discourse/mixins/singleton.js +++ b/app/assets/javascripts/discourse/mixins/singleton.js @@ -98,8 +98,16 @@ Discourse.Singleton = Em.Mixin.create({ } else { return instance.get(property); } - } + }, + /** + Resets the current singleton. Useful in testing. + + @method resetCurrent + **/ + resetCurrent: function(val) { + this._current = val; + } }); diff --git a/app/assets/javascripts/preload_store.js b/app/assets/javascripts/preload_store.js index 3f5e5d1391b..47a3fbc5c93 100644 --- a/app/assets/javascripts/preload_store.js +++ b/app/assets/javascripts/preload_store.js @@ -77,6 +77,14 @@ window.PreloadStore = { **/ remove: function(key) { if (this.data[key]) delete this.data[key]; + }, + + /** + Resets the contents of the store. Used in testing. + + **/ + reset: function() { + this.data = {}; } }; diff --git a/test/javascripts/helpers/qunit_helpers.js b/test/javascripts/helpers/qunit_helpers.js index 80f875fbc57..cb5aa1b7f2b 100644 --- a/test/javascripts/helpers/qunit_helpers.js +++ b/test/javascripts/helpers/qunit_helpers.js @@ -7,6 +7,10 @@ function integration(name, lifecycle) { if (lifecycle && lifecycle.setup) { lifecycle.setup.call(this); } + + if (lifecycle && lifecycle.user) { + Discourse.User.resetCurrent(Discourse.User.create(lifecycle.user)); + } Discourse.reset(); }, diff --git a/test/javascripts/integration/header-test.js.es6 b/test/javascripts/integration/header-test.js.es6 index 4ee6239daf9..6bf962a072b 100644 --- a/test/javascripts/integration/header-test.js.es6 +++ b/test/javascripts/integration/header-test.js.es6 @@ -1,17 +1,7 @@ -integration("Header", { - setup: function() { - var originalUser = Discourse.User.current(); - sandbox.stub(Discourse.User, "current").returns(originalUser); - Discourse.User.current.returns(Ember.Object.create({ - username: 'test', - staff: true, - site_flagged_posts_count: 1 - })); - }, - - teardown: function() { - Discourse.User.current.restore(); - } +integration("Header as Staff", { + user: { username: 'test', + staff: true, + site_flagged_posts_count: 1 } }); test("header", function() { diff --git a/test/javascripts/lib/utilities_test.js b/test/javascripts/lib/utilities_test.js index 6b611a93bd6..b51cf64f7ed 100644 --- a/test/javascripts/lib/utilities_test.js +++ b/test/javascripts/lib/utilities_test.js @@ -24,6 +24,7 @@ test("uploading one file", function() { test("new user cannot upload images", function() { Discourse.SiteSettings.newuser_max_images = 0; + Discourse.User.resetCurrent(Discourse.User.create()); sandbox.stub(bootbox, "alert"); not(validUpload([{name: "image.png"}]), 'the upload is not valid'); @@ -33,6 +34,7 @@ test("new user cannot upload images", function() { test("new user cannot upload attachments", function() { Discourse.SiteSettings.newuser_max_attachments = 0; sandbox.stub(bootbox, "alert"); + Discourse.User.resetCurrent(Discourse.User.create()); not(validUpload([{name: "roman.txt"}])); ok(bootbox.alert.calledWith(I18n.t('post.errors.attachment_upload_not_allowed_for_new_user'))); @@ -48,6 +50,7 @@ test("ensures an authorized upload", function() { }); test("prevents files that are too big from being uploaded", function() { + Discourse.User.resetCurrent(Discourse.User.create()); var image = { name: "image.png", size: 10 * 1024 }; Discourse.SiteSettings.max_image_size_kb = 5; Discourse.User.currentProp("trust_level", 1); @@ -69,6 +72,7 @@ var dummyBlob = function() { }; test("allows valid uploads to go through", function() { + Discourse.User.resetCurrent(Discourse.User.create()); Discourse.User.currentProp("trust_level", 1); Discourse.SiteSettings.max_image_size_kb = 15; sandbox.stub(bootbox, "alert"); diff --git a/test/javascripts/test_helper.js b/test/javascripts/test_helper.js index ba5a9b36798..1e6104de933 100644 --- a/test/javascripts/test_helper.js +++ b/test/javascripts/test_helper.js @@ -98,6 +98,8 @@ QUnit.testStart(function(ctx) { Discourse.SiteSettings = jQuery.extend(true, {}, Discourse.SiteSettingsOriginal); Discourse.BaseUri = "/"; Discourse.BaseUrl = ""; + Discourse.User.resetCurrent(); + PreloadStore.reset(); window.sandbox = sinon.sandbox.create();