Skip to content

Commit 75b1930

Browse files
committed
wip: Properties tests - more mocking to get correct rendering in test to work
1 parent d623cc6 commit 75b1930

File tree

1 file changed

+83
-21
lines changed

1 file changed

+83
-21
lines changed

packages/webui/src/client/ui/UserEditOperations/__tests__/PropertiesPanel.test.tsx

Lines changed: 83 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,115 @@
1-
jest.mock(
2-
'../../../../__mocks__/tracker',
3-
() => ({
1+
jest.mock('../../../../__mocks__/tracker', () => {
2+
interface TrackerComputation {
3+
stop: () => void
4+
_recompute: () => void
5+
invalidate: () => void
6+
onInvalidate: () => void
7+
}
8+
const computations = new Set<TrackerComputation>()
9+
10+
return {
411
setup: () => ({
512
Tracker: {
613
autorun: jest.fn((fn) => {
7-
fn()
8-
return {
14+
const computation = {
915
stop: jest.fn(),
16+
_recompute: () => fn(computation),
17+
invalidate: function () {
18+
this._recompute()
19+
},
20+
onInvalidate: jest.fn(),
1021
}
22+
computations.add(computation)
23+
fn(computation)
24+
return computation
1125
}),
1226
nonreactive: jest.fn((fn) => fn()),
1327
active: false,
1428
currentComputation: null,
15-
Dependency: jest.fn().mockImplementation(() => ({
16-
depend: jest.fn(),
17-
changed: jest.fn(),
18-
hasDependents: jest.fn(),
19-
})),
29+
afterFlush: (fn: () => void) => {
30+
setTimeout(fn, 0)
31+
},
32+
flush: () => {
33+
computations.forEach((comp) => comp._recompute())
34+
},
35+
Dependency: jest.fn().mockImplementation(() => {
36+
const dependents = new Set<TrackerComputation>()
37+
return {
38+
depend: jest.fn(() => {
39+
if (Tracker.currentComputation) {
40+
dependents.add(Tracker.currentComputation as any as TrackerComputation)
41+
}
42+
}),
43+
changed: jest.fn(() => {
44+
dependents.forEach((comp) => comp.invalidate())
45+
}),
46+
hasDependents: jest.fn(() => dependents.size > 0),
47+
}
48+
}),
2049
},
2150
}),
22-
}),
23-
{
24-
virtual: true,
2551
}
26-
)
52+
})
2753

2854
// Mock the ReactiveDataHelper:
2955
jest.mock('../../../lib/reactiveData/ReactiveDataHelper', () => {
56+
interface MockSubscription {
57+
stop: () => void
58+
ready: () => boolean
59+
}
60+
3061
class MockReactiveDataHelper {
31-
protected _subs: Array<{ stop: () => void }> = []
62+
protected _subs: MockSubscription[] = []
63+
protected _computations: any[] = []
3264

33-
protected subscribe() {
34-
const sub = { stop: jest.fn() }
65+
protected subscribe(_name: string, ..._args: any[]): MockSubscription {
66+
const sub: MockSubscription = {
67+
stop: jest.fn(),
68+
ready: jest.fn().mockReturnValue(true),
69+
}
3570
this._subs.push(sub)
3671
return sub
3772
}
3873

3974
protected autorun(f: () => void) {
75+
// Execute the function immediately
4076
f()
41-
return { stop: jest.fn() }
77+
const computation = {
78+
stop: jest.fn(),
79+
_recompute: () => f(),
80+
invalidate: function () {
81+
this._recompute()
82+
},
83+
onInvalidate: jest.fn(),
84+
}
85+
this._computations.push(computation)
86+
return computation
4287
}
4388

4489
destroy() {
4590
this._subs.forEach((sub) => sub.stop())
91+
this._computations.forEach((comp) => comp.stop())
4692
this._subs = []
93+
this._computations = []
4794
}
4895
}
4996

5097
class MockWithManagedTracker extends MockReactiveDataHelper {
5198
constructor() {
5299
super()
53100
}
101+
102+
triggerUpdate() {
103+
this._computations.forEach((comp) => comp.invalidate())
104+
}
54105
}
55106

56107
return {
57108
__esModule: true,
58109
WithManagedTracker: MockWithManagedTracker,
59110
meteorSubscribe: jest.fn().mockReturnValue({
60111
stop: jest.fn(),
112+
ready: jest.fn().mockReturnValue(true),
61113
}),
62114
}
63115
})
@@ -114,6 +166,8 @@ import { SelectedElementProvider, useSelection } from '../../RundownView/Selecte
114166
import { MongoMock } from '../../../../__mocks__/mongo'
115167
import { PropertiesPanel } from '../PropertiesPanel'
116168
import { UserAction } from '../../../lib/clientUserAction'
169+
import { SegmentId } from '@sofie-automation/corelib/dist/dataModel/Ids'
170+
import { Tracker } from 'meteor/tracker'
117171

118172
const mockSegmentsCollection = MongoMock.getInnerMockCollection(Segments)
119173
const mockPartsCollection = MongoMock.getInnerMockCollection(UIParts)
@@ -203,20 +257,28 @@ describe('PropertiesPanel', () => {
203257
test('renders segment properties when segment is selected', async () => {
204258
const mockSegment = createMockSegment('segment1')
205259

206-
mockSegmentsCollection.insert(mockSegment)
260+
const mockId = mockSegmentsCollection.insert(mockSegment) as any as SegmentId
261+
262+
const verifySegment = mockSegmentsCollection.findOne({ _id: mockId })
263+
expect(verifySegment).toBeTruthy()
264+
console.log('Verify segment :', verifySegment?._id)
207265

208-
expect(mockSegmentsCollection.findOne({ _id: mockSegment._id })).toBeTruthy()
266+
expect(mockSegmentsCollection.findOne({ _id: mockId })).toBeTruthy()
209267

210268
const { result } = renderHook(() => useSelection(), { wrapper })
211269

212270
// Update selection and wait for component to update
213271
await act(async () => {
214272
result.current.clearAndSetSelection({
215273
type: 'segment',
216-
elementId: mockSegment._id,
274+
elementId: mockId,
217275
})
218276
})
219277

278+
await act(async () => {
279+
jest.advanceTimersByTime(100)
280+
})
281+
220282
// Open component after segment is selected (as used in rundownview)
221283
const { container } = render(<PropertiesPanel />, { wrapper })
222284

0 commit comments

Comments
 (0)