tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DiscoveryStreamAdmin.test.jsx (7856B)


      1 import { actionCreators as ac, actionTypes as at } from "common/Actions.mjs";
      2 import {
      3  DiscoveryStreamAdminInner,
      4  DiscoveryStreamAdminUI,
      5  Personalization,
      6  ToggleStoryButton,
      7 } from "content-src/components/DiscoveryStreamAdmin/DiscoveryStreamAdmin";
      8 import React from "react";
      9 import { shallow } from "enzyme";
     10 
     11 describe("DiscoveryStreamAdmin", () => {
     12  let sandbox;
     13  let wrapper;
     14 
     15  beforeEach(() => {
     16    sandbox = sinon.createSandbox();
     17    wrapper = shallow(
     18      <DiscoveryStreamAdminInner
     19        collapsed={false}
     20        location={{ routes: [""] }}
     21        Prefs={{}}
     22      />
     23    );
     24  });
     25  afterEach(() => {
     26    sandbox.restore();
     27  });
     28  it("should render DiscoveryStreamAdmin component", () => {
     29    assert.ok(wrapper.exists());
     30  });
     31  it("should set a .collapsed class on the outer div if props.collapsed is true", () => {
     32    wrapper.setProps({ collapsed: true });
     33    assert.isTrue(wrapper.find(".discoverystream-admin").hasClass("collapsed"));
     34  });
     35  it("should set a .expanded class on the outer div if props.collapsed is false", () => {
     36    wrapper.setProps({ collapsed: false });
     37    assert.isTrue(wrapper.find(".discoverystream-admin").hasClass("expanded"));
     38    assert.isFalse(
     39      wrapper.find(".discoverystream-admin").hasClass("collapsed")
     40    );
     41  });
     42  it("should render a DS section", () => {
     43    assert.equal(wrapper.find("h1").at(0).text(), "Discovery Stream Admin");
     44  });
     45 
     46  describe("#DiscoveryStream", () => {
     47    let state = {};
     48    let dispatch;
     49    beforeEach(() => {
     50      dispatch = sandbox.stub();
     51      state = {
     52        config: {
     53          enabled: true,
     54        },
     55        layout: [],
     56        spocs: {
     57          frequency_caps: [],
     58        },
     59        feeds: {
     60          data: {},
     61        },
     62        blocks: {},
     63        impressions: {
     64          feed: {},
     65        },
     66      };
     67      wrapper = shallow(
     68        <DiscoveryStreamAdminUI
     69          dispatch={dispatch}
     70          otherPrefs={{
     71            "discoverystream.contextualContent.selectedFeed": "foo",
     72            "discoverystream.contextualContent.feeds": "foo, bar",
     73          }}
     74          state={{
     75            DiscoveryStream: state,
     76            Weather: {
     77              suggestions: [],
     78            },
     79            InferredPersonalization: {
     80              inferredInterests: {},
     81              coarseInferredInterests: {},
     82              coarsePrivateInferredInterests: {},
     83            },
     84          }}
     85        />
     86      );
     87    });
     88    it("should render a DiscoveryStreamAdminUI component", () => {
     89      assert.equal(wrapper.find("h3").at(0).text(), "Layout");
     90    });
     91    it("should render a spoc in DiscoveryStreamAdminUI component", () => {
     92      state.spocs = {
     93        frequency_caps: [],
     94        data: {
     95          newtab_spocs: {
     96            items: [
     97              {
     98                id: 12345,
     99              },
    100            ],
    101          },
    102        },
    103      };
    104      wrapper = shallow(
    105        <DiscoveryStreamAdminUI
    106          otherPrefs={{
    107            "discoverystream.contextualContent.selectedFeed": "foo",
    108            "discoverystream.contextualContent.feeds": "foo, bar",
    109          }}
    110          state={{
    111            DiscoveryStream: state,
    112            Weather: {
    113              suggestions: [],
    114            },
    115            InferredPersonalization: {
    116              inferredInterests: {},
    117              coarseInferredInterests: {},
    118              coarsePrivateInferredInterests: {},
    119            },
    120          }}
    121        />
    122      );
    123      wrapper.instance().onStoryToggle({ id: 12345 });
    124      const messageSummary = wrapper.find(".message-summary").at(0);
    125      const pre = messageSummary.find("pre").at(0);
    126      const spocText = pre.text();
    127      assert.equal(spocText, '{\n  "id": 12345\n}');
    128    });
    129    it("should fire restorePrefDefaults with DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS", () => {
    130      wrapper.find("button").at(0).simulate("click");
    131      assert.calledWith(
    132        dispatch,
    133        ac.OnlyToMain({
    134          type: at.DISCOVERY_STREAM_CONFIG_RESET_DEFAULTS,
    135        })
    136      );
    137    });
    138    it("should fire config change with DISCOVERY_STREAM_CONFIG_CHANGE", () => {
    139      wrapper.find("button").at(1).simulate("click");
    140      assert.calledWith(
    141        dispatch,
    142        ac.OnlyToMain({
    143          type: at.DISCOVERY_STREAM_CONFIG_CHANGE,
    144          data: { enabled: true },
    145        })
    146      );
    147    });
    148    it("should fire expireCache with DISCOVERY_STREAM_DEV_EXPIRE_CACHE", () => {
    149      wrapper.find("button").at(2).simulate("click");
    150      assert.calledWith(
    151        dispatch,
    152        ac.OnlyToMain({
    153          type: at.DISCOVERY_STREAM_DEV_EXPIRE_CACHE,
    154        })
    155      );
    156    });
    157    it("should fire systemTick with DISCOVERY_STREAM_DEV_SYSTEM_TICK", () => {
    158      wrapper.find("button").at(3).simulate("click");
    159      assert.calledWith(
    160        dispatch,
    161        ac.OnlyToMain({
    162          type: at.DISCOVERY_STREAM_DEV_SYSTEM_TICK,
    163        })
    164      );
    165    });
    166    it("should fire idleDaily with DISCOVERY_STREAM_DEV_IDLE_DAILY", () => {
    167      wrapper.find("button").at(4).simulate("click");
    168      assert.calledWith(
    169        dispatch,
    170        ac.OnlyToMain({
    171          type: at.DISCOVERY_STREAM_DEV_IDLE_DAILY,
    172        })
    173      );
    174    });
    175    it("should fire syncRemoteSettings with DISCOVERY_STREAM_DEV_SYNC_RS", () => {
    176      wrapper.find("button").at(6).simulate("click");
    177      assert.calledWith(
    178        dispatch,
    179        ac.OnlyToMain({
    180          type: at.DISCOVERY_STREAM_DEV_SYNC_RS,
    181        })
    182      );
    183    });
    184    it("should fire setConfigValue with DISCOVERY_STREAM_CONFIG_SET_VALUE", () => {
    185      const configName = "name";
    186      const configValue = "value";
    187      wrapper.instance().setConfigValue(configName, configValue);
    188      assert.calledWith(
    189        dispatch,
    190        ac.OnlyToMain({
    191          type: at.DISCOVERY_STREAM_CONFIG_SET_VALUE,
    192          data: { name: configName, value: configValue },
    193        })
    194      );
    195    });
    196  });
    197 
    198  describe("#Personalization", () => {
    199    let dispatch;
    200    beforeEach(() => {
    201      dispatch = sandbox.stub();
    202      wrapper = shallow(
    203        <Personalization
    204          dispatch={dispatch}
    205          state={{
    206            Personalization: {
    207              lastUpdated: 1000,
    208              initialized: true,
    209            },
    210          }}
    211        />
    212      );
    213    });
    214    it("should render with pref checkbox, lastUpdated, and initialized", () => {
    215      assert.lengthOf(wrapper.find("TogglePrefCheckbox"), 1);
    216      assert.equal(
    217        wrapper.find("td").at(1).text(),
    218        "Personalization Last Updated"
    219      );
    220      assert.equal(
    221        wrapper.find("td").at(2).text(),
    222        new Date(1000).toLocaleString()
    223      );
    224      assert.equal(
    225        wrapper.find("td").at(3).text(),
    226        "Personalization Initialized"
    227      );
    228      assert.equal(wrapper.find("td").at(4).text(), "true");
    229    });
    230    it("should render with no data with no last updated", () => {
    231      wrapper = shallow(
    232        <Personalization
    233          dispatch={dispatch}
    234          state={{
    235            Personalization: {
    236              version: 2,
    237              lastUpdated: 0,
    238              initialized: true,
    239            },
    240          }}
    241        />
    242      );
    243      assert.equal(wrapper.find("td").at(2).text(), "(no data)");
    244    });
    245    it("should dispatch DISCOVERY_STREAM_PERSONALIZATION_TOGGLE", () => {
    246      wrapper.instance().togglePersonalization();
    247      assert.calledWith(
    248        dispatch,
    249        ac.OnlyToMain({
    250          type: at.DISCOVERY_STREAM_PERSONALIZATION_TOGGLE,
    251        })
    252      );
    253    });
    254  });
    255 
    256  describe("#ToggleStoryButton", () => {
    257    it("should fire onClick in toggle button", async () => {
    258      let result = "";
    259      function onClick(spoc) {
    260        result = spoc;
    261      }
    262 
    263      wrapper = shallow(<ToggleStoryButton story="spoc" onClick={onClick} />);
    264      wrapper.find("button").simulate("click");
    265 
    266      assert.equal(result, "spoc");
    267    });
    268  });
    269 });