diff --git a/packages/react/src/useStore.spec.ts b/packages/react/src/useStore.spec.ts index 6dd0b2e3..fab28966 100644 --- a/packages/react/src/useStore.spec.ts +++ b/packages/react/src/useStore.spec.ts @@ -9,9 +9,23 @@ import Fleur, { import * as React from 'react' import { renderHook, act } from '@testing-library/react-hooks' -import { useStore } from './useStore' +import { isEqual, useStore } from './useStore' import { FleurContext } from './ComponentReactContext' +describe('isEqual', () => { + it('equals', () => { + expect(isEqual(1, 1)).toBe(true) + expect(isEqual([1, 2, 3], [1, 2, 3])).toBe(true) + expect(isEqual({ a: 1, b: 2 }, { a: 1, b: 2 })).toBe(true) + }) + + it('not equals', () => { + expect(isEqual(1, 2)).toBe(false) + expect(isEqual([1, 2, 3], [3, 2, 1])).toBe(false) + expect(isEqual({ a: 1, b: 2 }, { a: 1, b: null })).toBe(false) + }) +}) + describe('useStore', () => { // Action Identifier const ident = action<{ increase: number }>() diff --git a/packages/react/src/useStore.ts b/packages/react/src/useStore.ts index e3c40030..5f65393b 100644 --- a/packages/react/src/useStore.ts +++ b/packages/react/src/useStore.ts @@ -43,7 +43,7 @@ const is = (x: any, y: any): boolean => { } /** Shallow equality check */ -const isEqual = (prev: any, next: any) => { +export const isEqual = (prev: any, next: any) => { if (is(prev, next)) return true if (typeof prev !== typeof next) return false @@ -53,6 +53,8 @@ const isEqual = (prev: any, next: any) => { for (const idx in prev) { if (!is(prev[idx], next[idx])) return false } + + return true } if ( @@ -65,6 +67,8 @@ const isEqual = (prev: any, next: any) => { if (!hasOwnKey.call(next, key)) continue if (!is(prev[key], next[key])) return false } + + return true } return false