feat: utils/actions unit tests

This commit is contained in:
Yiannis
2026-01-24 20:00:20 +02:00
parent 1c15880ae3
commit 08189191b5
10 changed files with 652 additions and 41 deletions

View File

@@ -0,0 +1,145 @@
import * as MediaPageActions from '../../../src/static/js/utils/actions/MediaPageActions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by MediaPageActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('MediaPageActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('loadMediaData', () => {
it('Should dispatch LOAD_MEDIA_DATA action', () => {
MediaPageActions.loadMediaData();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'LOAD_MEDIA_DATA' });
});
});
describe('likeMedia / dislikeMedia', () => {
it('Should dispatch LIKE_MEDIA action', () => {
MediaPageActions.likeMedia();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'LIKE_MEDIA' });
});
it('Should dispatch DISLIKE_MEDIA action', () => {
MediaPageActions.dislikeMedia();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'DISLIKE_MEDIA' });
});
});
describe('reportMedia', () => {
it('Should dispatch REPORT_MEDIA with empty string when description is undefined', () => {
MediaPageActions.reportMedia();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REPORT_MEDIA', reportDescription: '' });
});
// @todo: Revisit this behavior
it('Should dispatch REPORT_MEDIA with stripped description when provided', () => {
MediaPageActions.reportMedia(' some text ');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REPORT_MEDIA', reportDescription: 'sometext' });
});
// @todo: Revisit this behavior
it('Should remove all whitespace characters including newlines and tabs', () => {
MediaPageActions.reportMedia('\n\t spaced\ntext \t');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REPORT_MEDIA', reportDescription: 'spacedtext' });
});
});
describe('copyShareLink / copyEmbedMediaCode', () => {
it('Should dispatch COPY_SHARE_LINK carrying the provided input element', () => {
const inputElem = document.createElement('input');
MediaPageActions.copyShareLink(inputElem);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'COPY_SHARE_LINK', inputElement: inputElem });
});
it('Should dispatch COPY_EMBED_MEDIA_CODE carrying the provided textarea element', () => {
const textarea = document.createElement('textarea');
MediaPageActions.copyEmbedMediaCode(textarea);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'COPY_EMBED_MEDIA_CODE', inputElement: textarea });
});
});
describe('removeMedia', () => {
it('Should dispatch REMOVE_MEDIA action', () => {
MediaPageActions.removeMedia();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REMOVE_MEDIA' });
});
});
describe('comments', () => {
it('Should dispatch SUBMIT_COMMENT with provided text', () => {
const commentText = 'Nice one';
MediaPageActions.submitComment(commentText);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'SUBMIT_COMMENT', commentText });
});
it('Should dispatch DELETE_COMMENT with provided comment id', () => {
const commentId = 'c-123';
MediaPageActions.deleteComment(commentId);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'DELETE_COMMENT', commentId });
});
// @todo: Revisit this behavior
it('Should dispatch DELETE_COMMENT with numeric comment id', () => {
const commentId = 42;
MediaPageActions.deleteComment(commentId);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'DELETE_COMMENT', commentId });
});
});
describe('playlists', () => {
it('Should dispatch CREATE_PLAYLIST with provided data', () => {
const payload = { title: 'My list', description: 'Desc' };
MediaPageActions.createPlaylist(payload);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'CREATE_PLAYLIST', playlist_data: payload });
});
it('Should dispatch ADD_MEDIA_TO_PLAYLIST with ids', () => {
const playlist_id = 'pl-1';
const media_id = 'm-1';
MediaPageActions.addMediaToPlaylist(playlist_id, media_id);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'ADD_MEDIA_TO_PLAYLIST', playlist_id, media_id });
});
it('Should dispatch REMOVE_MEDIA_FROM_PLAYLIST with ids', () => {
const playlist_id = 'pl-1';
const media_id = 'm-1';
MediaPageActions.removeMediaFromPlaylist(playlist_id, media_id);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REMOVE_MEDIA_FROM_PLAYLIST', playlist_id, media_id });
});
it('Should dispatch APPEND_NEW_PLAYLIST with provided playlist data', () => {
const playlist_data = {
playlist_id: 'pl-2',
add_date: new Date('2020-01-01T00:00:00Z'),
description: 'Cool',
title: 'T',
media_list: ['a', 'b'],
};
MediaPageActions.addNewPlaylist(playlist_data);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'APPEND_NEW_PLAYLIST', playlist_data });
});
});
});
});

View File

