Skip to content

Commit

Permalink
Moved logic needed for tests to test files (#225)
Browse files Browse the repository at this point in the history
  • Loading branch information
NikkelM authored Nov 5, 2023
1 parent 31403be commit 88661f2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/shuffleVideo.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ async function tryGetPlaylistFromDB(playlistId, localPlaylistInfo = null) {
};

// Some of the tests break if we do not create a deepCopy here, as the local and database object somehow get linked
let playlistInfo = JSON.parse(JSON.stringify(await chrome.runtime.sendMessage(msg)));
let playlistInfo = await chrome.runtime.sendMessage(msg);

/* c8 ignore start - These are legacy conversions we don't want to test */
// In case the playlist is still in the old Array format (before v1.0.0) in the database, convert it to the new format
Expand Down
10 changes: 5 additions & 5 deletions test/testSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ chrome.storage.local.get.callsFake(() => {
return Promise.resolve(mockedLocalStorage);
});
chrome.storage.local.set.callsFake((obj) => {
Object.assign(mockedLocalStorage, obj);
Object.assign(mockedLocalStorage, deepCopy(obj));
return Promise.resolve();
});
chrome.storage.local.clear.callsFake(() => {
Expand All @@ -51,19 +51,19 @@ chrome.runtime.sendMessage.callsFake((request) => {

case 'getPlaylistFromDB':
// Return a playlist from the database
return Promise.resolve(mockedDatabase[request.data] ?? null);
return Promise.resolve(deepCopy(mockedDatabase[request.data] ?? null));

// With our mocked database, both commands have the same effect
case 'updatePlaylistInfoInDB':
request.data.val.videos = { ...mockedDatabase[request.data.key]?.videos ?? {}, ...request.data.val.videos };
request.data.val.videos = { ...mockedDatabase[request.data.key]?.videos ?? {}, ...deepCopy(request.data.val.videos) };
case 'overwritePlaylistInfoInDB':
// Update/Overwrite a playlist in the database
mockedDatabase[request.data.key] = request.data.val;
mockedDatabase[request.data.key] = deepCopy(request.data.val);
return "PlaylistInfo was sent to database.";

// Only for the tests
case "setKeyInDB":
mockedDatabase[request.data.key] = request.data.val;
mockedDatabase[request.data.key] = deepCopy(request.data.val);
return "Key was set in the database (mocked for tests).";

case "getAPIKey":
Expand Down

0 comments on commit 88661f2

Please sign in to comment.