Skip to content

Commit

Permalink
Refactor ClipLoader to use shared specs
Browse files Browse the repository at this point in the history
  • Loading branch information
davidhu2000 committed Feb 21, 2021
1 parent 5e98482 commit 1fab885
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 70 deletions.
80 changes: 11 additions & 69 deletions __tests__/ClipLoader-tests.tsx
Original file line number Diff line number Diff line change
@@ -1,97 +1,39 @@
import * as React from "react";
import { mount, ReactWrapper } from "enzyme";
import { matchers } from '@emotion/jest';
import { matchers } from "@emotion/jest";
expect.extend(matchers);

import ClipLoader from "../src/ClipLoader";
import { LoaderSizeProps } from "../src/interfaces";
import { sizeDefaults } from "../src/helpers";
import { commonSpecs, cssSpecs, lengthSpecs } from "./sharedSpecs";

describe("ClipLoader", () => {
let loader: ReactWrapper<LoaderSizeProps, null, ClipLoader>;
let props: LoaderSizeProps;
const defaultColor = "#000000";
const defaultSize = 35;
const defaultUnit = "px";

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

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

it("parent span should contain styles created using default props", () => {
const loader = mount(<ClipLoader />);
expect(loader).toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("border-color", defaultColor);
});

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

it("should render the correct color based on prop", () => {
const color = "#e2e2e2";
loader = mount(<ClipLoader color={color} />);
const loader = mount(<ClipLoader color={color} />);
expect(loader).not.toHaveStyleRule("border-color", defaultColor);
expect(loader).toHaveStyleRule("border-color", color);
});

it("should render the correct size based on props", () => {
const size = 18;
loader = mount(<ClipLoader size={size} />);
const sizeExpectStatements = (loader: ReactWrapper, length: number, unit?: string) => {
expect(loader).not.toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).not.toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("height", `${size}${defaultUnit}`);
expect(loader).toHaveStyleRule("width", `${size}${defaultUnit}`);
});

describe("size props", () => {
it("should render the size with px unit when size is a number", () => {
const size = 18;
loader = mount(<ClipLoader size={size} />);
expect(loader).not.toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).not.toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("height", `${size}${defaultUnit}`);
expect(loader).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(<ClipLoader size={size} />);
expect(loader).not.toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).not.toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("height", `${size}`);
expect(loader).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(<ClipLoader size={size} />);
expect(loader).not.toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).not.toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("height", `${length}${defaultUnit}`);
expect(loader).toHaveStyleRule("width", `${length}${defaultUnit}`);
});
});

it("should render the css override based on props", () => {
loader = mount(
<ClipLoader css={"position: absolute; width: 100px; height: 200px; color: blue;"} />
);
expect(loader).not.toHaveStyleRule("position", "relative");
expect(loader).not.toHaveStyleRule("width", `${defaultSize}${defaultUnit}`);
expect(loader).not.toHaveStyleRule("height", `${defaultSize}${defaultUnit}`);
expect(loader).toHaveStyleRule("position", "absolute");
expect(loader).toHaveStyleRule("width", "100px");
expect(loader).toHaveStyleRule("height", "200px");
expect(loader).toHaveStyleRule("color", "blue");
});
expect(loader).toHaveStyleRule("height", `${length}${unit || defaultUnit}`);
expect(loader).toHaveStyleRule("width", `${length}${unit || defaultUnit}`);
};
lengthSpecs(ClipLoader, "size", sizeExpectStatements);
});
2 changes: 1 addition & 1 deletion __tests__/__snapshots__/ClipLoader-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[`ClipLoader should match snapshot 1`] = `
exports[`ClipLoader common specs should match snapshot 1`] = `
@keyframes animation-0 {
0% {
-webkit-transform: rotate(0deg) scale(1);
Expand Down

0 comments on commit 1fab885

Please sign in to comment.