Skip to content

Commit

Permalink
Refactor to use shared spec: RiseLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhu2000 committed Feb 22, 2021
1 parent 9624740 commit ff18bf6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 138 deletions.
171 changes: 34 additions & 137 deletions __tests__/RiseLoader-tests.tsx
Original file line number Diff line number Diff line change
@@ -1,63 +1,50 @@
import * as React from "react";
import { mount, ReactWrapper } from "enzyme";
import { matchers } from '@emotion/jest';
import { matchers } from "@emotion/jest";
expect.extend(matchers);

import RiseLoader from "../src/RiseLoader";
import { LoaderSizeMarginProps } from "../src/interfaces";
import { sizeMarginDefaults } from "../src/helpers";
import { commonSpecs, cssSpecs, lengthSpecs } from "./sharedSpecs";

describe("RiseLoader", () => {
let loader: ReactWrapper;
let props: LoaderSizeMarginProps;
const defaultSize = 15;
const defaultMargin = 2;
const defaultColor = "#000000";
const defaultUnit = "px";

it("should match snapshot", () => {
loader = mount(<RiseLoader />);
expect(loader).toMatchSnapshot();
});

it("should contain default props if no props are passed", () => {
props = loader.props();
expect(props).toEqual(sizeMarginDefaults(defaultSize));
});
commonSpecs(RiseLoader, sizeMarginDefaults(defaultSize));
cssSpecs(RiseLoader);

it("should contain styles created using default props", () => {
const loader = mount(<RiseLoader />);
for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).toHaveStyleRule("background-color", defaultColor);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"height",
`${defaultSize}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"width",
`${defaultSize}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"margin",
`${defaultMargin}${defaultUnit}`
);
}
});

it("should render null if loading prop is set as false", () => {
loader = mount(<RiseLoader loading={false} />);
expect(loader.isEmptyRender()).toBe(true);
});

it("should render the correct color based on prop", () => {
const color = "#e2e2e2";
loader = mount(<RiseLoader color={color} />);
const loader = mount(<RiseLoader color={color} />);
for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule("background-color", defaultColor);
expect(loader.find("span span").at(i)).toHaveStyleRule("background-color", color);
}
});

it("should render the correct size based on props", () => {
const size = 18;
loader = mount(<RiseLoader size={size} />);

const sizeExpectStatements = (loader: ReactWrapper, length: number, unit?: string) => {
for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"height",
Expand All @@ -68,119 +55,29 @@ describe("RiseLoader", () => {
`${defaultSize}${defaultUnit}`
);

expect(loader.find("span span").at(i)).toHaveStyleRule("height", `${size}${defaultUnit}`);
expect(loader.find("span span").at(i)).toHaveStyleRule("width", `${size}${defaultUnit}`);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"height",
`${length}${unit || defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"width",
`${length}${unit || defaultUnit}`
);
}
});

describe("size props", () => {
it("should render the size with px unit when size is a number", () => {
const size = 18;
loader = mount(<RiseLoader size={size} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"height",
`${defaultSize}${defaultUnit}`
);
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"width",
`${defaultSize}${defaultUnit}`
);

expect(loader.find("span span").at(i)).toHaveStyleRule("height", `${size}${defaultUnit}`);
expect(loader.find("span span").at(i)).toHaveStyleRule("width", `${size}${defaultUnit}`);
}
});

it("should render the size as is when size is a string with valid css unit", () => {
const size = "18px";
loader = mount(<RiseLoader size={size} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"height",
`${defaultSize}${defaultUnit}`
);
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"width",
`${defaultSize}${defaultUnit}`
);
};
lengthSpecs(RiseLoader, "size", sizeExpectStatements);

expect(loader.find("span span").at(i)).toHaveStyleRule("height", `${size}`);
expect(loader.find("span span").at(i)).toHaveStyleRule("width", `${size}`);
}
});

it("should render the size with default unit of px when the unit is incorrect", () => {
const length = 18;
const unit = "ad";
const size = `${length}${unit}`;
loader = mount(<RiseLoader size={size} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"height",
`${defaultSize}${defaultUnit}`
);
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"width",
`${defaultSize}${defaultUnit}`
);

expect(loader.find("span span").at(i)).toHaveStyleRule("height", `${length}${defaultUnit}`);
expect(loader.find("span span").at(i)).toHaveStyleRule("width", `${length}${defaultUnit}`);
}
});
});

describe("margin props", () => {
it("should render the margin with px unit when margin is a number", () => {
const margin = 18;
loader = mount(<RiseLoader margin={margin} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"margin",
`${defaultMargin}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule("margin", `${margin}${defaultUnit}`);
}
});

it("should render the margin as is when margin is a string with valid css unit", () => {
const margin = "18px";
loader = mount(<RiseLoader margin={margin} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"margin",
`${defaultMargin}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule("margin", `${margin}`);
}
});

it("should render the margin with default unit of px when the unit is incorrect", () => {
const length = 18;
const unit = "ad";
const margin = `${length}${unit}`;
loader = mount(<RiseLoader margin={margin} />);

for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"margin",
`${defaultMargin}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule("margin", `${length}${defaultUnit}`);
}
});
});

it("should render the css override based on props", () => {
loader = mount(<RiseLoader css={"position: fixed; width: 100px; color: blue;"} />);
expect(loader).toHaveStyleRule("position", "fixed");
expect(loader).toHaveStyleRule("width", "100px");
expect(loader).toHaveStyleRule("color", "blue");
});
const marginExpectStatements = (loader: ReactWrapper, length: number, unit?: string) => {
for (let i = 0; i < 5; i++) {
expect(loader.find("span span").at(i)).not.toHaveStyleRule(
"margin",
`${defaultMargin}${defaultUnit}`
);
expect(loader.find("span span").at(i)).toHaveStyleRule(
"margin",
`${length}${unit || defaultUnit}`
);
}
};
lengthSpecs(RiseLoader, "margin", marginExpectStatements);
});
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/RiseLoader-tests.tsx.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`RiseLoader should match snapshot 1`] = `
exports[`RiseLoader common specs should match snapshot 1`] = `
@keyframes animation-0 {
0% {
-webkit-transform: scale(0.4);
Expand Down

0 comments on commit ff18bf6

Please sign in to comment.