|
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 { |
4 | 11 | setup: () => ({ |
5 | 12 | Tracker: { |
6 | 13 | autorun: jest.fn((fn) => { |
7 | | - fn() |
8 | | - return { |
| 14 | + const computation = { |
9 | 15 | stop: jest.fn(), |
| 16 | + _recompute: () => fn(computation), |
| 17 | + invalidate: function () { |
| 18 | + this._recompute() |
| 19 | + }, |
| 20 | + onInvalidate: jest.fn(), |
10 | 21 | } |
| 22 | + computations.add(computation) |
| 23 | + fn(computation) |
| 24 | + return computation |
11 | 25 | }), |
12 | 26 | nonreactive: jest.fn((fn) => fn()), |
13 | 27 | active: false, |
14 | 28 | 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 | + }), |
20 | 49 | }, |
21 | 50 | }), |
22 | | - }), |
23 | | - { |
24 | | - virtual: true, |
25 | 51 | } |
26 | | -) |
| 52 | +}) |
27 | 53 |
|
28 | 54 | // Mock the ReactiveDataHelper: |
29 | 55 | jest.mock('../../../lib/reactiveData/ReactiveDataHelper', () => { |
| 56 | + interface MockSubscription { |
| 57 | + stop: () => void |
| 58 | + ready: () => boolean |
| 59 | + } |
| 60 | + |
30 | 61 | class MockReactiveDataHelper { |
31 | | - protected _subs: Array<{ stop: () => void }> = [] |
| 62 | + protected _subs: MockSubscription[] = [] |
| 63 | + protected _computations: any[] = [] |
32 | 64 |
|
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 | + } |
35 | 70 | this._subs.push(sub) |
36 | 71 | return sub |
37 | 72 | } |
38 | 73 |
|
39 | 74 | protected autorun(f: () => void) { |
| 75 | + // Execute the function immediately |
40 | 76 | 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 |
42 | 87 | } |
43 | 88 |
|
44 | 89 | destroy() { |
45 | 90 | this._subs.forEach((sub) => sub.stop()) |
| 91 | + this._computations.forEach((comp) => comp.stop()) |
46 | 92 | this._subs = [] |
| 93 | + this._computations = [] |
47 | 94 | } |
48 | 95 | } |
49 | 96 |
|
50 | 97 | class MockWithManagedTracker extends MockReactiveDataHelper { |
51 | 98 | constructor() { |
52 | 99 | super() |
53 | 100 | } |
| 101 | + |
| 102 | + triggerUpdate() { |
| 103 | + this._computations.forEach((comp) => comp.invalidate()) |
| 104 | + } |
54 | 105 | } |
55 | 106 |
|
56 | 107 | return { |
57 | 108 | __esModule: true, |
58 | 109 | WithManagedTracker: MockWithManagedTracker, |
59 | 110 | meteorSubscribe: jest.fn().mockReturnValue({ |
60 | 111 | stop: jest.fn(), |
| 112 | + ready: jest.fn().mockReturnValue(true), |
61 | 113 | }), |
62 | 114 | } |
63 | 115 | }) |
@@ -114,6 +166,8 @@ import { SelectedElementProvider, useSelection } from '../../RundownView/Selecte |
114 | 166 | import { MongoMock } from '../../../../__mocks__/mongo' |
115 | 167 | import { PropertiesPanel } from '../PropertiesPanel' |
116 | 168 | import { UserAction } from '../../../lib/clientUserAction' |
| 169 | +import { SegmentId } from '@sofie-automation/corelib/dist/dataModel/Ids' |
| 170 | +import { Tracker } from 'meteor/tracker' |
117 | 171 |
|
118 | 172 | const mockSegmentsCollection = MongoMock.getInnerMockCollection(Segments) |
119 | 173 | const mockPartsCollection = MongoMock.getInnerMockCollection(UIParts) |
@@ -203,20 +257,28 @@ describe('PropertiesPanel', () => { |
203 | 257 | test('renders segment properties when segment is selected', async () => { |
204 | 258 | const mockSegment = createMockSegment('segment1') |
205 | 259 |
|
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) |
207 | 265 |
|
208 | | - expect(mockSegmentsCollection.findOne({ _id: mockSegment._id })).toBeTruthy() |
| 266 | + expect(mockSegmentsCollection.findOne({ _id: mockId })).toBeTruthy() |
209 | 267 |
|
210 | 268 | const { result } = renderHook(() => useSelection(), { wrapper }) |
211 | 269 |
|
212 | 270 | // Update selection and wait for component to update |
213 | 271 | await act(async () => { |
214 | 272 | result.current.clearAndSetSelection({ |
215 | 273 | type: 'segment', |
216 | | - elementId: mockSegment._id, |
| 274 | + elementId: mockId, |
217 | 275 | }) |
218 | 276 | }) |
219 | 277 |
|
| 278 | + await act(async () => { |
| 279 | + jest.advanceTimersByTime(100) |
| 280 | + }) |
| 281 | + |
220 | 282 | // Open component after segment is selected (as used in rundownview) |
221 | 283 | const { container } = render(<PropertiesPanel />, { wrapper }) |
222 | 284 |
|
|
0 commit comments