124 lines
3.2 KiB
JavaScript
124 lines
3.2 KiB
JavaScript
const test = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
|
|
const registerPlaylistRoutes = require('../src/web/routes/admin/playlists');
|
|
|
|
function createAppAndHandlers() {
|
|
const handlers = {};
|
|
const app = {
|
|
post(path, ...routeHandlers) {
|
|
handlers[path] = routeHandlers;
|
|
},
|
|
get() {}
|
|
};
|
|
|
|
return { app, handlers };
|
|
}
|
|
|
|
function createResponse() {
|
|
return {
|
|
redirectedTo: '',
|
|
statusCode: 200,
|
|
redirect(url) {
|
|
this.redirectedTo = url;
|
|
},
|
|
status(code) {
|
|
this.statusCode = code;
|
|
return this;
|
|
},
|
|
send() {
|
|
return this;
|
|
}
|
|
};
|
|
}
|
|
|
|
test('playlist create and update redirect to the canonical edit path', async () => {
|
|
const calls = [];
|
|
const connection = {
|
|
async beginTransaction() {},
|
|
async commit() {},
|
|
async rollback() {},
|
|
release() {},
|
|
async query(sql) {
|
|
if (sql.startsWith('INSERT INTO c_playlists')) {
|
|
return [{ insertId: 4 }];
|
|
}
|
|
if (sql.startsWith('UPDATE c_playlists SET')) {
|
|
calls.push('update-playlist');
|
|
return [{ affectedRows: 1 }];
|
|
}
|
|
if (sql.startsWith('DELETE FROM c_playlist_slides')) {
|
|
calls.push('delete-slides');
|
|
return [{ affectedRows: 1 }];
|
|
}
|
|
return [[]];
|
|
}
|
|
};
|
|
|
|
const { app, handlers } = createAppAndHandlers();
|
|
registerPlaylistRoutes(app, {
|
|
pool: {
|
|
async getConnection() {
|
|
return connection;
|
|
}
|
|
},
|
|
common: {
|
|
async fetchDuplicateName() { return null; },
|
|
async fetchCanvasSizeById() { return { id: 7 }; },
|
|
async fetchPlaylistById() { return { id: 4, canvas_id: 7 }; }
|
|
},
|
|
pages: {},
|
|
fetchOrderedPlaylistSlides: async () => [],
|
|
fetchScreensByPlaylistId: async () => [],
|
|
fetchPlaylistCanvasId: async () => null,
|
|
readArrayField: () => [],
|
|
getAuditUserId: () => 12,
|
|
redirectAfterSave(req, res, url) {
|
|
calls.push({ kind: 'redirectAfterSave', url, path: req.path });
|
|
res.redirect(url);
|
|
},
|
|
notifyPlayerScreens: async () => {},
|
|
broadcastDashboardState: async () => {},
|
|
getPlaylistDeleteBlockMessage: async () => '',
|
|
requirePermission() {
|
|
return function (_req, _res, next) {
|
|
next();
|
|
};
|
|
}
|
|
});
|
|
|
|
const createHandlers = handlers['/playlists'];
|
|
const updateHandlers = handlers['/playlists/:id'];
|
|
assert.equal(Array.isArray(createHandlers), true);
|
|
assert.equal(Array.isArray(updateHandlers), true);
|
|
|
|
const createRes = createResponse();
|
|
await createHandlers[1]({
|
|
path: '/playlists',
|
|
body: {
|
|
name: 'Lunch Loop',
|
|
canvas_size_id: '7',
|
|
slide_id: []
|
|
}
|
|
}, createRes, () => {});
|
|
|
|
const updateRes = createResponse();
|
|
await updateHandlers[1]({
|
|
path: '/playlists/4',
|
|
params: { id: '4' },
|
|
body: {
|
|
name: 'Lunch Loop',
|
|
slide_id: []
|
|
}
|
|
}, updateRes, () => {});
|
|
|
|
assert.equal(createRes.redirectedTo, '/playlists/4/edit');
|
|
assert.equal(updateRes.redirectedTo, '/playlists/4/edit');
|
|
assert.deepEqual(calls, [
|
|
'delete-slides',
|
|
{ kind: 'redirectAfterSave', url: '/playlists/4/edit', path: '/playlists' },
|
|
'update-playlist',
|
|
'delete-slides',
|
|
{ kind: 'redirectAfterSave', url: '/playlists/4/edit', path: '/playlists/4' }
|
|
]);
|
|
}); |