-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathSmartComposerModal.spec.tsx
More file actions
158 lines (133 loc) · 4.33 KB
/
SmartComposerModal.spec.tsx
File metadata and controls
158 lines (133 loc) · 4.33 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import React from 'react';
import { render, screen } from '@testing-library/react';
import { useAuthContext } from '../../../contexts/AuthContext';
import { useLogContext } from '../../../contexts/LogContext';
import { useViewSize, ViewSize } from '../../../hooks';
import { usePrompt } from '../../../hooks/usePrompt';
import { useNotificationToggle } from '../../../hooks/notifications';
import { useComposerAudience } from '../../post/composer/useComposerAudience';
import { useComposerSubmit } from '../../post/composer/useComposerSubmit';
import { SmartComposerModal } from './SmartComposerModal';
jest.mock('../../../contexts/AuthContext', () => ({
useAuthContext: jest.fn(),
}));
jest.mock('../../../contexts/LogContext', () => ({
useLogContext: jest.fn(),
}));
jest.mock('../../../hooks', () => {
const actual = jest.requireActual('../../../hooks');
return {
...actual,
useViewSize: jest.fn(),
};
});
jest.mock('../../../hooks/usePrompt', () => ({
usePrompt: jest.fn(),
}));
jest.mock('../../../hooks/notifications', () => ({
useNotificationToggle: jest.fn(),
}));
jest.mock('../../post/composer/useComposerAudience', () => ({
isUserAudience: jest.fn(() => false),
useComposerAudience: jest.fn(),
}));
jest.mock('../../post/composer/useComposerSubmit', () => ({
useComposerSubmit: jest.fn(),
}));
jest.mock('../../post/composer/AudienceChip', () => ({
AudienceChip: () => null,
}));
jest.mock('../../post/composer/KindModePicker', () => ({
KindModePicker: () => null,
}));
jest.mock('../../post/composer/LinkForm', () => ({
LinkForm: () => null,
}));
jest.mock('../../post/composer/PollForm', () => ({
PollForm: () => null,
}));
jest.mock('../../post/composer/TextForm', () => ({
TextForm: () => null,
}));
jest.mock('../../tooltip/Tooltip', () => ({
Tooltip: ({ children }: React.PropsWithChildren) => children,
}));
jest.mock('../common/Modal', () => ({
Modal: ({ children }: React.PropsWithChildren) => <div>{children}</div>,
}));
describe('SmartComposerModal', () => {
const logEvent = jest.fn();
const showPrompt = jest.fn();
const onSubmitted = jest.fn();
const onRequestClose = jest.fn();
beforeEach(() => {
jest.clearAllMocks();
jest.mocked(useAuthContext).mockReturnValue({
user: null,
} as unknown as ReturnType<typeof useAuthContext>);
jest.mocked(useLogContext).mockReturnValue({
logEvent,
} as unknown as ReturnType<typeof useLogContext>);
jest
.mocked(useViewSize)
.mockImplementation((size) => size === ViewSize.Laptop);
jest.mocked(usePrompt).mockReturnValue({
showPrompt,
} as unknown as ReturnType<typeof usePrompt>);
jest.mocked(useNotificationToggle).mockReturnValue({
shouldShowCta: true,
isEnabled: true,
onToggle: jest.fn(),
onSubmitted,
} as unknown as ReturnType<typeof useNotificationToggle>);
jest.mocked(useComposerAudience).mockReturnValue({
audiences: [],
selectedIds: ['squad-1'],
selected: [
{
id: 'squad-1',
name: 'Squad',
handle: 'squad',
},
],
setSelectedIds: jest.fn(),
userAudienceId: 'user-1',
} as unknown as ReturnType<typeof useComposerAudience>);
jest.mocked(useComposerSubmit).mockReturnValue({
handleSubmit: jest.fn(),
isSubmitDisabled: false,
isInFlight: false,
preview: { url: 'https://daily.dev', title: 'daily.dev' },
isLoadingPreview: false,
fetchPreview: jest.fn(),
});
});
it('keeps the production notification CTA visible in the composer', () => {
render(
<SmartComposerModal
isOpen
initialUrl="https://daily.dev"
onRequestClose={onRequestClose}
/>,
);
expect(
screen.getByText(
'Receive updates whenever your Squad members engage with your post',
),
).toBeInTheDocument();
});
it('runs notification submit handling before closing after a successful post', async () => {
render(
<SmartComposerModal
isOpen
initialUrl="https://daily.dev"
onRequestClose={onRequestClose}
/>,
);
const hookProps = jest.mocked(useComposerSubmit).mock.calls[0]?.[0];
expect(hookProps?.onComplete).toBeDefined();
await hookProps?.onComplete();
expect(onSubmitted).toHaveBeenCalledTimes(1);
expect(onRequestClose).toHaveBeenCalledTimes(1);
});
});