tor-browser

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

accessibility-prefs.test.js (3395B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 "use strict";
      5 
      6 const { mount } = require("enzyme");
      7 
      8 const {
      9  createFactory,
     10 } = require("resource://devtools/client/shared/vendor/react.mjs");
     11 const Provider = createFactory(
     12  require("resource://devtools/client/shared/vendor/react-redux.js").Provider
     13 );
     14 
     15 const MenuButton = require("resource://devtools/client/shared/components/menu/MenuButton.js");
     16 const ConnectedAccessibilityPrefsClass = require("resource://devtools/client/accessibility/components/AccessibilityPrefs.js");
     17 const AccessibilityPrefsClass =
     18  ConnectedAccessibilityPrefsClass.WrappedComponent;
     19 const AccessibilityPrefs = createFactory(ConnectedAccessibilityPrefsClass);
     20 const {
     21  checkMenuItem,
     22  setupStore,
     23 } = require("resource://devtools/client/accessibility/test/node/helpers.js");
     24 
     25 const {
     26  PREFS,
     27 } = require("resource://devtools/client/accessibility/constants.js");
     28 
     29 function checkTogglePrefCheckbox(wrapper, pref) {
     30  const prefsInstance = wrapper.find(AccessibilityPrefsClass).instance();
     31  prefsInstance.togglePref = jest.fn();
     32  pref instanceof Node ? pref.click() : pref.simulate("click");
     33  expect(prefsInstance.togglePref.mock.calls.length).toBe(1);
     34 }
     35 
     36 function getMenuItems(wrapper, selector) {
     37  const menuButton = wrapper.find(MenuButton);
     38  // Focusing on the menu button will trigger rendering of the HTMLTooltip with
     39  // the menu list.
     40  menuButton.childAt(0).getDOMNode().focus();
     41 
     42  return menuButton
     43    .instance()
     44    .tooltip.panel.querySelectorAll(`.menuitem ${selector}`);
     45 }
     46 
     47 function checkPrefsState(wrapper, expected) {
     48  const prefs = getMenuItems(wrapper, ".pref");
     49  for (let i = 0; i < prefs.length; i++) {
     50    checkMenuItem(prefs[i], {
     51      checked: expected.prefs[i].active,
     52      label: expected.prefs[i].text,
     53    });
     54  }
     55  checkMenuItem(getMenuItems(wrapper, ".help")[0], {
     56    role: "link",
     57    label: "Documentation…",
     58  });
     59 }
     60 
     61 describe("AccessibilityPrefs component:", () => {
     62  it("prefs not set by default", () => {
     63    const store = setupStore();
     64    const wrapper = mount(
     65      Provider({ store }, AccessibilityPrefs({ toolboxDoc: document }))
     66    );
     67    const accPrefs = wrapper.find(AccessibilityPrefsClass);
     68    const menuButton = accPrefs.childAt(0);
     69 
     70    expect(wrapper.html()).toMatchSnapshot();
     71    expect(accPrefs.children().length).toBe(1);
     72    expect(menuButton.is(MenuButton)).toBe(true);
     73 
     74    checkPrefsState(wrapper, {
     75      prefs: [{ active: false, text: "Scroll into view" }],
     76    });
     77  });
     78 
     79  it("prefs checked", () => {
     80    const store = setupStore({
     81      preloadedState: {
     82        ui: {
     83          [PREFS.SCROLL_INTO_VIEW]: true,
     84        },
     85      },
     86    });
     87    const wrapper = mount(
     88      Provider({ store }, AccessibilityPrefs({ toolboxDoc: document }))
     89    );
     90    expect(wrapper.html()).toMatchSnapshot();
     91    checkPrefsState(wrapper, {
     92      prefs: [{ active: true, text: "Scroll into view" }],
     93    });
     94  });
     95 
     96  it("toggle pref", () => {
     97    const store = setupStore();
     98    const wrapper = mount(
     99      Provider({ store }, AccessibilityPrefs({ toolboxDoc: document }))
    100    );
    101 
    102    expect(wrapper.html()).toMatchSnapshot();
    103    for (const pref of getMenuItems(wrapper, ".pref")) {
    104      checkTogglePrefCheckbox(wrapper, pref);
    105    }
    106  });
    107 });