From 0ba4878abeb23fc739b28b077f809cfc024337aa Mon Sep 17 00:00:00 2001 From: Douglas Waltman II Date: Wed, 2 Oct 2019 08:43:05 -0700 Subject: [PATCH] feat(component): adds optional second param to "is" and "isnt" matchers --- src/matchers.tsx | 20 +++++++++----- src/styled-tidy.test.tsx | 56 ++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/matchers.tsx b/src/matchers.tsx index 507e340..ce9827d 100644 --- a/src/matchers.tsx +++ b/src/matchers.tsx @@ -2,13 +2,21 @@ import { css } from "styled-components"; -export const is = (prop: string) => (str: any, ...args: any[]) => ( - props: object -) => (props[prop] ? css(str, ...args) : ""); +export const is = (prop: string, value: any = null) => ( + str: any, + ...args: any[] +) => (props: object) => { + const match = value === null ? !!props[prop] : props[prop] === value; + return match ? css(str, ...args) : ""; +}; -export const isnt = (prop: string) => (str: any, ...args: any[]) => ( - props: object -) => (!props[prop] ? css(str, ...args) : ""); +export const isnt = (prop: string, value: any = null) => ( + str: any, + ...args: any[] +) => (props: object) => { + const match = value === null ? !props[prop] : props[prop] !== value; + return match ? css(str, ...args) : ""; +}; export const isAny = (prop: string, matches: any[]) => ( str: any, diff --git a/src/styled-tidy.test.tsx b/src/styled-tidy.test.tsx index 4cdecce..84ce338 100644 --- a/src/styled-tidy.test.tsx +++ b/src/styled-tidy.test.tsx @@ -39,23 +39,39 @@ describe("styed-tidy", () => { afterEach(cleanup); describe("'is' matcher", () => { - const Test = styled.div` - ${is("enabled")`color: green`}; - `; - it("should render the given CSS when matched", () => { + const Test = styled.div` + ${is("enabled")`color: green`}; + `; const { getByText } = setup(test); expect(getByText("test")).toHaveStyleRule("color", "green"); }); + it("should render the given CSS when given value is matched", () => { + const Test = styled.div` + ${is("size", "big")`color: green`}; + `; + const { getByText } = setup(test); + expect(getByText("test")).toHaveStyleRule("color", "green"); + }); + it("should not render the given CSS when not matched", () => { + const Test = styled.div` + ${is("enabled")`color: green`}; + `; const { getByText } = setup(test); expect(getByText("test")).not.toHaveStyleRule("color"); }); - }); - describe("'is' matcher with a mixin", () => { - it("should render the given CSS when matched", () => { + it("should not render the given CSS when given value is not matched", () => { + const Test = styled.div` + ${is("size", "big")`color: green`}; + `; + const { getByText } = setup(test); + expect(getByText("test")).not.toHaveStyleRule("color"); + }); + + it("should render the given CSS when matched when a mixin is included", () => { const Test = styled.div` ${is("enabled")`height: ${rem(16)}`}; `; @@ -65,19 +81,37 @@ describe("styed-tidy", () => { }); describe("'isnt' matcher", () => { - const Test = styled.div` - ${isnt("enabled")`color: red`}; - `; - it("should not render the given CSS when matched", () => { + const Test = styled.div` + ${isnt("enabled")`color: red`}; + `; const { getByText } = setup(test); expect(getByText("test")).not.toHaveStyleRule("color"); }); it("should render the given CSS when not matched", () => { + const Test = styled.div` + ${isnt("enabled")`color: red`}; + `; const { getByText } = setup(test); expect(getByText("test")).toHaveStyleRule("color", "red"); }); + + it("should not render the given CSS when given value is matched", () => { + const Test = styled.div` + ${isnt("size", "small")`color: red`}; + `; + const { getByText } = setup(test); + expect(getByText("test")).not.toHaveStyleRule("color"); + }); + + it("should render the given CSS when given value is not matched", () => { + const Test = styled.div` + ${isnt("size", "small")`color: red`}; + `; + const { getByText } = setup(test); + expect(getByText("test")).toHaveStyleRule("color", "red"); + }); }); describe("'isAny' matcher", () => {