Skip to content

Commit 117cbb1

Browse files
committed
test: add tests for Subscription and shallowEqual
1 parent 66cd4c7 commit 117cbb1

File tree

5 files changed

+142
-8
lines changed

5 files changed

+142
-8
lines changed

projects/angular-redux/src/lib/utils/Subscription.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { defaultNoopBatch as batch } from './batch'
2-
31
// encapsulates the subscription logic for connecting a component to the redux store, as
42
// well as nesting subscriptions of descendant components, so that we can ensure the
53
// ancestor components re-render before descendants
@@ -23,13 +21,11 @@ function createListenerCollection() {
2321
},
2422

2523
notify() {
26-
batch(() => {
2724
let listener = first
2825
while (listener) {
2926
listener.callback()
3027
listener = listener.next
3128
}
32-
})
3329
},
3430

3531
get() {

projects/angular-redux/src/lib/utils/batch.ts

Lines changed: 0 additions & 4 deletions
This file was deleted.

projects/angular-redux/src/public-api.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ export * from './lib/inject-store';
88
export * from './lib/provide-redux';
99
export * from "./lib/provider"
1010
export * from "./lib/utils/shallowEqual"
11+
export type { Subscription } from './lib/utils/Subscription'
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { createSubscription, Subscription } from '../../lib/utils/Subscription'
2+
import { Store } from 'redux'
3+
4+
describe('Subscription', () => {
5+
let notifications: string[]
6+
let store: Store
7+
let parent: Subscription
8+
9+
beforeEach(() => {
10+
notifications = []
11+
store = { subscribe: () => jest.fn() } as unknown as Store
12+
13+
parent = createSubscription(store)
14+
parent.onStateChange = () => {}
15+
parent.trySubscribe()
16+
})
17+
18+
function subscribeChild(name: string) {
19+
const child = createSubscription(store, parent)
20+
child.onStateChange = () => notifications.push(name)
21+
child.trySubscribe()
22+
return child
23+
}
24+
25+
it('listeners are notified in order', () => {
26+
subscribeChild('child1')
27+
subscribeChild('child2')
28+
subscribeChild('child3')
29+
subscribeChild('child4')
30+
31+
parent.notifyNestedSubs()
32+
33+
expect(notifications).toEqual(['child1', 'child2', 'child3', 'child4'])
34+
})
35+
36+
it('listeners can be unsubscribed', () => {
37+
const child1 = subscribeChild('child1')
38+
const child2 = subscribeChild('child2')
39+
const child3 = subscribeChild('child3')
40+
41+
child2.tryUnsubscribe()
42+
parent.notifyNestedSubs()
43+
44+
expect(notifications).toEqual(['child1', 'child3'])
45+
notifications.length = 0
46+
47+
child1.tryUnsubscribe()
48+
parent.notifyNestedSubs()
49+
50+
expect(notifications).toEqual(['child3'])
51+
notifications.length = 0
52+
53+
child3.tryUnsubscribe()
54+
parent.notifyNestedSubs()
55+
56+
expect(notifications).toEqual([])
57+
})
58+
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { shallowEqual } from '../../public-api'
2+
3+
describe('Utils', () => {
4+
describe('shallowEqual', () => {
5+
it('should return true if arguments fields are equal', () => {
6+
expect(
7+
shallowEqual(
8+
{ a: 1, b: 2, c: undefined },
9+
{ a: 1, b: 2, c: undefined },
10+
),
11+
).toBe(true)
12+
13+
expect(shallowEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2, c: 3 })).toBe(
14+
true,
15+
)
16+
17+
const o = {}
18+
expect(shallowEqual({ a: 1, b: 2, c: o }, { a: 1, b: 2, c: o })).toBe(
19+
true,
20+
)
21+
22+
const d = function () {
23+
return 1
24+
}
25+
expect(
26+
shallowEqual({ a: 1, b: 2, c: o, d }, { a: 1, b: 2, c: o, d }),
27+
).toBe(true)
28+
})
29+
30+
it('should return false if arguments fields are different function identities', () => {
31+
expect(
32+
shallowEqual(
33+
{
34+
a: 1,
35+
b: 2,
36+
d: function () {
37+
return 1
38+
},
39+
},
40+
{
41+
a: 1,
42+
b: 2,
43+
d: function () {
44+
return 1
45+
},
46+
},
47+
),
48+
).toBe(false)
49+
})
50+
51+
it('should return false if first argument has too many keys', () => {
52+
expect(shallowEqual({ a: 1, b: 2, c: 3 }, { a: 1, b: 2 })).toBe(false)
53+
})
54+
55+
it('should return false if second argument has too many keys', () => {
56+
expect(shallowEqual({ a: 1, b: 2 }, { a: 1, b: 2, c: 3 })).toBe(false)
57+
})
58+
59+
it('should return false if arguments have different keys', () => {
60+
expect(
61+
shallowEqual(
62+
{ a: 1, b: 2, c: undefined },
63+
{ a: 1, bb: 2, c: undefined },
64+
),
65+
).toBe(false)
66+
})
67+
68+
it('should compare two NaN values', () => {
69+
expect(shallowEqual(NaN, NaN)).toBe(true)
70+
})
71+
72+
it('should compare empty objects, with false', () => {
73+
expect(shallowEqual({}, false)).toBe(false)
74+
expect(shallowEqual(false, {})).toBe(false)
75+
expect(shallowEqual([], false)).toBe(false)
76+
expect(shallowEqual(false, [])).toBe(false)
77+
})
78+
79+
it('should compare two zero values', () => {
80+
expect(shallowEqual(0, 0)).toBe(true)
81+
})
82+
})
83+
})

0 commit comments

Comments
 (0)