From 07a1349a2b62cb17716c2fe05720c0c98ac36090 Mon Sep 17 00:00:00 2001 From: Alexey Plutalov Date: Mon, 8 May 2017 00:12:46 +0300 Subject: [PATCH] Add `addClasses` function --- d.ts/homey.d.ts | 1 + spec/css/add-class.spec.js | 2 +- spec/css/add-classes.spec.js | 61 ++++++++++++++++++++++++++++++++++++ src/css/add-classes.js | 14 +++++++++ src/index.js | 2 ++ 5 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 spec/css/add-classes.spec.js create mode 100644 src/css/add-classes.js diff --git a/d.ts/homey.d.ts b/d.ts/homey.d.ts index 0d6c02e..cf8f4a5 100644 --- a/d.ts/homey.d.ts +++ b/d.ts/homey.d.ts @@ -17,6 +17,7 @@ export const html: Element; /* CSS */ export function addClass(element: Element, cssClass: CSSClass): boolean; +export function addClass(element: Element, ...cssClasses: CSSClass[]): boolean; export function hasClass(element: Element, cssClass: CSSClass): boolean; export function removeClass(element: Element, cssClass: CSSClass): boolean; export function toggleClass(element: Element, cssClass: CSSClass, state?: boolean): boolean; diff --git a/spec/css/add-class.spec.js b/spec/css/add-class.spec.js index 31ebce9..af16cc2 100644 --- a/spec/css/add-class.spec.js +++ b/spec/css/add-class.spec.js @@ -3,7 +3,7 @@ import { addClass } from "../../src"; describe("addClass", () => { afterEach(clearFixtures); - it("adds given class to element if element has't given class already", () => { + it("adds given class to element if element hasn't given class already", () => { useFixture(`
`); const subject = document.querySelector(".root"); diff --git a/spec/css/add-classes.spec.js b/spec/css/add-classes.spec.js new file mode 100644 index 0000000..4c17a00 --- /dev/null +++ b/spec/css/add-classes.spec.js @@ -0,0 +1,61 @@ +import { addClasses } from "../../src"; + +describe("addClasses", () => { + afterEach(clearFixtures); + + it("adds classes from given list which not added to element early", () => { + useFixture(`
`); + + const subject = document.querySelector(".root"); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(false); + + addClasses(subject, "foo", "bar"); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(true); + }); + + it("returns `true` if all given classes has been added to element", () => { + useFixture(`
`); + + const subject = document.querySelector(".root"); + + expect(subject.classList.contains("foo")).toBe(false); + expect(subject.classList.contains("bar")).toBe(false); + + expect(addClasses(subject, "foo", "bar")).toBe(true); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(true); + }); + + it("returns `true` if any given class has been added to element", () => { + useFixture(`
`); + + const subject = document.querySelector(".root"); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(false); + + expect(addClasses(subject, "foo", "bar")).toBe(true); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(true); + }); + + it("returns `true` if any given class has been added to element", () => { + useFixture(`
`); + + const subject = document.querySelector(".root"); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(true); + + expect(addClasses(subject, "foo", "bar")).toBe(false); + + expect(subject.classList.contains("foo")).toBe(true); + expect(subject.classList.contains("bar")).toBe(true); + }); +}); diff --git a/src/css/add-classes.js b/src/css/add-classes.js new file mode 100644 index 0000000..bc1e351 --- /dev/null +++ b/src/css/add-classes.js @@ -0,0 +1,14 @@ +/* @flow */ + +import type { CSSClass } from "./types"; + +import addClass from "./add-class"; + +function addClasses(element: Element, ...cssClasses: Array) { + return cssClasses.reduce( + (result, cssClass) => addClass(element, cssClass) || result, + false + ); +} + +export default addClasses; diff --git a/src/index.js b/src/index.js index 401547e..d1ed691 100644 --- a/src/index.js +++ b/src/index.js @@ -13,6 +13,7 @@ import type { CSSSelector, Elements } from "./types"; import body from "./aliases/body"; import html from "./aliases/html"; import addClass from "./css/add-class"; +import addClasses from "./css/add-classes"; import hasClass from "./css/has-class"; import removeClass from "./css/remove-class"; import toggleClass from "./css/toggle-class"; @@ -46,6 +47,7 @@ export { body, html, addClass, + addClasses, hasClass, removeClass, toggleClass,