Bug 1881588 - Add Wallpaper component r=home-newtab-reviewers,fluent-reviewers,bolsso...
[gecko.git] / browser / components / newtab / test / xpcshell / test_WallpaperFeed.js
blob48b39243c7a94c3a4c63ff52f29154e26577f4a6
1 /* Any copyright is dedicated to the Public Domain.
2    http://creativecommons.org/publicdomain/zero/1.0/ */
4 "use strict";
6 const { WallpaperFeed } = ChromeUtils.importESModule(
7   "resource://activity-stream/lib/WallpaperFeed.sys.mjs"
8 );
10 const { actionCreators: ac, actionTypes: at } = ChromeUtils.importESModule(
11   "resource://activity-stream/common/Actions.mjs"
14 ChromeUtils.defineESModuleGetters(this, {
15   Utils: "resource://services-settings/Utils.sys.mjs",
16   sinon: "resource://testing-common/Sinon.sys.mjs",
17 });
19 const PREF_WALLPAPERS_ENABLED =
20   "browser.newtabpage.activity-stream.newtabWallpapers.enabled";
22 add_task(async function test_construction() {
23   let feed = new WallpaperFeed();
25   info("WallpaperFeed constructor should create initial values");
27   Assert.ok(feed, "Could construct a WallpaperFeed");
28   Assert.ok(feed.loaded === false, "WallpaperFeed is not loaded");
29   Assert.ok(
30     feed.wallpaperClient === "",
31     "wallpaperClient is initialized as an empty string"
32   );
33   Assert.ok(
34     feed.wallpaperDB === "",
35     "wallpaperDB is initialized as an empty string"
36   );
37   Assert.ok(
38     feed.baseAttachmentURL === "",
39     "baseAttachmentURL is initialized as an empty string"
40   );
41 });
43 add_task(async function test_onAction_INIT() {
44   let sandbox = sinon.createSandbox();
45   let feed = new WallpaperFeed();
46   Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
47   const attachment = {
48     attachment: {
49       location: "attachment",
50     },
51   };
52   sandbox.stub(feed, "RemoteSettings").returns({
53     get: () => [attachment],
54     on: () => {},
55   });
56   sandbox.stub(Utils, "SERVER_URL").returns("http://localhost:8888/v1");
57   feed.store = {
58     dispatch: sinon.spy(),
59   };
60   sandbox.stub(feed, "fetch").resolves({
61     json: () => ({
62       capabilities: {
63         attachments: {
64           base_url: "http://localhost:8888/base_url/",
65         },
66       },
67     }),
68   });
70   info("WallpaperFeed.onAction INIT should initialize wallpapers");
72   await feed.onAction({
73     type: at.INIT,
74   });
76   Assert.ok(feed.store.dispatch.calledOnce);
77   Assert.ok(
78     feed.store.dispatch.calledWith(
79       ac.BroadcastToContent({
80         type: at.WALLPAPERS_SET,
81         data: [
82           {
83             ...attachment,
84             wallpaperUrl: "http://localhost:8888/base_url/attachment",
85           },
86         ],
87       })
88     )
89   );
90   Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
91   sandbox.restore();
92 });
94 add_task(async function test_onAction_PREF_CHANGED() {
95   let sandbox = sinon.createSandbox();
96   let feed = new WallpaperFeed();
97   Services.prefs.setBoolPref(PREF_WALLPAPERS_ENABLED, true);
98   sandbox.stub(feed, "wallpaperSetup").returns();
100   info("WallpaperFeed.onAction PREF_CHANGED should call wallpaperSetup");
102   feed.onAction({
103     type: at.PREF_CHANGED,
104     data: { name: "newtabWallpapers.enabled" },
105   });
107   Assert.ok(feed.wallpaperSetup.calledOnce);
109   Services.prefs.clearUserPref(PREF_WALLPAPERS_ENABLED);
110   sandbox.restore();