-
Notifications
You must be signed in to change notification settings - Fork 10
/
debug.test.tsx
135 lines (119 loc) · 2.97 KB
/
debug.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React from 'react';
import { initStore, collect, WithStoreProp } from '../..';
import * as testUtils from '../testUtils';
it('should do the right thing', () => {
initStore({
prop1: 'Prop 1',
prop2: 'Prop 2',
tasks: [
{
id: 0,
name: 'Task 0',
},
{
id: 1,
name: 'Task 1',
},
],
});
type TaskProps = WithStoreProp & {
taskId: number;
};
const Task = ({ store, taskId }: TaskProps) => {
if (!store.tasks) return null;
const task = store.tasks.find(({ id }) => id === taskId);
if (!task) return null;
return (
<div>
<p>{store.prop2}</p>
<p>{task.name}</p>
</div>
);
};
const TaskCollected = collect(Task);
const TaskList = ({ store }: WithStoreProp) => (
<div>
<p>{store.prop1}</p>
{store.tasks &&
store.tasks.map((task) => (
<TaskCollected key={task.id} taskId={task.id} />
))}
</div>
);
testUtils.collectAndRenderStrict(TaskList);
expect(window.__RR__.getListenersByComponent()).toEqual({
Task: [
'tasks',
'tasks.0',
'tasks.0.id',
'tasks.1',
'tasks.1.id',
'prop2',
'tasks.0.name',
'tasks.1.name',
],
TaskList: [
'prop1',
'tasks',
'tasks.0',
'tasks.0.id',
'tasks.1',
'tasks.1.id',
],
});
// Filter for the task list only
expect(window.__RR__.getListenersByComponent(/TaskList/)).toEqual({
TaskList: [
'prop1',
'tasks',
'tasks.0',
'tasks.0.id',
'tasks.1',
'tasks.1.id',
],
});
// Put the ID in the key and filter for a specific instance
expect(
window.__RR__.getListenersByComponent(
'Task0',
(props: TaskProps) => props.taskId
)
).toEqual({
Task0: ['tasks', 'tasks.0', 'tasks.0.id', 'prop2', 'tasks.0.name'],
});
expect(window.__RR__.getComponentsByListener()).toEqual({
tasks: ['TaskList', 'Task'],
'tasks.0': ['TaskList', 'Task'],
'tasks.0.id': ['TaskList', 'Task'],
'tasks.1': ['TaskList', 'Task'],
'tasks.1.id': ['TaskList', 'Task'],
prop2: ['Task'],
'tasks.0.name': ['Task'],
'tasks.1.name': ['Task'],
prop1: ['TaskList'],
});
// Which components _instances_ listen to `tasks.0.id`?
// Note that Task1 listens too because it loops over all tasks
expect(
window.__RR__.getComponentsByListener(
'tasks.0.id',
(props: TaskProps) => props.taskId
)['tasks.0.id']
).toEqual(['TaskList', 'Task0', 'Task1']);
expect(
window.__RR__.getComponentsByListener(
null,
(props: TaskProps) => props.taskId
)
).toEqual({
tasks: ['TaskList', 'Task0', 'Task1'],
'tasks.0': ['TaskList', 'Task0', 'Task1'],
'tasks.0.id': ['TaskList', 'Task0', 'Task1'],
'tasks.1': ['TaskList', 'Task1'],
'tasks.1.id': ['TaskList', 'Task1'],
prop2: ['Task0', 'Task1'],
'tasks.0.name': ['Task0'],
'tasks.1.name': ['Task1'],
prop1: ['TaskList'],
});
});