-
Notifications
You must be signed in to change notification settings - Fork 304
Expand file tree
/
Copy pathHotTakeItem.spec.tsx
More file actions
89 lines (74 loc) · 2.84 KB
/
Copy pathHotTakeItem.spec.tsx
File metadata and controls
89 lines (74 loc) · 2.84 KB
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
import React from 'react';
import type { RenderResult } from '@testing-library/react';
import { render, screen } from '@testing-library/react';
import { QueryClient } from '@tanstack/react-query';
import type { HotTake } from '../../../../graphql/user/userHotTake';
import { HotTakeItem } from './HotTakeItem';
import { TestBootProvider } from '../../../../../__tests__/helpers/boot';
import loggedUser from '../../../../../__tests__/fixture/loggedUser';
import { useEngagementBarV2 } from '../../../../hooks/useEngagementBarV2';
import { useHotTakeShareEnabled } from '../../../../hooks/useHotTakeShareEnabled';
jest.mock('../../../../hooks/useEngagementBarV2', () => ({
useEngagementBarV2: jest.fn(),
}));
jest.mock('../../../../hooks/useHotTakeShareEnabled', () => ({
useHotTakeShareEnabled: jest.fn(),
}));
const engagementBarMock = useEngagementBarV2 as jest.Mock;
const shareEnabledMock = useHotTakeShareEnabled as jest.Mock;
const item: HotTake = {
id: 'take-1',
emoji: '🔥',
title: 'Small PRs or bust',
subtitle: 'Review time is a feature',
position: 1,
createdAt: '2026-01-01T00:00:00.000Z',
upvotes: 3,
upvoted: false,
};
const shareLabel = 'Share "Small PRs or bust"';
let client: QueryClient;
beforeEach(() => {
jest.clearAllMocks();
engagementBarMock.mockReturnValue(false);
shareEnabledMock.mockReturnValue(true);
client = new QueryClient({ defaultOptions: { queries: { retry: false } } });
});
const renderItem = (
props: Partial<Parameters<typeof HotTakeItem>[0]> = {},
): RenderResult =>
render(
<TestBootProvider client={client} auth={{ user: loggedUser }}>
<HotTakeItem
item={item}
isOwner={false}
ownerUsername="spicydev"
onUpvoteClick={jest.fn()}
{...props}
/>
</TestBootProvider>,
);
describe.each([
['V1', false],
['V2', true],
])('HotTakeItem %s share control', (_, isV2) => {
beforeEach(() => engagementBarMock.mockReturnValue(isV2));
it('renders exactly one share control next to the existing actions', () => {
renderItem();
expect(screen.getAllByLabelText(shareLabel)).toHaveLength(1);
// The upvote affordance is untouched.
expect(screen.getByLabelText(/upvote/i)).toBeInTheDocument();
});
it('renders no share control without an owner username', () => {
renderItem({ ownerUsername: undefined });
expect(screen.queryByLabelText(shareLabel)).not.toBeInTheDocument();
});
it('is byte-identical to the pre-share markup when the flag is off', () => {
shareEnabledMock.mockReturnValue(false);
const { container: flagOff } = renderItem();
// Rendering without the new prop is the exact markup this PR branched off.
const { container: baseline } = renderItem({ ownerUsername: undefined });
expect(screen.queryAllByLabelText(shareLabel)).toHaveLength(0);
expect(flagOff.innerHTML).toBe(baseline.innerHTML);
});
});