Skip to content

feat/useToggleValue #1019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions generatedTypes/commons/new.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export { default as asBaseComponent, BaseComponentInjectedProps } from './asBase
export { default as forwardRef, ForwardRefInjectedProps } from './forwardRef';
export { default as withScrollEnabler, WithScrollEnablerProps } from './withScrollEnabler';
export { default as withScrollReached, WithScrollReachedProps } from './withScrollReached';
export { default as useToggleValue } from './useToggleValue';
export { ContainerModifiers, MarginModifiers, PaddingModifiers, TypographyModifiers, ColorsModifiers, BackgroundColorModifier } from './modifiers';
2 changes: 2 additions & 0 deletions generatedTypes/commons/useToggleValue/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare const useToggleValue: (initial: any, second?: any) => any[];
export default useToggleValue;
4 changes: 3 additions & 1 deletion generatedTypes/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
export * from './style';
export * from './services';
export * as Incubator from './incubator';
export * as Hooks from './hooks';
export {
asBaseComponent,
withScrollEnabler,
Expand All @@ -19,7 +20,8 @@ export {
PaddingModifiers,
TypographyModifiers,
ColorsModifiers,
BackgroundColorModifier
BackgroundColorModifier,
useToggleValue
} from './commons/new';
export {default as ActionBar, ActionBarProps} from './components/actionBar';
export {default as Avatar, AvatarPropTypes} from './components/avatar';
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@
"@react-native-community/eslint-config": "^1.1.0",
"@react-native-community/netinfo": "^5.6.2",
"@react-native-community/picker": "^1.6.5",
"@testing-library/react-hooks": "^3.4.2",
"@types/lodash": "^4.0.0",
"@types/prop-types": "^15.5.3",
"@types/react-native": "0.62.2",
Expand Down
1 change: 1 addition & 0 deletions src/commons/new.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export {default as asBaseComponent, BaseComponentInjectedProps} from './asBaseCo
export {default as forwardRef, ForwardRefInjectedProps} from './forwardRef';
export {default as withScrollEnabler, WithScrollEnablerProps} from './withScrollEnabler';
export {default as withScrollReached, WithScrollReachedProps} from './withScrollReached';

export {
ContainerModifiers,
MarginModifiers,
Expand Down
1 change: 1 addition & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default as useToggleValue} from './useToggleValue';
89 changes: 89 additions & 0 deletions src/hooks/useToggleValue/__tests__/useToggleValue.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import {renderHook, act} from '@testing-library/react-hooks';
import useToggleValue from '../index';

describe('useOrientation hook tests', () => {
const makeSUT = (initial, secondary) => {
return renderHook(() => {
return useToggleValue(initial, secondary);
});
};

const getValue = (result) => {
return result.current[0];
};

const getToggle = (result) => {
return result.current[1]();
};

const expectValueToToggle = (a, b) => {
const {result} = makeSUT(a, b);
expect(getValue(result)).toEqual(a);

act(() => getToggle(result));
expect(getValue(result)).toEqual(b);
};

it('Initial value should be set', () => {
const {result} = makeSUT(true, false);

expect(getValue(result)).toEqual(true);
});

it('Initial value can be undefined', () => {
const {result} = makeSUT(undefined, 'Sid');
expect(getValue(result)).toEqual(undefined);

act(() => getToggle(result));
expect(getValue(result)).toEqual('Sid');
});

it('Initial value can be change', () => {
const {result} = makeSUT(true, false);
act(() => getToggle(result));

expect(getValue(result)).toEqual(false);
});

it('Allow toggle boolean without passing a second parameter', () => {
const {result} = makeSUT(true);
act(() => getToggle(result));

expect(getValue(result)).toEqual(false);
});

it('Can toggle strings', () => {
const a = 'Ohio';
const b = 'Texas';

expectValueToToggle(a, b);
});

it('Can toggle numbers', () => {
const a = 30;
const b = 22;

expectValueToToggle(a, b);
});

it('Can toggle arrays', () => {
const a = [1, 2, 3, 4];
const b = ['Booking', 'OneApp', 'Fed'];

expectValueToToggle(a, b);
});

it('Can toggle objects', () => {
const a = {};
const b = {name: 'Darth', occupation: 'Father'};

expectValueToToggle(a, b);
});

it('Can toggle different types', () => {
const a = 999;
const b = '999';

expectValueToToggle(a, b);
});
});
23 changes: 23 additions & 0 deletions src/hooks/useToggleValue/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {useState, useRef} from 'react';
import _ from 'lodash';

const useToggleValue = (initial: any, second?: any) => {
const initialValue = useRef(initial).current;
const secondValue = useRef(second).current;

const [value, setValue] = useState(initial);

const toggle = () => {
if (_.isBoolean(initialValue)) {
setValue(!initialValue);
} else if (value == initialValue) {
setValue(secondValue);
} else {
setValue(initialValue);
}
};

return [value, toggle, setValue];
};

export default useToggleValue;
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,9 @@ export default {
get AnimatableManager() {
return require('./style').AnimatableManager;
},
get Hooks() {
return require('./hooks');
},

// Incubator
get Incubator() {
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export {
BackgroundColorModifier
} from './commons/new';
export * as Incubator from './incubator';
export * as Hooks from './hooks';
export {default as ActionBar, ActionBarProps} from './components/actionBar';
export {default as Avatar, AvatarPropTypes} from './components/avatar';
export {default as Badge, BadgeProps} from './components/badge';
Expand Down
1 change: 1 addition & 0 deletions typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export * from './nativeComponents';
export * from './services';
export * from './generatedTypes';
export * from './globalTypes';
export * from './hooks';