-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathreschedule_draft.test.tsx
197 lines (157 loc) · 6.75 KB
/
reschedule_draft.test.tsx
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {act, fireEvent} from '@testing-library/react-native';
import moment from 'moment-timezone';
import React from 'react';
import {Navigation} from 'react-native-navigation';
import {updateScheduledPost} from '@actions/remote/scheduled_post';
import {Screens} from '@constants';
import {useServerUrl} from '@context/server';
import {dismissModal, setButtons} from '@screens/navigation';
import {renderWithEverything} from '@test/intl-test-helper';
import TestHelper from '@test/test_helper';
import RescheduledDraft from './reschedule_draft';
import type {Database} from '@nozbe/watermelondb';
import type ScheduledPostModel from '@typings/database/models/servers/scheduled_post';
jest.mock('@actions/remote/scheduled_post', () => ({
updateScheduledPost: jest.fn().mockResolvedValue({scheduledPost: {} as ScheduledPost, error: undefined}),
}));
jest.mock('@screens/navigation', () => ({
buildNavigationButton: jest.fn().mockReturnValue({
id: 'reschedule-draft',
testID: 'reschedule-draft.save.button',
showAsAction: 'always',
}),
dismissModal: jest.fn(),
setButtons: jest.fn(),
}));
jest.mock('react-native-navigation', () => {
const registerComponentListenerMock = jest.fn();
return {
Navigation: {
events: () => ({
registerComponentListener: registerComponentListenerMock,
}),
},
};
});
jest.mock('@context/server', () => ({
useServerUrl: jest.fn(),
}));
const SERVER_URL = 'https://appv1.mattermost.com';
describe('RescheduledDraft', () => {
let database: Database;
const mockDraft = {
scheduledAt: moment().add(1, 'day').valueOf(),
toApi: jest.fn().mockResolvedValue({
scheduled_at: moment().add(1, 'day').valueOf(),
}),
} as unknown as ScheduledPostModel;
const baseProps = {
componentId: Screens.RESCHEDULE_DRAFT,
closeButtonId: 'close-button-id',
currentUserTimezone: {
useAutomaticTimezone: true,
automaticTimezone: 'America/New_York',
manualTimezone: '',
},
draft: mockDraft,
};
beforeAll(async () => {
const server = await TestHelper.setupServerDatabase();
database = server.database;
});
beforeEach(() => {
jest.mocked(useServerUrl).mockReturnValue(SERVER_URL);
});
it('should render correctly', () => {
const {getByTestId} = renderWithEverything(
<RescheduledDraft {...baseProps}/>, {database},
);
expect(getByTestId('edit_post.screen')).toBeTruthy();
});
it('should have navigation component registered on initialization', () => {
renderWithEverything(<RescheduledDraft {...baseProps}/>, {database});
// Verify navigation listener was registered
expect(Navigation.events().registerComponentListener).toHaveBeenCalledWith(
expect.any(Object),
baseProps.componentId,
);
// Check that the component registered with proper navigation handler
const registerCall = jest.mocked(Navigation.events().registerComponentListener).mock.calls[0][0];
expect(registerCall).toBeDefined();
expect(registerCall.navigationButtonPressed).toBeDefined();
});
it('Should enable save button when data changes', async () => {
jest.mocked(updateScheduledPost).mockResolvedValue({scheduledPost: {} as ScheduledPost, error: undefined});
const {getByTestId} = renderWithEverything(
<RescheduledDraft {...baseProps}/>,
{database},
);
const dateTimeSelector = getByTestId('custom_date_time_picker'); // Ensure testID is set in the component
expect(dateTimeSelector).toBeTruthy();
const newDate = moment().add(2, 'days');
await act(async () => {
fireEvent(dateTimeSelector, 'handleChange', newDate);
});
expect(setButtons).toHaveBeenCalled();
// Use jest.mocked for proper type inference
const setButtonsMock = jest.mocked(setButtons);
const mockCalls = setButtonsMock.mock.calls;
const lastCallIndex = mockCalls.length - 1;
const setButtonsCall = mockCalls[lastCallIndex]?.[1]; // Ensure lastCallIndex exists
expect(setButtonsCall).toBeDefined();
expect(setButtonsCall?.rightButtons).toBeDefined();
expect(setButtonsCall?.rightButtons?.length).toBeGreaterThan(0);
const saveButton = setButtonsCall?.rightButtons?.[0];
expect(saveButton?.enabled).toBeTruthy();
});
it('should call Navigation event when save button is pressed', async () => {
const {getByTestId} = renderWithEverything(
<RescheduledDraft {...baseProps}/>,
{database},
);
const dateTimeSelector = getByTestId('custom_date_time_picker'); // Ensure testID is set in the component
expect(dateTimeSelector).toBeTruthy();
const newDate = moment().add(2, 'days');
await act(async () => {
fireEvent(dateTimeSelector, 'handleChange', newDate);
});
// Verify navigation listener was registered
expect(Navigation.events().registerComponentListener).toHaveBeenCalledWith(
expect.any(Object),
baseProps.componentId,
);
// Get the navigationButtonPressed handler
const functionToCall = jest.mocked(Navigation.events().registerComponentListener).mock.calls[1][0].navigationButtonPressed;
// Simulate pressing the save button
await act(async () => {
functionToCall?.({
buttonId: 'reschedule-draft',
componentId: '',
});
});
expect(dismissModal).toHaveBeenCalledWith({componentId: baseProps.componentId});
});
it('should dismiss modal when close button is pressed', async () => {
renderWithEverything(
<RescheduledDraft {...baseProps}/>, {database},
);
// Verify navigation listener was registered
expect(Navigation.events().registerComponentListener).toHaveBeenCalledWith(
expect.any(Object),
baseProps.componentId,
);
// Get the navigationButtonPressed handler
const functionToCall = jest.mocked(Navigation.events().registerComponentListener).mock.calls[0][0].navigationButtonPressed;
// Simulate pressing the close button
await act(async () => {
functionToCall?.({
buttonId: baseProps.closeButtonId,
componentId: '',
});
});
// Verify dismissModal was called
expect(dismissModal).toHaveBeenCalledWith({componentId: baseProps.componentId});
});
});