Skip to content

Commit

Permalink
feat(lib): add stories + tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emileNetter committed Apr 21, 2021
1 parent e0b000d commit 488bcfe
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
115 changes: 115 additions & 0 deletions packages/junipero/lib/RadioField/index.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import React, { useState } from 'react';
import { action } from '@storybook/addon-actions';

import RadioField from './index';

export default { title: 'junipero/RadioField' };

export const basic = () => {
const [checked, setChecked] = useState(false);

const onChange = value => {
setChecked(value.checked);
};

return (
<>
<RadioField checked={checked}
onChange={value => onChange(value)}
label="Enabled"/>
<button onClick={() => setChecked(false)}>reset</button>
</>

);
};

export const disabled = () => {
return (
<RadioField label="Disabled" onChange={action('change')} disabled/>
);
};

export const active = () => {
return (
<RadioField label="Active" onChange={action('change')} checked/>
);
};

export const activeDisabled = () => {
return (
<RadioField
label="Active and disabled"
onChange={action('change')}
disabled
checked
/>
);
};

export const withDescription = () => {
return (
<RadioField
label="Super title"
description="This is a description"
onChange={action('change')}
checked
/>
);
};

export const withDescriptionAndUnchecked = () => {
return (
<RadioField
label="Super title"
description="This is a description"
onChange={action('change')}
/>
);
};

export const withDescriptionButDisabled = () => {
return (
<RadioField
label="Super title"
description="This is a description"
onChange={action('change')}
disabled
/>
);
};

export const withDescriptionChecked = () => {
return (
<RadioField
label="Super title"
description="This is a description"
onChange={action('change')}
checked
/>
);
};

export const twoRadiosCombined = () => {
const [smart, setSmart] = useState(false);
const [strong, setStrong] = useState(false);

const onChange = (value, type) => {
};

return (
<>
<RadioField
label="This one's stronger"
description="This is a description"
onChange={value => onChange(value, 'strong')}
checked={strong}
/>
<RadioField
label="This one's smarter"
description="This is a description"
onChange={value => onChange(value, 'smart')}
checked={smart}
/>
</>
);
};
31 changes: 31 additions & 0 deletions packages/junipero/lib/RadioField/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { createRef } from 'react';
import { mount } from 'enzyme';
import sinon from 'sinon';

import RadioField from './';

describe('<RadioField />', () => {

it('should render', () => {
const component = mount(
<RadioField label="Test" />
);
expect(component.find('.junipero.radio').length).toBe(1);
});

it('should provide some imperative handles', () => {
const ref = createRef();
const component = mount(<RadioField ref={ref} label="Test" />);
expect(ref.current.innerRef.current).toBeDefined();
expect(component.getDOMNode()).toBe(ref.current.innerRef.current);
});

it('should correctly fire onChange event', () => {
const onChange = sinon.spy();
const component = mount(<RadioField checked={true} onChange={onChange} />);
component.find('input').simulate('change', { target: { checked: true } });
expect(onChange.withArgs(sinon.match({ checked: true })).called)
.toBe(true);
});

});

0 comments on commit 488bcfe

Please sign in to comment.