Skip to content

Commit

Permalink
Lint Jest test files
Browse files Browse the repository at this point in the history
  • Loading branch information
ylavoie committed Jun 3, 2024
1 parent 18186d9 commit 22b8c14
Show file tree
Hide file tree
Showing 26 changed files with 775 additions and 674 deletions.
4 changes: 2 additions & 2 deletions UI/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@
"build:dev": "webpack --progress --mode=development --stats errors-warnings",
"lint": "yarn run lint:css && yarn run lint:js && yarn run lint:markdown && yarn run lint:vue",
"lint:css": "stylelint css/**/*.css",
"lint:js": "eslint {src,js-src}/**/*.js",
"lint:js:fix": "eslint --fix {src,js-src}/**/*.js",
"lint:js": "eslint {src,js-src,tests}/**/*.js",
"lint:js:fix": "eslint --fix {src,js-src,tests}/**/*.js",
"lint:markdown": "markdownlint --config ../.markdownlint.json --ignore ./node_modules --ignore ./js .",
"lint:vue": "eslint src/**/*.vue",
"lint:vue:fix": "eslint --fix src/**/*.vue",
Expand Down
10 changes: 6 additions & 4 deletions UI/tests/common/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const rtlDetect = require("rtl-detect");

import { createI18n } from "vue-i18n";

const SUPPORT_LOCALES = [ "en", "fr_CA", "ar_EG" ];
const SUPPORT_LOCALES = ["en", "fr_CA", "ar_EG"];

function _mapLocale(locale) {
const _locale = locale.match(/([a-z]{2})-([a-z]{2})/);
Expand All @@ -17,7 +17,7 @@ function _mapLocale(locale) {
var _messages = {};
SUPPORT_LOCALES.forEach(function (it) {
const locale = _mapLocale(it);
// eslint-disable-next-line global-require
// eslint-disable-next-line import-x/no-dynamic-require, global-require
_messages[locale] = require("@/locales/" + locale + ".json");
});

Expand All @@ -35,8 +35,10 @@ export async function setI18nLanguage(lang) {
const locale = _mapLocale(lang.value);

// If the language hasn't been loaded yet
if (i18n.global.availableLocales.includes(locale) &&
await document.querySelector("html").setAttribute("lang", locale)) {
if (
i18n.global.availableLocales.includes(locale) &&
document.querySelector("html").setAttribute("lang", locale)
) {
if (rtlDetect.isRtlLang(locale)) {
document.querySelector("html").setAttribute("dir", "rtl");
}
Expand Down
122 changes: 65 additions & 57 deletions UI/tests/common/jest-setup.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@

import { jest, beforeAll, afterAll, beforeEach, afterEach } from "@jest/globals";
import 'core-js';
import { setGlobalOrigin } from 'undici';
/** @format */

import {
jest,
beforeAll,
afterAll,
beforeEach,
afterEach
} from "@jest/globals";
import "core-js";
import { setGlobalOrigin } from "undici";

import "./mocks/lsmb_elements";
import { server } from './mocks/server.js';
import { server } from "./mocks/server.js";

Object.defineProperty(window, "lsmbConfig", {
writable: true,
Expand All @@ -15,77 +22,78 @@ Object.defineProperty(window, "lsmbConfig", {
});

// Enable i18n
import { config } from '@vue/test-utils'
import { i18n } from '../common/i18n'
import { config } from "@vue/test-utils";
import { i18n } from "../common/i18n";

config.global.plugins = [i18n]
config.global.plugins = [i18n];

const oldWindowLocation = window.location;

beforeAll(() => {
delete window.location

window.location = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(oldWindowLocation),
assign: {
configurable: true,
value: jest.fn(),
}
},
)

// Establish API mocking before all tests.
server.listen({
onUnhandledRequest(req) {
console.error(
'Found an unhandled %s request to %s',
req.method,
req.url.href
)
}
})
})
delete window.location;

window.location = Object.defineProperties(
{},
{
...Object.getOwnPropertyDescriptors(oldWindowLocation),
assign: {
configurable: true,
value: jest.fn()
}
}
);

// Establish API mocking before all tests.
server.listen({
onUnhandledRequest(req) {
console.error(
"Found an unhandled %s request to %s",
req.method,
req.url.href
);
}
});
});

afterAll(() => {
// restore `window.location` to the original `jsdom`
// `Location` object
window.location = oldWindowLocation
// restore `window.location` to the original `jsdom`
// `Location` object
window.location = oldWindowLocation;

// Clean up after the tests are finished.
server.close();
})
// Clean up after the tests are finished.
server.close();
});

beforeEach(() => {
// Set the global origin (used by fetch) to the url provided in vitest.config.ts
setGlobalOrigin(window.location.href)
})
// Set the global origin (used by fetch) to the url provided in vitest.config.ts
setGlobalOrigin(window.location.href);
});

