Skip to content

Commit

Permalink
Merge pull request #80 from gaeaehrlich/remove_useunmounted
Browse files Browse the repository at this point in the history
  • Loading branch information
yairEO authored Mar 1, 2022
2 parents 1084c51 + 729d5ed commit 22337a9
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export {default as useAnimationFrame} from './useAnimationFrame';
export {default as useBooleanState, useVisibilityState, useFocusabilityState} from './useBooleanState';
export {default as useBoundingRectObserver} from './useBoundingRectObserver';
export {default as useClickOutside, ClickOutside, ClickOutsideOverride} from './useClickOutside';
export {default as useMounted, useUnmounted} from './useMounted';
export {default as useMounted} from './useMounted';
export {default as useDebounce} from './useDebounce';
export {default as useDimensions} from './useDimensions';
export {default as useEventListener} from './useEventListener';
Expand Down
1 change: 0 additions & 1 deletion src/hooks/useMounted/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@
*/

import {useMounted} from './useMounted';
export {useUnmounted} from './useMounted';
export default useMounted;
7 changes: 0 additions & 7 deletions src/hooks/useMounted/useMounted.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ export const useMounted = () => {
const mounted = useRef(false);
useEffect(() => {
mounted.current = true;
return () => mounted.current = false;
}, []);
return mounted.current;
};

export const useUnmounted = () => {
const unmounted = useRef(false);
useEffect(() => () => {unmounted.current = true}, []);
return unmounted.current;
};
20 changes: 1 addition & 19 deletions src/hooks/useMounted/useMounted.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react';
import {act} from 'react-dom/test-utils';
import {mount} from 'enzyme';
import {useMounted, useUnmounted} from './useMounted';
import {useMounted} from './useMounted';

describe('useMounted()', () => {
it('Should return the previous value', () => {
Expand All @@ -20,21 +20,3 @@ describe('useMounted()', () => {
expect(wrapper.find('.unmounted').length).toEqual(0);
});
});

describe('useUnmounted()', () => {
it('Should return the previous value', () => {
const Elem = () => {
const unmounted = useUnmounted();
return (
<div className={`${unmounted ? 'un' : ''}mounted`}/>
);
};
let wrapper = null;
act(() => {wrapper = mount(<Elem/>)});
expect(wrapper.find('.unmounted').length).toEqual(0);
expect(wrapper.find('.mounted').length).toEqual(1);
wrapper.setProps({foo: 'bar'}); // Force an update...
expect(wrapper.find('.mounted').length).toEqual(1);
expect(wrapper.find('.unmounted').length).toEqual(0);
});
});
28 changes: 27 additions & 1 deletion src/utility/object/object.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {get, set, isEqual, omit, clone} from './object';
import {get, set, isEqual, omit, clone, EqualityIterators} from './object';

describe('Object', () => {

Expand Down Expand Up @@ -86,6 +86,32 @@ describe('Object', () => {
expect(isEqual(parent1, parent2)).toEqual(false);
});

it('isEqual() with shallow comparison', () => {
expect(isEqual(1, 1, EqualityIterators.SHALLOW)).toEqual(true);
expect(isEqual(true, false, EqualityIterators.SHALLOW)).toEqual(false);
expect(isEqual('hello', 'hello', EqualityIterators.SHALLOW)).toEqual(true);
expect(isEqual([1, 2, 3], [1, 2, 3], EqualityIterators.SHALLOW)).toEqual(true);
expect(isEqual([1, 2, 3], [3, 2, 1], EqualityIterators.SHALLOW)).toEqual(false);
expect(isEqual([{id: 1}], [{id: 2}], EqualityIterators.SHALLOW)).toEqual(false);
expect(isEqual({foo: 'bar'}, {foo: 'bar'}, EqualityIterators.SHALLOW)).toEqual(true);
expect(isEqual({foo: 'bar'}, {bar: 'foo'}, EqualityIterators.SHALLOW)).toEqual(false);
expect(isEqual({foo: [1, 2, {a: 1}]}, {foo: [1, 2, {a: 1}]}, EqualityIterators.SHALLOW)).toEqual(false);
expect(isEqual({foo: [1, 2, {a: 1}]}, {foo: [1, 2, {a: 2}]}, EqualityIterators.SHALLOW)).toEqual(false);

/* Check Circular Traversal */
const sub1 = {foo: [1, 2, {a: 1}]};
const sub2 = {foo: [1, 2, {a: 1}]};
const parent1 = {foo: [1, 2, {a: 1}], sub1, sub2};
const parent2 = {foo: [1, 2, {a: 1}], sub1, sub2};
parent1.aaa = parent2;
parent1.bbb = parent1;
parent2.aaa = parent1;
parent2.bbb = parent2;
expect(isEqual(parent1, parent2, EqualityIterators.SHALLOW)).toEqual(false);
parent2.ccc = 6;
expect(isEqual(parent1, parent2, EqualityIterators.SHALLOW)).toEqual(false);
});

it('clone()', () => {
const source = {foo: 1, bar: 'hello', baz: {baq: 23, test: [1, 2, 3]}};
const target = clone(source);
Expand Down

0 comments on commit 22337a9

Please sign in to comment.