-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathappController.spec.ts
More file actions
304 lines (257 loc) · 11.4 KB
/
appController.spec.ts
File metadata and controls
304 lines (257 loc) · 11.4 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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import appController from './appController';
import http from '../helpers/httpClient';
import { fetchOmdbData, fetchAndUpdatePosters, getSeriesDetail } from '../helpers/appHelper';
import History from '../models/History';
import { getLatest, setLatest, invalidateLatest } from '../helpers/cache';
jest.mock('../helpers/httpClient', () => ({
__esModule: true,
default: { get: jest.fn() },
}));
jest.mock('../helpers/appHelper', () => {
const actual = jest.requireActual('../helpers/appHelper');
return {
...actual,
fetchOmdbData: jest.fn(),
fetchAndUpdatePosters: jest.fn(),
getSeriesDetail: jest.fn(),
};
});
jest.mock('../helpers/cache', () => ({
getLatest: jest.fn(),
setLatest: jest.fn(),
invalidateLatest: jest.fn(),
}));
jest.mock('../models/History', () => ({
__esModule: true,
default: {
findOne: jest.fn(),
findOneAndUpdate: jest.fn(),
},
}));
jest.mock('../config/app', () => ({
VIDSRC_DOMAIN: 'domain',
MULTI_DOMAIN: undefined,
APP_URL: 'http://app',
APP_NAME: 'name',
APP_SUBTITLE: '',
APP_DESCRIPTION: '',
}));
describe('controllers/appController', () => {
beforeEach(() => {
jest.clearAllMocks();
(getSeriesDetail as jest.Mock).mockResolvedValue({
totalSeasons: 1,
currentSeason: { season: 1, episodes: [{ episode: 1, title: 'E1' }] },
});
});
test('getHome renders index with movies and series', async () => {
(http.get as jest.Mock)
.mockResolvedValueOnce({ data: { result: [{ imdb_id: '1' }] } })
.mockResolvedValueOnce({ data: { result: [{ imdb_id: '2' }] } });
(fetchAndUpdatePosters as jest.Mock).mockResolvedValue(undefined);
(getLatest as jest.Mock).mockReturnValue(undefined);
const req: any = { query: {}, user: { id: 1 } };
const res: any = { locals: { APP_URL: 'http://app', CARD_TYPE: 'card' }, render: jest.fn() };
await appController.getHome(req, res, jest.fn());
expect(http.get).toHaveBeenCalledTimes(2);
expect(fetchAndUpdatePosters).toHaveBeenCalledTimes(2);
expect(setLatest).toHaveBeenCalledWith({ movies: [{ imdb_id: '1' }], series: [{ imdb_id: '2' }] });
expect(res.render).toHaveBeenCalledWith('index', expect.objectContaining({
newMovies: [{ imdb_id: '1' }],
newSeries: [{ imdb_id: '2' }],
card: 'card',
user: req.user,
}));
});
test('getHome handles empty results', async () => {
(http.get as jest.Mock)
.mockResolvedValueOnce({ data: {} })
.mockResolvedValueOnce({ data: {} });
(fetchAndUpdatePosters as jest.Mock).mockResolvedValue(undefined);
(getLatest as jest.Mock).mockReturnValue(undefined);
const req: any = { query: {}, user: {} };
const res: any = {
locals: { APP_URL: 'http://app', CARD_TYPE: 'card' },
render: jest.fn(),
};
await appController.getHome(req, res, jest.fn());
expect(res.render).toHaveBeenCalledWith('index', expect.objectContaining({
newMovies: [],
newSeries: [],
}));
});
test('getHome returns cached results when available', async () => {
(getLatest as jest.Mock).mockReturnValue({
movies: [{ imdb_id: 'm1' }],
series: [{ imdb_id: 's1' }],
});
const req: any = { query: {}, user: {} };
const res: any = { locals: { APP_URL: 'http://app', CARD_TYPE: 'card' }, render: jest.fn() };
await appController.getHome(req, res, jest.fn());
expect(http.get).not.toHaveBeenCalled();
expect(fetchAndUpdatePosters).not.toHaveBeenCalled();
expect(res.render).toHaveBeenCalledWith('index', expect.objectContaining({
newMovies: [{ imdb_id: 'm1' }],
newSeries: [{ imdb_id: 's1' }],
}));
});
test('clearCache invalidates cache', async () => {
const req: any = {};
const res: any = { json: jest.fn() };
await appController.clearCache(req, res, jest.fn());
expect(invalidateLatest).toHaveBeenCalled();
expect(res.json).toHaveBeenCalledWith({ cleared: true });
});
test('getView renders series view', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
const req: any = { params: { q: '', id: 'tt', type: 'series', season: '1', episode: '2' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(History.findOneAndUpdate).toHaveBeenCalledWith(
{ userId: 'u1', imdbId: 'tt' },
{ $set: { type: 'series', lastSeason: 1, lastEpisode: 2 } },
{ upsert: true }
);
expect(getSeriesDetail).toHaveBeenCalledWith('tt', 1);
expect(res.render).toHaveBeenCalledWith('view', expect.objectContaining({
season: '1',
episode: '2',
type: 'series',
iframeSrc: 'https://domain/embed/tv?imdb=tt&season=1&episode=2',
server1Src: 'https://domain/embed/tv?imdb=tt&season=1&episode=2',
server2Src: '',
currentServer: '1',
}));
});
test('getView defaults season and episode when missing', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOne as jest.Mock).mockResolvedValue(undefined);
const req: any = { params: { q: '', id: 'tt', type: 'series' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(res.redirect).not.toHaveBeenCalled();
expect(History.findOneAndUpdate).toHaveBeenCalledWith(
{ userId: 'u1', imdbId: 'tt' },
{ $set: { type: 'series', lastSeason: 1, lastEpisode: 1 } },
{ upsert: true }
);
expect(res.render).toHaveBeenCalledWith('view', expect.objectContaining({
season: '1',
episode: '1',
}));
});
test('getView renders movie view', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOneAndUpdate as jest.Mock).mockResolvedValue({ watched: true });
const req: any = { params: { q: '', id: 'tt', type: 'movie' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(History.findOneAndUpdate).toHaveBeenCalledWith(
{ userId: 'u1', imdbId: 'tt' },
{ $set: { type: 'movie', watched: true } },
{ upsert: true, new: true }
);
expect(res.render).toHaveBeenCalledWith(
'view',
expect.objectContaining({
type: 'movie',
watched: true,
iframeSrc: 'https://domain/embed/movie/tt',
server1Src: 'https://domain/embed/movie/tt',
server2Src: '',
currentServer: '1',
})
);
});
test('getView handles missing history on movie view', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOneAndUpdate as jest.Mock).mockResolvedValue(null);
const req: any = { params: { q: '', id: 'tt', type: 'movie' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(res.render).toHaveBeenCalledWith(
'view',
expect.objectContaining({ type: 'movie', watched: false })
);
});
test('getView redirects to history position for series', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOne as jest.Mock).mockResolvedValue({ lastSeason: 5, lastEpisode: 11 });
const req: any = { params: { q: '', id: 'tt', type: 'series' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(res.redirect).toHaveBeenCalledWith('/view/tt/series/5/11');
expect(History.findOneAndUpdate).not.toHaveBeenCalled();
});
test('getView ignores malformed history and uses defaults', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOne as jest.Mock).mockResolvedValue({ lastSeason: 'abc', lastEpisode: null });
const req: any = { params: { q: '', id: 'tt', type: 'series' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(res.redirect).not.toHaveBeenCalled();
expect(History.findOneAndUpdate).toHaveBeenCalledWith(
{ userId: 'u1', imdbId: 'tt' },
{ $set: { type: 'series', lastSeason: 1, lastEpisode: 1 } },
{ upsert: true }
);
expect(res.render).toHaveBeenCalledWith(
'view',
expect.objectContaining({ season: '1', episode: '1' })
);
});
test('getView series without user does not query history', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
const req: any = { params: { q: '', id: 'tt', type: 'series' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(History.findOne).not.toHaveBeenCalled();
expect(History.findOneAndUpdate).not.toHaveBeenCalled();
expect(res.render).toHaveBeenCalledWith(
'view',
expect.objectContaining({ season: '1', episode: '1' })
);
});
test('getView propagates errors from History.findOne', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOne as jest.Mock).mockRejectedValue(new Error('fail'));
const req: any = { params: { q: '', id: 'tt', type: 'series' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn(), redirect: jest.fn() };
const next = jest.fn();
await appController.getView(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(Error));
});
test('getView propagates errors from History.findOneAndUpdate', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
(History.findOneAndUpdate as jest.Mock).mockRejectedValue(new Error('fail'));
const req: any = { params: { q: '', id: 'tt', type: 'movie' }, user: { id: 'u1' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn() };
const next = jest.fn();
await appController.getView(req, res, next);
expect(next).toHaveBeenCalledWith(expect.any(Error));
});
test('getView movie without user skips history', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({});
const req: any = { params: { q: '', id: 'tt', type: 'movie' } };
const res: any = { locals: { APP_URL: 'http://app' }, render: jest.fn() };
await appController.getView(req, res, jest.fn());
expect(History.findOneAndUpdate).not.toHaveBeenCalled();
});
test('getSearch redirects when query empty', async () => {
const req: any = { query: { q: ' ' }, user: {} };
const res: any = { locals: { APP_URL: 'http://app' }, redirect: jest.fn(), render: jest.fn() };
await appController.getSearch(req, res, jest.fn());
expect(res.redirect).toHaveBeenCalledWith('/');
});
test('getSearch renders search results', async () => {
(fetchOmdbData as jest.Mock).mockResolvedValue({ Search: [{ Title: 'A' }] });
const req: any = { query: { q: 'test', type: 'movie' }, user: {} };
const res: any = { locals: { APP_URL: 'http://app', CARD_TYPE: 'card' }, redirect: jest.fn(), render: jest.fn() };
await appController.getSearch(req, res, jest.fn());
expect(fetchOmdbData).toHaveBeenCalledWith('test', true, 'movie');
expect(res.render).toHaveBeenCalledWith('search', expect.objectContaining({
results: [{ Title: 'A' }],
card: 'card',
}));
});
});