@@ -0,0 +1,55 @@
import * as PageActions from '../../../src/static/js/utils/actions/PageActions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by PageActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('PageActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('initPage', () => {
it('Should dispatch INIT_PAGE with provided page string', () => {
PageActions.initPage('home');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'INIT_PAGE', page: 'home' });
});
// @todo: Revisit this behavior
it('Should dispatch INIT_PAGE with empty string', () => {
PageActions.initPage('');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'INIT_PAGE', page: '' });
});
});
describe('toggleMediaAutoPlay', () => {
it('Should dispatch TOGGLE_AUTO_PLAY action', () => {
PageActions.toggleMediaAutoPlay();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_AUTO_PLAY' });
});
});
describe('addNotification', () => {
it('Should dispatch ADD_NOTIFICATION with message and id', () => {
const notification = 'Saved!';
const notificationId = 'notif-1';
PageActions.addNotification(notification, notificationId);
expect(dispatch).toHaveBeenCalledWith({ type: 'ADD_NOTIFICATION', notification, notificationId });
});
// @todo: Revisit this behavior
it('Should dispatch ADD_NOTIFICATION with empty notification message', () => {
const notification = '';
const notificationId = 'id-empty';
PageActions.addNotification(notification, notificationId);
expect(dispatch).toHaveBeenCalledWith({ type: 'ADD_NOTIFICATION', notification, notificationId });
});
});
});
});

View File

@@ -0,0 +1,96 @@
import { PlaylistPageActions } from '../../../src/static/js/utils/actions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by PlaylistPageActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('PlaylistPageActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('loadPlaylistData', () => {
it('Should dispatch LOAD_PLAYLIST_DATA action', () => {
PlaylistPageActions.loadPlaylistData();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'LOAD_PLAYLIST_DATA' });
});
});
describe('toggleSave', () => {
it('Should dispatch TOGGLE_SAVE action', () => {
PlaylistPageActions.toggleSave();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_SAVE' });
});
});
describe('updatePlaylist', () => {
it('Should dispatch UPDATE_PLAYLIST with provided title and description', () => {
const payload = { title: 'My Playlist', description: 'A description' };
PlaylistPageActions.updatePlaylist(payload);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_PLAYLIST', playlist_data: payload });
});
// @todo: Revisit this behavior
it('Should dispatch UPDATE_PLAYLIST with empty strings for title and description', () => {
const payload = { title: '', description: '' };
PlaylistPageActions.updatePlaylist(payload);
expect(dispatch).toHaveBeenCalledWith({ type: 'UPDATE_PLAYLIST', playlist_data: payload });
});
});
describe('removePlaylist', () => {
it('Should dispatch REMOVE_PLAYLIST action', () => {
PlaylistPageActions.removePlaylist();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REMOVE_PLAYLIST' });
});
});
describe('removedMediaFromPlaylist', () => {
it('Should dispatch MEDIA_REMOVED_FROM_PLAYLIST with media and playlist ids', () => {
PlaylistPageActions.removedMediaFromPlaylist('m1', 'p1');
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({
type: 'MEDIA_REMOVED_FROM_PLAYLIST',
media_id: 'm1',
playlist_id: 'p1',
});
});
// @todo: Revisit this behavior
it('Should dispatch MEDIA_REMOVED_FROM_PLAYLIST with empty ids as strings', () => {
PlaylistPageActions.removedMediaFromPlaylist('', '');
expect(dispatch).toHaveBeenCalledWith({
type: 'MEDIA_REMOVED_FROM_PLAYLIST',
media_id: '',
playlist_id: '',
});
});
});
describe('reorderedMediaInPlaylist', () => {
it('Should dispatch PLAYLIST_MEDIA_REORDERED with provided array', () => {
const items = [
{ id: '1', url: '/1', thumbnail_url: '/t1' },
{ id: '2', url: '/2', thumbnail_url: '/t2' },
];
PlaylistPageActions.reorderedMediaInPlaylist(items);
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'PLAYLIST_MEDIA_REORDERED', playlist_media: items });
});
// @todo: Revisit this behavior
it('Should dispatch PLAYLIST_MEDIA_REORDERED with empty array for playlist media', () => {
const items: any[] = [];
PlaylistPageActions.reorderedMediaInPlaylist(items);
expect(dispatch).toHaveBeenCalledWith({ type: 'PLAYLIST_MEDIA_REORDERED', playlist_media: items });
});
});
});
});

View File

@@ -0,0 +1,39 @@
import { PlaylistViewActions } from '../../../src/static/js/utils/actions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by PlaylistViewActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('PlaylistViewActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('toggleLoop', () => {
it('Should dispatch TOGGLE_LOOP action', () => {
PlaylistViewActions.toggleLoop();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_LOOP' });
});
});
describe('toggleShuffle', () => {
it('Should dispatch TOGGLE_SHUFFLE action', () => {
PlaylistViewActions.toggleShuffle();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_SHUFFLE' });
});
});
describe('toggleSave', () => {
it('Should dispatch TOGGLE_SAVE action', () => {
PlaylistViewActions.toggleSave();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'TOGGLE_SAVE' });
});
});
});
});

