Skip to content

Commit

Permalink
Add addClasses function
Browse files Browse the repository at this point in the history
  • Loading branch information
demiazz committed May 7, 2017
1 parent 1bb17ed commit 07a1349
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 1 deletion.
1 change: 1 addition & 0 deletions d.ts/homey.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion spec/css/add-class.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(`<div class="root"></div>`);

const subject = document.querySelector(".root");
Expand Down
61 changes: 61 additions & 0 deletions spec/css/add-classes.spec.js
Original file line number Diff line number Diff line change
@@ -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(`<div class="root foo"></div>`);

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(`<div class="root"></div>`);

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(`<div class="root foo"></div>`);

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(`<div class="root foo bar"></div>`);

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);
});
});
14 changes: 14 additions & 0 deletions src/css/add-classes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* @flow */

import type { CSSClass } from "./types";

import addClass from "./add-class";

function addClasses(element: Element, ...cssClasses: Array<CSSClass>) {
return cssClasses.reduce(
(result, cssClass) => addClass(element, cssClass) || result,
false
);
}

export default addClasses;
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
body,
html,
addClass,
addClasses,
hasClass,
removeClass,
toggleClass,
Expand Down

0 comments on commit 07a1349

Please sign in to comment.