diff --git a/assets/javascripts/discourse/components/google-adsense.js.es6 b/assets/javascripts/discourse/components/google-adsense.js.es6 index 2fa3abb..bdab286 100644 --- a/assets/javascripts/discourse/components/google-adsense.js.es6 +++ b/assets/javascripts/discourse/components/google-adsense.js.es6 @@ -1,6 +1,10 @@ +import { + default as computed, + observes +} from "ember-addons/ember-computed-decorators"; import loadScript from "discourse/lib/load-script"; -var _loaded = false, +let _loaded = false, _promise = null, currentUser = Discourse.User.current(), publisher_id = Discourse.SiteSettings.adsense_publisher_code; @@ -30,7 +34,7 @@ function loadAdsense() { return _promise; } - var adsenseSrc = + const adsenseSrc = ("https:" === document.location.protocol ? "https:" : "http:") + "//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"; _promise = loadScript(adsenseSrc, { scriptTag: true }).then(function() { @@ -40,7 +44,7 @@ function loadAdsense() { return _promise; } -var data = { +let data = { "topic-list-top": {}, "topic-above-post-stream": {}, "topic-above-suggested": {}, @@ -188,6 +192,7 @@ export default Ember.Component.extend({ Ember.run.scheduleOnce("afterRender", this, this._triggerAds); }, + @observes("listLoading") waitForLoad: function() { if (this.get("adRequested")) { return; @@ -195,45 +200,46 @@ export default Ember.Component.extend({ if (!this.get("listLoading")) { Ember.run.scheduleOnce("afterRender", this, this._triggerAds); } - }.observes("listLoading"), + }, - isResponsive: function() { - return this.get("ad_width") === "auto"; - }.property("ad_width"), + @computed("ad_width") + isResponsive: function(adWidth) { + return adWidth === "auto"; + }, - classForSlot: function() { - return `adsense-${this.get("placement")}`.htmlSafe(); - }.property("placement"), + @computed("placement", "checkTrustLevels") + classForSlot: function(placement, shown) { + return shown ? `adsense-${placement}`.htmlSafe() : ""; + }, - autoAdFormat: function() { - return this.get("isResponsive") ? "auto".htmlSafe() : false; - }.property("isResponsive"), + @computed("isResponsive") + autoAdFormat: function(isResponsive) { + return isResponsive ? "auto".htmlSafe() : false; + }, - adWrapperStyle: function() { - return (this.get("isResponsive") - ? "" - : `width: ${this.get("ad_width")}; height: ${this.get("ad_height")};` - ).htmlSafe(); - }.property("ad_width", "ad_height"), + @computed("ad_width", "ad_height", "isResponsive") + adWrapperStyle: function(w, h, isResponsive) { + return (isResponsive ? "" : `width: ${w}; height: ${h};`).htmlSafe(); + }, - adInsStyle: function() { + @computed("adWrapperStyle", "isResponsive") + adInsStyle: function(adWrapperStyle, isResponsive) { return `display: ${ - this.get("isResponsive") ? "block" : "inline-block" - }; ${this.get("adWrapperStyle")}`.htmlSafe(); - }.property("adWrapperStyle", "isResponsive"), + isResponsive ? "block" : "inline-block" + }; ${adWrapperStyle}`.htmlSafe(); + }, + @computed() checkTrustLevels: function() { return !( currentUser && currentUser.get("trust_level") > Discourse.SiteSettings.adsense_through_trust_level ); - }.property("trust_level"), + }, - showAd: function() { - return ( - Discourse.SiteSettings.adsense_publisher_code && - this.get("checkTrustLevels") - ); - }.property("checkTrustLevels") + @computed("checkTrustLevels") + showAd: function(shown) { + return shown && Discourse.SiteSettings.adsense_publisher_code; + } }); diff --git a/test/javascripts/acceptance/adsense-test.js.es6 b/test/javascripts/acceptance/adsense-test.js.es6 new file mode 100644 index 0000000..fda87ca --- /dev/null +++ b/test/javascripts/acceptance/adsense-test.js.es6 @@ -0,0 +1,56 @@ +import { acceptance, replaceCurrentUser } from "helpers/qunit-helpers"; + +acceptance("AdSense", { + loggedIn: true, + settings: { + adsense_publisher_code: "MYADSENSEID", + adsense_through_trust_level: 2, + adsense_topic_list_top_code: "list_top_ad_unit", + adsense_topic_list_top_ad_sizes: "728*90 - leaderboard", + adsense_mobile_topic_list_top_code: "mobile_list_top_ad_unit", + adsense_mobile_topic_list_top_ad_size: "300*250 - medium rectangle", + adsense_post_bottom_code: "post_bottom_ad_unit", + adsense_post_bottom_ad_sizes: "728*90 - leaderboard", + adsense_mobile_post_bottom_code: "mobile_post_bottom_ad_unit", + adsense_mobile_post_bottom_ad_size: "300*250 - medium rectangle", + adsense_nth_post_code: 6 + } +}); + +test("correct number of ads should show", async assert => { + replaceCurrentUser({ staff: false, trust_level: 1 }); + await visit("/t/280"); // 20 posts + const ads = find(".google-adsense.adsense-post-bottom"); + assert.equal(ads.length, 3, "it should render 3 ads"); + assert.equal( + find("#post_6 + .widget-connector").find( + ".google-adsense.adsense-post-bottom" + ).length, + 1, + "ad after 6th post" + ); + assert.equal( + find("#post_12 + .widget-connector").find( + ".google-adsense.adsense-post-bottom" + ).length, + 1, + "ad after 12th post" + ); + assert.equal( + find("#post_18 + .widget-connector").find( + ".google-adsense.adsense-post-bottom" + ).length, + 1, + "ad after 18th post" + ); +}); + +test("no ads for trust level 3", async assert => { + replaceCurrentUser({ staff: false, trust_level: 3 }); + await visit("/t/280"); + assert.equal( + find(".google-adsense.adsense-post-bottom").length, + 0, + "it should render 0 ads" + ); +});