Skip to content

Commit a32aa94

Browse files
committed
Fix TypeScript errors in shield-subscription.test.tsx
- Remove non-existent properties from `useSubscriptionEligibility` mock - Cast `useSubscriptionMetrics` mock to match expected return type - Use mutable ref object for `evaluateFn` to fix TS control flow narrowing
1 parent a6e6daa commit a32aa94

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

ui/contexts/shield/shield-subscription.test.tsx

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ jest.mock('../../hooks/subscription/useSubscription');
1818
jest.mock('../../hooks/shield/metrics/useSubscriptionMetrics');
1919
jest.mock('../../store/actions', () => ({
2020
assignUserToCohort: jest.fn(),
21+
setPendingShieldCohort: jest.fn(),
2122
setShowShieldEntryModalOnce: jest.fn(),
2223
subscriptionsStartPolling: jest.fn(),
2324
}));
@@ -59,17 +60,14 @@ describe('ShieldSubscriptionProvider', () => {
5960
// Mock hooks
6061
jest.spyOn(useSubscription, 'useSubscriptionEligibility').mockReturnValue({
6162
getSubscriptionEligibility: mockGetSubscriptionEligibility,
62-
isLoading: false,
63-
error: null,
64-
data: null,
6563
});
6664

6765
jest
6866
.spyOn(useSubscriptionMetrics, 'useSubscriptionMetrics')
6967
.mockReturnValue({
7068
captureShieldEligibilityCohortEvent:
7169
mockCaptureShieldEligibilityCohortEvent,
72-
});
70+
} as unknown as ReturnType<typeof useSubscriptionMetrics.useSubscriptionMetrics>);
7371
});
7472

7573
it('renders children correctly', () => {
@@ -180,12 +178,13 @@ describe('ShieldSubscriptionProvider', () => {
180178
modalType: 'entry',
181179
});
182180

183-
let evaluateFn: ((cohort: string) => Promise<void>) | null = null;
181+
const evaluateFnRef: {
182+
current: ((cohort: string) => Promise<void>) | null;
183+
} = { current: null };
184184

185185
const TestConsumer = () => {
186186
const { evaluateCohortEligibility } = useShieldSubscriptionContext();
187-
// eslint-disable-next-line react-compiler/react-compiler
188-
evaluateFn = evaluateCohortEligibility;
187+
evaluateFnRef.current = evaluateCohortEligibility;
189188
return <div data-testid="consumer">Consumer</div>;
190189
};
191190

@@ -195,16 +194,14 @@ describe('ShieldSubscriptionProvider', () => {
195194
</ShieldSubscriptionProvider>,
196195
);
197196

198-
// Call the function after render
199-
await evaluateFn?.('wallet_home');
197+
await evaluateFnRef.current?.('wallet_home');
200198

201199
await waitFor(() => {
202200
expect(mockGetSubscriptionEligibility).toHaveBeenCalled();
203201
});
204202
});
205203

206204
it('accesses current values even with stable callback', async () => {
207-
// Initially return false for basic functionality
208205
let isBasicFunctionalityEnabled = false;
209206

210207
jest.spyOn(redux, 'useSelector').mockImplementation((selector) => {
@@ -237,12 +234,13 @@ describe('ShieldSubscriptionProvider', () => {
237234
modalType: 'entry',
238235
});
239236

240-
let evaluateFn: ((cohort: string) => Promise<void>) | null = null;
237+
const evaluateFnRef: {
238+
current: ((cohort: string) => Promise<void>) | null;
239+
} = { current: null };
241240

242241
const TestConsumer = () => {
243242
const { evaluateCohortEligibility } = useShieldSubscriptionContext();
244-
// eslint-disable-next-line react-compiler/react-compiler
245-
evaluateFn = evaluateCohortEligibility;
243+
evaluateFnRef.current = evaluateCohortEligibility;
246244
return <div data-testid="consumer">Consumer</div>;
247245
};
248246

@@ -252,28 +250,22 @@ describe('ShieldSubscriptionProvider', () => {
252250
</ShieldSubscriptionProvider>,
253251
);
254252

255-
// Call with basic functionality disabled
256-
await evaluateFn?.('wallet_home');
253+
await evaluateFnRef.current?.('wallet_home');
257254

258-
// Should not call getSubscriptionEligibility when basic functionality is disabled
259255
await waitFor(() => {
260256
expect(mockGetSubscriptionEligibility).not.toHaveBeenCalled();
261257
});
262258

263-
// Enable basic functionality
264259
isBasicFunctionalityEnabled = true;
265260

266-
// Force re-render with updated state
267261
rerender(
268262
<ShieldSubscriptionProvider>
269263
<TestConsumer />
270264
</ShieldSubscriptionProvider>,
271265
);
272266

273-
// Call again with basic functionality enabled
274-
await evaluateFn?.('wallet_home');
267+
await evaluateFnRef.current?.('wallet_home');
275268

276-
// Now it should call getSubscriptionEligibility
277269
await waitFor(
278270
() => {
279271
expect(mockGetSubscriptionEligibility).toHaveBeenCalled();

0 commit comments

Comments
 (0)