View File

@@ -0,0 +1,27 @@
import { ProfilePageActions } from '../../../src/static/js/utils/actions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by ProfilePageActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('ProfilePageActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
it('Should dispatch LOAD_AUTHOR_DATA ', () => {
ProfilePageActions.load_author_data();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'LOAD_AUTHOR_DATA' });
});
it('Should dispatch REMOVE_PROFILE ', () => {
ProfilePageActions.remove_profile();
expect(dispatch).toHaveBeenCalledTimes(1);
expect(dispatch).toHaveBeenCalledWith({ type: 'REMOVE_PROFILE' });
});
});
});

View File

@@ -0,0 +1,25 @@
import { SearchFieldActions } from '../../../src/static/js/utils/actions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by SearchFieldActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('SearchFieldActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('requestPredictions', () => {
it('Should dispatch REQUEST_PREDICTIONS with provided query strings', () => {
SearchFieldActions.requestPredictions('cats');
SearchFieldActions.requestPredictions('');
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'REQUEST_PREDICTIONS', query: 'cats' });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'REQUEST_PREDICTIONS', query: '' });
});
});
});
});

View File

@@ -0,0 +1,72 @@
import { VideoViewerActions } from '../../../src/static/js/utils/actions';
import dispatcher from '../../../src/static/js/utils/dispatcher';
// Mock the dispatcher module used by VideoViewerActions
jest.mock('../../../src/static/js/utils/dispatcher', () => ({ dispatch: jest.fn() }));
describe('utils/actions', () => {
describe('VideoViewerActions', () => {
const dispatch = dispatcher.dispatch;
beforeEach(() => {
(dispatcher.dispatch as jest.Mock).mockClear();
});
describe('set_viewer_mode', () => {
it('Should dispatch SET_VIEWER_MODE with "true" and "false" for enabling and disabling theater mode', () => {
VideoViewerActions.set_viewer_mode(true);
VideoViewerActions.set_viewer_mode(false);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'SET_VIEWER_MODE', inTheaterMode: true });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'SET_VIEWER_MODE', inTheaterMode: false });
});
});
describe('set_player_volume', () => {
it('Should dispatch SET_PLAYER_VOLUME with provided volume numbers', () => {
VideoViewerActions.set_player_volume(0);
VideoViewerActions.set_player_volume(0.75);
VideoViewerActions.set_player_volume(1);
expect(dispatch).toHaveBeenCalledTimes(3);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'SET_PLAYER_VOLUME', playerVolume: 0 });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'SET_PLAYER_VOLUME', playerVolume: 0.75 });
expect(dispatch).toHaveBeenNthCalledWith(3, { type: 'SET_PLAYER_VOLUME', playerVolume: 1 });
});
});
describe('set_player_sound_muted', () => {
it('Should dispatch SET_PLAYER_SOUND_MUTED with "true" and "false"', () => {
VideoViewerActions.set_player_sound_muted(true);
VideoViewerActions.set_player_sound_muted(false);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'SET_PLAYER_SOUND_MUTED', playerSoundMuted: true });
expect(dispatch).toHaveBeenNthCalledWith(2, {
type: 'SET_PLAYER_SOUND_MUTED',
playerSoundMuted: false,
});
});
});
describe('set_video_quality', () => {
it('Should dispatch SET_VIDEO_QUALITY with "auto" and numeric quality', () => {
VideoViewerActions.set_video_quality('auto');
VideoViewerActions.set_video_quality(720);
expect(dispatch).toHaveBeenCalledTimes(2);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'SET_VIDEO_QUALITY', quality: 'auto' });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'SET_VIDEO_QUALITY', quality: 720 });
});
});
describe('set_video_playback_speed', () => {
it('Should dispatch SET_VIDEO_PLAYBACK_SPEED with different speeds', () => {
VideoViewerActions.set_video_playback_speed(1.5);
VideoViewerActions.set_video_playback_speed(0.5);
VideoViewerActions.set_video_playback_speed(2);
expect(dispatch).toHaveBeenCalledTimes(3);
expect(dispatch).toHaveBeenNthCalledWith(1, { type: 'SET_VIDEO_PLAYBACK_SPEED', playbackSpeed: 1.5 });
expect(dispatch).toHaveBeenNthCalledWith(2, { type: 'SET_VIDEO_PLAYBACK_SPEED', playbackSpeed: 0.5 });
expect(dispatch).toHaveBeenNthCalledWith(3, { type: 'SET_VIDEO_PLAYBACK_SPEED', playbackSpeed: 2 });
});
});
});
});