test_NewTabAttributionFeed.js (5142B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 ChromeUtils.defineESModuleGetters(this, { 7 actionTypes: "resource://newtab/common/Actions.mjs", 8 NewTabAttributionFeed: "resource://newtab/lib/NewTabAttributionFeed.sys.mjs", 9 newTabAttributionService: 10 "resource://newtab/lib/NewTabAttributionService.sys.mjs", 11 sinon: "resource://testing-common/Sinon.sys.mjs", 12 }); 13 14 function makeStore(state) { 15 return ( 16 state || { 17 Prefs: { 18 values: { 19 "discoverystream.attribution.enabled": true, 20 trainhopConfig: { attribution: { enabled: true } }, 21 "unifiedAds.adsFeed.enabled": true, 22 "unifiedAds.spocs.enabled": true, 23 "unifiedAds.tiles.enabled": true, 24 "feeds.topsites": true, 25 "feeds.system.topsites": true, 26 "feeds.section.topstories": true, 27 "feeds.system.topstories": true, 28 "system.showSponsored": true, 29 showSponsored: true, 30 showSponsoredTopSites: false, 31 }, 32 }, 33 } 34 ); 35 } 36 37 add_task(async function test_init() { 38 const feed = new NewTabAttributionFeed(); 39 feed.store = { 40 getState() { 41 return this.state; 42 }, 43 state: makeStore(), 44 }; 45 46 Assert.ok(!feed.loaded); 47 48 await feed.onAction({ type: actionTypes.INIT }); 49 Assert.ok(feed.loaded); 50 Assert.ok(feed.isEnabled()); 51 52 await feed.onAction({ type: actionTypes.UNINIT }); 53 Assert.ok(!feed.loaded); 54 }); 55 56 add_task(async function test_events_when_enabled() { 57 const feed = new NewTabAttributionFeed(); 58 feed.store = { 59 getState() { 60 return this.state; 61 }, 62 state: makeStore({ 63 Prefs: { 64 values: { 65 "discoverystream.attribution.enabled": true, 66 trainhopConfig: { attribution: { enabled: true } }, 67 68 "unifiedAds.adsFeed.enabled": true, 69 "unifiedAds.spocs.enabled": true, 70 "unifiedAds.tiles.enabled": true, 71 "feeds.topsites": true, 72 "feeds.system.topsites": true, 73 "feeds.section.topstories": true, 74 "feeds.system.topstories": true, 75 "system.showSponsored": true, 76 showSponsored: true, 77 showSponsoredTopSites: true, 78 }, 79 }, 80 }), 81 }; 82 83 await feed.onAction({ type: actionTypes.INIT }); 84 Assert.ok(feed.loaded); 85 86 const onAttributionEvent = sinon 87 .stub(newTabAttributionService, "onAttributionEvent") 88 .resolves(); 89 const onReset = sinon.stub(newTabAttributionService, "onAttributionReset"); 90 91 const attribution = { campaignId: "bar", creativeId: "bar" }; 92 93 // top sites impression 94 await feed.onAction({ 95 type: actionTypes.TOP_SITES_SPONSORED_IMPRESSION_STATS, 96 data: { type: "impression", attribution }, 97 }); 98 Assert.equal(onAttributionEvent.callCount, 1); 99 Assert.deepEqual(onAttributionEvent.firstCall.args, ["view", attribution]); 100 101 // Top Sites click 102 await feed.onAction({ 103 type: actionTypes.TOP_SITES_SPONSORED_IMPRESSION_STATS, 104 data: { type: "click", attribution }, 105 }); 106 107 Assert.equal(onAttributionEvent.callCount, 2); 108 Assert.deepEqual(onAttributionEvent.secondCall.args, ["click", attribution]); 109 110 // DS impression 111 await feed.onAction({ 112 type: actionTypes.DISCOVERY_STREAM_IMPRESSION_STATS, 113 data: { tiles: [{ attribution }] }, 114 }); 115 Assert.equal(onAttributionEvent.callCount, 3); 116 Assert.deepEqual(onAttributionEvent.thirdCall.args, ["view", attribution]); 117 118 // DS click event 119 await feed.onAction({ 120 type: actionTypes.DISCOVERY_STREAM_USER_EVENT, 121 data: { value: { attribution } }, 122 }); 123 124 Assert.equal(onAttributionEvent.callCount, 4); 125 Assert.deepEqual(onAttributionEvent.lastCall.args, ["click", attribution]); 126 127 // History cleared 128 await feed.onAction({ type: actionTypes.PLACES_HISTORY_CLEARED }); 129 Assert.ok(onReset.calledOnce); 130 131 onAttributionEvent.restore(); 132 onReset.restore(); 133 }); 134 135 add_task(async function test_pref_changed_trigger_init() { 136 const feed = new NewTabAttributionFeed(); 137 feed.store = { 138 getState() { 139 return this.state; 140 }, 141 state: makeStore({ 142 Prefs: { 143 values: { 144 "discoverystream.attribution.enabled": false, 145 trainhopConfig: { attribution: { enabled: false } }, 146 147 "unifiedAds.adsFeed.enabled": true, 148 "unifiedAds.spocs.enabled": true, 149 "unifiedAds.tiles.enabled": true, 150 "feeds.topsites": true, 151 "feeds.system.topsites": true, 152 "feeds.section.topstories": true, 153 "feeds.system.topstories": true, 154 "system.showSponsored": true, 155 showSponsored: true, 156 showSponsoredTopSites: false, 157 }, 158 }, 159 }), 160 }; 161 162 await feed.onAction({ type: actionTypes.INIT }); 163 Assert.ok(!feed.loaded); 164 165 // need both to trigger the PREF_CHANGED action and change the value of the pref in the store. 166 feed.store.state.Prefs.values["discoverystream.attribution.enabled"] = true; 167 await feed.onAction({ 168 type: actionTypes.PREF_CHANGED, 169 data: { name: "discoverystream.attribution.enabled", value: true }, 170 }); 171 Assert.ok(feed.isEnabled()); 172 173 Assert.ok(feed.loaded); 174 });