Feature/add unit testing framework and start writing unit tests (acceptance tests) (#708)

* add jest and rewire for testing

* wrap loose code for initMutationObserver() in function and call it instead of just executing it

Otherwise this code is executed everytime state.js is imported somewhere in a file.
This leads to not being able to test properly, since state.js is imported in a lot
of the src files and fails when running the tests since we are missing global environment
settings such as document, window etc in the jest environment.

* export roundDown and getNumberformatter to make them testable as well

* add basic acceptance tests for utils.js, create snapshots and add jest config

* add failing test for buggy behaviour in a separate test case and skip it until fixed

* Fixes after merge conflicts

---------

Co-authored-by: Dmitrii Selivanov <selivano.d@gmail.com>
This commit is contained in:
Leon BubovaandDmitrii Selivanov authored and GitHub committed 2024-06-16 19:52:58 +02:00
1 parent fbf00e474d
commit d62130b898
9 files changed
+400 -8134

No files matched your search

+24 -47
View File
@@ -32,17 +32,12 @@ api.runtime.onMessage.addListener((request, sender, sendResponse) => {
} else if (request.message == "set_state") {
// chrome.identity.getAuthToken({ interactive: true }, function (token) {
let token = "";
fetch(
`${apiUrl}/votes?videoId=${request.videoId}&likeCount=${
request.likeCount || ""
}`,
{
method: "GET",
headers: {
Accept: "application/json",
},
fetch(`${apiUrl}/votes?videoId=${request.videoId}&likeCount=${request.likeCount || ""}`, {
method: "GET",
headers: {
Accept: "application/json",
},
)
})
.then((response) => response.json())
.then((response) => {
sendResponse(response);
@@ -147,15 +142,12 @@ async function sendVote(videoId, vote) {
async function register() {
const userId = generateUserID();
api.storage.sync.set({ userId });
const registrationResponse = await fetch(
`${apiUrl}/puzzle/registration?userId=${userId}`,
{
method: "GET",
headers: {
Accept: "application/json",
},
const registrationResponse = await fetch(`${apiUrl}/puzzle/registration?userId=${userId}`, {
method: "GET",
headers: {
Accept: "application/json",
},
).then((response) => response.json());
}).then((response) => response.json());
const solvedPuzzle = await solvePuzzle(registrationResponse);
if (!solvedPuzzle.solution) {
await register();
@@ -210,9 +202,7 @@ function countLeadingZeroes(uInt8View, limit) {
}
async function solvePuzzle(puzzle) {
let challenge = Uint8Array.from(atob(puzzle.challenge), (c) =>
c.charCodeAt(0),
);
let challenge = Uint8Array.from(atob(puzzle.challenge), (c) => c.charCodeAt(0));
let buffer = new ArrayBuffer(20);
let uInt8View = new Uint8Array(buffer);
let uInt32View = new Uint32Array(buffer);
@@ -235,8 +225,7 @@ async function solvePuzzle(puzzle) {
}
function generateUserID(length = 36) {
const charset =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
if (crypto && crypto.getRandomValues) {
const values = new Uint32Array(length);
@@ -255,9 +244,7 @@ function generateUserID(length = 36) {
function storageChangeHandler(changes, area) {
if (changes.disableVoteSubmission !== undefined) {
handleDisableVoteSubmissionChangeEvent(
changes.disableVoteSubmission.newValue,
);
handleDisableVoteSubmissionChangeEvent(changes.disableVoteSubmission.newValue);
}
if (changes.coloredThumbs !== undefined) {
handleColoredThumbsChangeEvent(changes.coloredThumbs.newValue);
@@ -272,21 +259,16 @@ function storageChangeHandler(changes, area) {
handleNumberDisplayFormatChangeEvent(changes.numberDisplayFormat.newValue);
}
if (changes.numberDisplayReformatLikes !== undefined) {
handleNumberDisplayReformatLikesChangeEvent(
changes.numberDisplayReformatLikes.newValue,
);
handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue);
}
if (changes.disableLogging !== undefined) {
handleDisableLoggingChangeEvent(changes.disableLogging.newValue);
}
if (changes.showTooltipPercentage !== undefined) {
handleShowTooltipPercentageChangeEvent(
changes.showTooltipPercentage.newValue,
);
handleShowTooltipPercentageChangeEvent(changes.showTooltipPercentage.newValue);
}
if (changes.numberDisplayReformatLikes !== undefined) {
handleNumberDisplayReformatLikesChangeEvent(
changes.numberDisplayReformatLikes.newValue,
);
handleNumberDisplayReformatLikesChangeEvent(changes.numberDisplayReformatLikes.newValue);
}
}
@@ -318,10 +300,8 @@ function handleTooltipPercentageModeChangeEvent(value) {
}
function changeIcon(iconName) {
if (api.action !== undefined)
api.action.setIcon({ path: "/icons/" + iconName });
else if (api.browserAction !== undefined)
api.browserAction.setIcon({ path: "/icons/" + iconName });
if (api.action !== undefined) api.action.setIcon({ path: "/icons/" + iconName });
else if (api.browserAction !== undefined) api.browserAction.setIcon({ path: "/icons/" + iconName });
else console.log("changing icon is not supported");
}
@@ -369,12 +349,11 @@ function initializeDisableVoteSubmission() {
});
}
function initializeDisableLogging(){
api.storage.sync.get(['disableLogging'],(res)=>{
function initializeDisableLogging() {
api.storage.sync.get(["disableLogging"], (res) => {
if (res.disableLogging === undefined) {
api.storage.sync.set({disableLogging:true});
}
else {
api.storage.sync.set({ disableLogging: true });
} else {
extConfig.disableLogging = res.disableLogging;
}
});
@@ -454,7 +433,5 @@ function isChrome() {
}
function isFirefox() {
return (
typeof browser !== "undefined" && typeof browser.runtime !== "undefined"
);
return typeof browser !== "undefined" && typeof browser.runtime !== "undefined";
}