Skip to content

Commit

Permalink
feat(component): adds "between" matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
dw2 committed Oct 3, 2019
1 parent 0ba4878 commit 0ebbdd7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/matchers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,11 @@ export const under = (prop: string, amount: number) => (
str: any,
...args: any[]
) => (props: object) => (props[prop] < amount ? css(str, ...args) : "");

export const between = (prop: string, from: number, to: number) => (
str: any,
...args: any[]
) => (props: object) => {
const value = props[prop];
return value > from && value < to ? css(str, ...args) : "";
};
17 changes: 17 additions & 0 deletions src/styled-tidy.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
choose,
over,
under,
between,
minWidth,
maxWidth,
minHeight,
Expand Down Expand Up @@ -236,6 +237,22 @@ describe("styed-tidy", () => {
});
});

describe("'between' matcher", () => {
const Test = styled.div<TestProps>`
${between("amount", 0, 1)`color: green`};
`;

it("should render the given CSS when the prop is between the given range", () => {
const { getByText } = setup(<Test amount={0.5}>test</Test>);
expect(getByText("test")).toHaveStyleRule("color", "green");
});

it("should not render the given CSS when the prop is outside the given range", () => {
const { getByText } = setup(<Test amount={0}>test</Test>);
expect(getByText("test")).not.toHaveStyleRule("color");
});
});

describe("'minWidth' media query", () => {
it("renders a min-width media query with the given CSS", () => {
const Test = styled.div<TestProps>`
Expand Down
3 changes: 2 additions & 1 deletion src/styled-tidy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export {
swap,
choose,
over,
under
under,
between
} from "./matchers";
export { minWidth, maxWidth, minHeight, maxHeight } from "./media";
export { rem, opacify, transparentize, flex, grid, position } from "./mixins";

0 comments on commit 0ebbdd7

Please sign in to comment.