18F/web-design-standards

View on GitHub
packages/uswds-core/src/js/utils/test/toggle-field-mask.spec.js

Summary

Maintainability
A
3 hrs
Test Coverage
const assert = require("assert");
const toggleFieldMask = require("../toggle-field-mask");

const createElement = (name, attrs) => {
  const el = document.createElement(name);

  Object.entries(attrs).forEach(([key, value]) => el.setAttribute(key, value));

  return el;
};

describe("toggleFieldMask()", () => {
  it("switches the type of an input from text to password when true", () => {
    const text = createElement("input", {
      type: "text",
      autocapitalize: "off",
      autocorrect: "off",
    });
    toggleFieldMask(text, true);
    assert.strictEqual(text.getAttribute("type"), "password");
  });

  it("switches the type of an input from password to text when false", () => {
    const password = createElement("input", {
      type: "password",
      autocapitalize: "off",
      autocorrect: "off",
    });
    toggleFieldMask(password, false);
    assert.strictEqual(password.getAttribute("type"), "text");
  });
});