// Reset any request handlers that we may add during the tests,
// so they don't affect other tests.
afterEach(() => {
server.resetHandlers();
server.resetHandlers();
});

// Helper function to wait for DOM updates
// eslint no-unused-expressions: ["error", { "allowTernary": true }]
export const retry = (assertion, { interval = 20, timeout = 1000 } = {}) => {
return new Promise((resolve, reject) => {
const startTime = Date.now();

const tryAgain = () => {
setTimeout(() => {
try {
resolve(assertion());
} catch (err) {
Date.now() - startTime > timeout ? reject(err) : tryAgain();
}
}, interval);
};

tryAgain();
});
return new Promise((resolve, reject) => {
const startTime = Date.now();

const tryAgain = () => {
setTimeout(() => {
try {
resolve(assertion());
} catch (err) {
// eslint-disable-next-line no-unused-expressions
Date.now() - startTime > timeout ? reject(err) : tryAgain();
}
}, interval);
};

tryAgain();
});
};

window.retry = retry;
15 changes: 8 additions & 7 deletions UI/tests/common/jest.polyfills.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
/* @format */
/* global globalThis */

const { TextEncoder, TextDecoder } = require('node:util')
const { performance } = require('node:perf_hooks')
const { TextEncoder, TextDecoder } = require("node:util");
const { performance } = require("node:perf_hooks");

Object.defineProperties(globalThis, {
TextDecoder: { value: TextDecoder },
TextEncoder: { value: TextEncoder },
performance: { value: performance }
})
});

const { Blob } = require('node:buffer')
const { fetch, Headers, FormData, Request, Response } = require('undici')
const { Blob } = require("node:buffer");
const { fetch, Headers, FormData, Request, Response } = require("undici");

Object.defineProperties(globalThis, {
fetch: { value: fetch, writable: true },
Blob: { value: Blob },
Headers: { value: Headers },
FormData: { value: FormData },
Request: { value: Request },
Response: { value: Response },
})
Response: { value: Response }
});
27 changes: 14 additions & 13 deletions UI/tests/common/mocks/handlers.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/** @format */

// MSW Handlers

// Stores
import { businessTypesHandlers} from "./store_business-types_handlers";
import { businessTypesHandlers } from "./store_business-types_handlers";
import { gifiHandlers } from "./store_gifi_handlers";
import { countriesHandlers } from "./store_countries_handlers";
import { languageHandlers } from "./store_language_handlers";
Expand All @@ -14,16 +16,15 @@ import { warehousesHandlers } from "./store_warehouses_handlers";
import { loginHandlers } from "./login_handlers";

export const handlers = [
// Stores
...businessTypesHandlers,
...countriesHandlers,
...gifiHandlers,
...languageHandlers,
...pricegroupHandlers,
...sessionUserHandlers,
...sicsHandlers,
...warehousesHandlers,
// Views
...loginHandlers
// Stores
...businessTypesHandlers,
...countriesHandlers,
...gifiHandlers,
...languageHandlers,
...pricegroupHandlers,
...sessionUserHandlers,
...sicsHandlers,
...warehousesHandlers,
// Views
...loginHandlers
];

39 changes: 21 additions & 18 deletions UI/tests/common/mocks/login_handlers.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
import { http, HttpResponse } from 'msw';
/** @format */

export const loginHandlers = [

http.post('login.pl', async ({ request }) => {
import { http, HttpResponse } from "msw";

export const loginHandlers = [
http.post("login.pl", async ({ request }) => {
const url = new URL(request.url);
const action = url.searchParams.get('action');
const action = url.searchParams.get("action");
const params = await request.json();
const username = params.login;
const password = params.password;
const company = params.company;

if ( action === 'authenticate' ) {
if ( username === 'MyUser' && password === 'MyPassword' && company === 'MyCompany' ) {
window.location.assign('http://lsmb/erp.pl?action=root');
if (action === "authenticate") {
if (
username === "MyUser" &&
password === "MyPassword" &&
company === "MyCompany"
) {
window.location.assign("http://lsmb/erp.pl?action=root");
return new HttpResponse(null, {
status: 200
})
});
}
if ( username && password && company === 'MyOldCompany' ) {
if (username && password && company === "MyOldCompany") {
return new HttpResponse(null, {
status: 521
})
});
}
if ( username === 'BadUser' && password && company ) {
if (username === "BadUser" && password && company) {
return new HttpResponse(null, {
status: 401
})
});
}
}
if (username === 'My' && password === 'My' && company === 'My') {
if (username === "My" && password === "My" && company === "My") {
return new HttpResponse(null, {
status: 500
})
});
}
return new HttpResponse.error('Failed to connect');
return new HttpResponse.error("Failed to connect");
})
]

];
Loading

0 comments on commit 22b8c14

Please sign in to comment.