Skip to content

Commit

Permalink
fix: more test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
hvolschenk committed Nov 8, 2017
1 parent 74c34ff commit 3e412fb
Showing 1 changed file with 139 additions and 0 deletions.
139 changes: 139 additions & 0 deletions src/index.test.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import rippleFoundation from '@materialr/ripple';
import { mount, shallow } from 'enzyme';
import React from 'react';

Expand Down Expand Up @@ -61,3 +62,141 @@ test('Does not add a ripple when it is disabled', () => {

expect(actual).toBe(expected);
});

test('Adds a ripple when it is enabled', () => {
const wrapper = mount(<Button rippleEnabled>{CHILDREN}</Button>);
const { disabled, rippleCentered } = wrapper.props();
const instance = wrapper.instance();
const { button, updateClassNames, updateCssVariables } = instance;
const expected = rippleFoundation({
centered: rippleCentered,
disabled,
element: button,
self: instance,
updateClassNames,
updateCssVariables,
});

const actual = instance.rippleFoundation;

expect(JSON.stringify(actual)).toEqual(JSON.stringify(expected));
});

test('Adds the ripple if the prop changes', () => {
const wrapper = mount(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.rippleCreate = jest.fn();

wrapper.setProps({ rippleEnabled: true });

expect(instance.rippleCreate).toHaveBeenCalledTimes(1);
});

test('Removes the ripple if the prop changes', () => {
const wrapper = mount(<Button rippleEnabled>{CHILDREN}</Button>);
const instance = wrapper.instance();
const expected = undefined;

wrapper.setProps({ rippleEnabled: false });
const actual = instance.rippleFoundation;

expect(actual).toBe(expected);
});

test('Centers the ripple if it was previously uncentered', () => {
const wrapper = mount(<Button rippleEnabled>{CHILDREN}</Button>);
const { disabled } = wrapper.props();
const instance = wrapper.instance();
const { button, updateClassNames, updateCssVariables } = instance;
const expected = rippleFoundation({
centered: true,
disabled,
element: button,
self: instance,
updateClassNames,
updateCssVariables,
});

wrapper.setProps({ rippleCentered: true });
const actual = instance.rippleFoundation;

expect(JSON.stringify(actual)).toEqual(JSON.stringify(expected));
});

test('Updates classNames in state when \'updateClassNames()\' is called', () => {
const CLASS_NAMES = ['CLASS_NAME'];
const wrapper = mount(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
const expected = CLASS_NAMES;

instance.updateClassNames(CLASS_NAMES);
const actual = instance.state.classNames;

expect(actual).toEqual(expected);
});

test('Does not update classNames in state when \'updateClassNames()\' is called on an unmounted component', () => {
const CLASS_NAMES = ['CLASS_NAME'];
const wrapper = shallow(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.setState = jest.fn();

instance.componentIsMounted = false;
instance.updateClassNames(CLASS_NAMES);

expect(instance.setState).toHaveBeenCalledTimes(0);
});

test('Updates cssVariables in state when \'updateCssVariables()\' is called', () => {
const CSS_VARIABLES = ['CSS_VARIABLE'];
const wrapper = mount(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
const expected = CSS_VARIABLES;

instance.updateCssVariables(CSS_VARIABLES);
const actual = instance.state.cssVariables;

expect(actual).toEqual(expected);
});

test('Does not update cssVariables in state when \'updateCssVariables()\' is called on an unmounted component', () => {
const CSS_VARIABLES = ['CSS_VARIABLE'];
const wrapper = mount(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.setState = jest.fn();

instance.componentIsMounted = false;
instance.updateCssVariables(CSS_VARIABLES);

expect(instance.setState).toHaveBeenCalledTimes(0);
});

test('Destroys the ripple when the component unmounts', () => {
const wrapper = mount(<Button rippleEnabled>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.rippleDestroy = jest.fn();

wrapper.unmount();

expect(instance.rippleDestroy).toHaveBeenCalledTimes(1);
});

test('Does not detroy the ripple when the component unmounts without a ripple', () => {
const wrapper = mount(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.rippleDestroy = jest.fn();

wrapper.unmount();

expect(instance.rippleDestroy).toHaveBeenCalledTimes(0);
});

test('Does not detroy the ripple when the component unmounts', () => {
const wrapper = shallow(<Button>{CHILDREN}</Button>);
const instance = wrapper.instance();
instance.rippleDestroy = jest.fn();

wrapper.unmount();

expect(instance.rippleDestroy).toHaveBeenCalledTimes(0);
});

0 comments on commit 3e412fb

Please sign in to comment.