Skip to content

Commit

Permalink
feat(framework): toggle missing events tests add
Browse files Browse the repository at this point in the history
  • Loading branch information
32penkin committed Jan 21, 2019
1 parent 54ea653 commit 832d27f
Show file tree
Hide file tree
Showing 3 changed files with 900 additions and 257 deletions.
79 changes: 43 additions & 36 deletions src/framework/ui/toggle/toggle.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
PanResponderInstance,
GestureResponderEvent,
PanResponderGestureState,
TouchableOpacity,
} from 'react-native';
import {
StyledComponentProps,
Expand Down Expand Up @@ -172,9 +173,9 @@ export class Toggle extends React.Component<Props> {
this.animateThumb(thumbSize);
};

onValueChange = (value: boolean) => {
onValueChange = () => {
if (this.props.onValueChange) {
this.props.onValueChange(value);
this.props.onValueChange(this.props.value);
}
};

Expand Down Expand Up @@ -258,42 +259,48 @@ export class Toggle extends React.Component<Props> {
return (
<View style={[componentStyle.wrapper, styles.wrapper, style]}>
{!this.props.disabled && <View style={[styles.highlight, componentStyle.highlight]}/>}
<Animated.View
style={[
styles.container,
componentStyle.componentContainer,
{ backgroundColor: interpolatedTintColor },
]}
{...this.panResponder.panHandlers}>
<Animated.View style={[
styles.ellipse,
componentStyle.componentEllipse,
{
transform: [{ scale: value ? returnScale : this.ellipseAnimation }],
backgroundColor: interpolatedTintColor,
},
]}
/>
<Animated.View style={[
componentStyle.componentThumb,
styles.thumb,
{
width: this.thumbAnimation,
alignSelf: value ? 'flex-end' : 'flex-start',
transform: [{ translateX: this.switchAnimation }],
borderColor: interpolatedTintColor,
backgroundColor: componentStyle.colors.thumb,
elevation: disabled ? 0 : 5,
},
]}
>
<CheckMark
size={componentStyle.checkMarkSize}
color={interpolatedCheckColor}
isAnimated={true}
<TouchableOpacity
onPressIn={this.onPressIn}
onPressOut={this.onPressOut}
onPress={this.onValueChange}
>
<Animated.View
style={[
styles.container,
componentStyle.componentContainer,
{ backgroundColor: interpolatedTintColor },
]}
{...this.panResponder.panHandlers}>
<Animated.View style={[
styles.ellipse,
componentStyle.componentEllipse,
{
transform: [{ scale: value ? returnScale : this.ellipseAnimation }],
backgroundColor: interpolatedTintColor,
},
]}
/>
<Animated.View style={[
componentStyle.componentThumb,
styles.thumb,
{
width: this.thumbAnimation,
alignSelf: value ? 'flex-end' : 'flex-start',
transform: [{ translateX: this.switchAnimation }],
borderColor: interpolatedTintColor,
backgroundColor: componentStyle.colors.thumb,
elevation: disabled ? 0 : 5,
},
]}
>
<CheckMark
size={componentStyle.checkMarkSize}
color={interpolatedCheckColor}
isAnimated={true}
/>
</Animated.View>
</Animated.View>
</Animated.View>
</TouchableOpacity>
</View>
);
}
Expand Down
56 changes: 52 additions & 4 deletions src/framework/ui/toggle/toggle.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,30 @@ import {
render,
shallow,
RenderAPI,
fireEvent,
waitForElement,
} from 'react-native-testing-library';
import { TouchableOpacity } from 'react-native';
import {
styled,
StyleProvider,
StyleProviderProps,
} from '@kitten/theme';
import {
Toggle,
ToggleProps,
Props,
} from './toggle.component';
import * as config from './toggle.spec.config';

const StyledComponent = styled<Toggle, ToggleProps>(Toggle);
const StyledComponent = styled<Toggle, Props>(Toggle);

const Mock = (props?: ToggleProps): React.ReactElement<StyleProviderProps> => (
const Mock = (props?: Props): React.ReactElement<StyleProviderProps> => (
<StyleProvider mapping={config.mapping} theme={config.theme} styles={{}}>
<StyledComponent {...props} />
</StyleProvider>
);

const renderComponent = (props?: ToggleProps): RenderAPI => render(<Mock {...props}/>);
const renderComponent = (props?: Props): RenderAPI => render(<Mock {...props}/>);

describe('@toggle: matches snapshot', () => {

Expand Down Expand Up @@ -55,4 +58,49 @@ describe('@toggle: matches snapshot', () => {
expect(output).toMatchSnapshot();
});

it('active', async () => {
const component = renderComponent();

fireEvent(component.getByType(TouchableOpacity), 'pressIn');

const active = await waitForElement(() => component.getByType(Toggle));
const { output: activeOutput } = shallow(active);

fireEvent(component.getByType(TouchableOpacity), 'pressOut');

const inactive = await waitForElement(() => component.getByType(Toggle));
const { output: inactiveOutput } = shallow(inactive);

expect(activeOutput).toMatchSnapshot();
expect(inactiveOutput).toMatchSnapshot('default');
});

it('active checked', async () => {
const component = renderComponent({ value: true });

fireEvent(component.getByType(TouchableOpacity), 'pressIn');
const active = await waitForElement(() => component.getByType(Toggle));
const { output: activeOutput } = shallow(active);

fireEvent(component.getByType(TouchableOpacity), 'pressOut');

const inactive = await waitForElement(() => component.getByType(Toggle));
const { output: inactiveOutput } = shallow(inactive);

expect(activeOutput).toMatchSnapshot();
expect(inactiveOutput).toMatchSnapshot('checked');
});

});

describe('@toggle: component checks', () => {

it('emits onValueChange', () => {
const onChange = jest.fn();
const component = renderComponent({ onValueChange: onChange });
fireEvent.press(component.getByType(TouchableOpacity));

expect(onChange).toBeCalled();
});

});
Loading

0 comments on commit 832d27f

Please sign in to comment.