-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2328188
commit b820017
Showing
7 changed files
with
274 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
function is(x: unknown, y: unknown) { | ||
if (x === y) { | ||
return x !== 0 || y !== 0 || 1 / x === 1 / y | ||
} else { | ||
return x !== x && y !== y | ||
} | ||
} | ||
|
||
export function shallowEqual(objA: any, objB: any) { | ||
if (is(objA, objB)) return true | ||
|
||
if ( | ||
typeof objA !== 'object' || | ||
objA === null || | ||
typeof objB !== 'object' || | ||
objB === null | ||
) { | ||
return false | ||
} | ||
|
||
const keysA = Object.keys(objA) | ||
const keysB = Object.keys(objB) | ||
|
||
if (keysA.length !== keysB.length) return false | ||
|
||
for (let i = 0; i < keysA.length; i++) { | ||
if ( | ||
!(Object.prototype.hasOwnProperty as Function).call(objB, keysA[i]) || | ||
!is(objA[keysA[i]!], objB[keysA[i]!]) | ||
) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest' | ||
import { createSubscription } from '../src/utils/Subscription' | ||
import type { Subscription } from '../src/utils/Subscription' | ||
import type { Store } from 'redux' | ||
|
||
describe('Subscription', () => { | ||
let notifications: string[] | ||
let store: Store | ||
let parent: Subscription | ||
|
||
beforeEach(() => { | ||
notifications = [] | ||
store = { subscribe: () => vi.fn() } as unknown as Store | ||
|
||
parent = createSubscription(store) | ||
parent.onStateChange = () => {} | ||
parent.trySubscribe() | ||
}) | ||
|
||
function subscribeChild(name: string) { | ||
const child = createSubscription(store, parent) | ||
child.onStateChange = () => notifications.push(name) | ||
child.trySubscribe() | ||
return child | ||
} | ||
|
||
it('listeners are notified in order', () => { | ||
subscribeChild('child1') | ||
subscribeChild('child2') | ||
subscribeChild('child3') | ||
subscribeChild('child4') | ||
|
||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child1', 'child2', 'child3', 'child4']) | ||
}) | ||
|
||
it('listeners can be unsubscribed', () => { | ||
const child1 = subscribeChild('child1') | ||
const child2 = subscribeChild('child2') | ||
const child3 = subscribeChild('child3') | ||
|
||
child2.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child1', 'child3']) | ||
notifications.length = 0 | ||
|
||
child1.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual(['child3']) | ||
notifications.length = 0 | ||
|
||
child3.tryUnsubscribe() | ||
parent.notifyNestedSubs() | ||
|
||
expect(notifications).toEqual([]) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { shallowEqual } from '../src' | ||
|
||
describe('Utils', () => { | ||
describe('shallowEqual', () => { | ||
it('should return true if arguments fields are equal', () => { | ||
expect( | ||
shallowEqual( | ||
{ a: 1, b: 2, c: undefined }, | ||
{ a: 1, b: 2, c: undefined }, | ||
), | ||
).toBe(true) | ||
|
||
expect(shallowEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 })).toBe( | ||
true, | ||
) | ||
|
||
const o = {} | ||
expect(shallowEqual({ a: 1, b: 2, c: o }, { a: 1, b: 2, c: o })).toBe( | ||
true, | ||
) | ||
|
||
const d = function () { | ||
return 1 | ||
} | ||
expect( | ||
shallowEqual({ a: 1, b: 2, c: o, d }, { a: 1, b: 2, c: o, d }), | ||
).toBe(true) | ||
}) | ||
|
||
it('should return false if arguments fields are different function identities', () => { | ||
expect( | ||
shallowEqual( | ||
{ | ||
a: 1, | ||
b: 2, | ||
d: function () { | ||
return 1 | ||
}, | ||
}, | ||
{ | ||
a: 1, | ||
b: 2, | ||
d: function () { | ||
return 1 | ||
}, | ||
}, | ||
), | ||
).toBe(false) | ||
}) | ||
|
||
it('should return false if first argument has too many keys', () => { | ||
expect(shallowEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 })).toBe(false) | ||
}) | ||
|
||
it('should return false if second argument has too many keys', () => { | ||
expect(shallowEqual({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 })).toBe(false) | ||
}) | ||
|
||
it('should return false if arguments have different keys', () => { | ||
expect( | ||
shallowEqual( | ||
{ a: 1, b: 2, c: undefined }, | ||
{ a: 1, bb: 2, c: undefined }, | ||
), | ||
).toBe(false) | ||
}) | ||
|
||
it('should compare two NaN values', () => { | ||
expect(shallowEqual(NaN, NaN)).toBe(true) | ||
}) | ||
|
||
it('should compare empty objects, with false', () => { | ||
expect(shallowEqual({}, false)).toBe(false) | ||
expect(shallowEqual(false, {})).toBe(false) | ||
expect(shallowEqual([], false)).toBe(false) | ||
expect(shallowEqual(false, [])).toBe(false) | ||
}) | ||
|
||
it('should compare two zero values', () => { | ||
expect(shallowEqual(0, 0)).toBe(true) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters