Skip to content

Commit 0ba4878

Browse files
committed
feat(component): adds optional second param to "is" and "isnt" matchers
1 parent 36c9b2b commit 0ba4878

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

src/matchers.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
import { css } from "styled-components";
44

5-
export const is = (prop: string) => (str: any, ...args: any[]) => (
6-
props: object
7-
) => (props[prop] ? css(str, ...args) : "");
5+
export const is = (prop: string, value: any = null) => (
6+
str: any,
7+
...args: any[]
8+
) => (props: object) => {
9+
const match = value === null ? !!props[prop] : props[prop] === value;
10+
return match ? css(str, ...args) : "";
11+
};
812

9-
export const isnt = (prop: string) => (str: any, ...args: any[]) => (
10-
props: object
11-
) => (!props[prop] ? css(str, ...args) : "");
13+
export const isnt = (prop: string, value: any = null) => (
14+
str: any,
15+
...args: any[]
16+
) => (props: object) => {
17+
const match = value === null ? !props[prop] : props[prop] !== value;
18+
return match ? css(str, ...args) : "";
19+
};
1220

1321
export const isAny = (prop: string, matches: any[]) => (
1422
str: any,

src/styled-tidy.test.tsx

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,39 @@ describe("styed-tidy", () => {
3939
afterEach(cleanup);
4040

4141
describe("'is' matcher", () => {
42-
const Test = styled.div<TestProps>`
43-
${is("enabled")`color: green`};
44-
`;
45-
4642
it("should render the given CSS when matched", () => {
43+
const Test = styled.div<TestProps>`
44+
${is("enabled")`color: green`};
45+
`;
4746
const { getByText } = setup(<Test enabled>test</Test>);
4847
expect(getByText("test")).toHaveStyleRule("color", "green");
4948
});
5049

50+
it("should render the given CSS when given value is matched", () => {
51+
const Test = styled.div<TestProps>`
52+
${is("size", "big")`color: green`};
53+
`;
54+
const { getByText } = setup(<Test size="big">test</Test>);
55+
expect(getByText("test")).toHaveStyleRule("color", "green");
56+
});
57+
5158
it("should not render the given CSS when not matched", () => {
59+
const Test = styled.div<TestProps>`
60+
${is("enabled")`color: green`};
61+
`;
5262
const { getByText } = setup(<Test>test</Test>);
5363
expect(getByText("test")).not.toHaveStyleRule("color");
5464
});
55-
});
5665

57-
describe("'is' matcher with a mixin", () => {
58-
it("should render the given CSS when matched", () => {
66+
it("should not render the given CSS when given value is not matched", () => {
67+
const Test = styled.div<TestProps>`
68+
${is("size", "big")`color: green`};
69+
`;
70+
const { getByText } = setup(<Test size="small">test</Test>);
71+
expect(getByText("test")).not.toHaveStyleRule("color");
72+
});
73+
74+
it("should render the given CSS when matched when a mixin is included", () => {
5975
const Test = styled.div<TestProps>`
6076
${is("enabled")`height: ${rem(16)}`};
6177
`;
@@ -65,19 +81,37 @@ describe("styed-tidy", () => {
6581
});
6682

6783
describe("'isnt' matcher", () => {
68-
const Test = styled.div<TestProps>`
69-
${isnt("enabled")`color: red`};
70-
`;
71-
7284
it("should not render the given CSS when matched", () => {
85+
const Test = styled.div<TestProps>`
86+
${isnt("enabled")`color: red`};
87+
`;
7388
const { getByText } = setup(<Test enabled>test</Test>);
7489
expect(getByText("test")).not.toHaveStyleRule("color");
7590
});
7691

7792
it("should render the given CSS when not matched", () => {
93+
const Test = styled.div<TestProps>`
94+
${isnt("enabled")`color: red`};
95+
`;
7896
const { getByText } = setup(<Test>test</Test>);
7997
expect(getByText("test")).toHaveStyleRule("color", "red");
8098
});
99+
100+
it("should not render the given CSS when given value is matched", () => {
101+
const Test = styled.div<TestProps>`
102+
${isnt("size", "small")`color: red`};
103+
`;
104+
const { getByText } = setup(<Test size="small">test</Test>);
105+
expect(getByText("test")).not.toHaveStyleRule("color");
106+
});
107+
108+
it("should render the given CSS when given value is not matched", () => {
109+
const Test = styled.div<TestProps>`
110+
${isnt("size", "small")`color: red`};
111+
`;
112+
const { getByText } = setup(<Test size="big">test</Test>);
113+
expect(getByText("test")).toHaveStyleRule("color", "red");
114+
});
81115
});
82116

83117
describe("'isAny' matcher", () => {

0 commit comments

Comments
 (0)