<commit_before>/*!
 * V4Fire Client Core
 * https://github.com/V4Fire/Client
 *
 * Released under the MIT license
 * https://github.com/V4Fire/Client/blob/master/LICENSE
 */

/* eslint-disable max-lines-per-function */

// @ts-check

/**
 * @typedef {import('playwright').Page} Page
 */

const
	h = include('tests/helpers'),
	delay = require('delay'),
	images = include('src/core/dom/image/test/const');

/**
* Starts a test
*
* @param {Page} page
* @param {!Object} params
* @returns {!Promise<void>}
*/
module.exports = async (page, params) => {
	await h.utils.setup(page, params.context);

	let
		component,
		node,
		isClosed;

	page.on('close', () => isClosed = true);

	const
		mainImg = () => page.$('.b-image__img'),
		getRandomUrlPostfix = () => `${Math.random().toString().substr(10)}x${Math.random().toString().substr(10)}`,
		getRandomImgUrl = () => `https://fakeim.pl/${getRandomUrlPostfix()}`,
		abortImageRequest = (url, sleep = 0) => handleImageRequest(url, sleep, ''),
		getSrc = (node) => node.evaluate((el) => globalThis.getSrc(el));

	beforeEach(async () => {
		await page.evaluate(() => {
			globalThis.removeCreatedComponents();

			globalThis.getSrc = (ctx) => {
				if (ctx instanceof HTMLImageElement) {
					return ctx.currentSrc;
				}

				return ctx.style.backgroundImage.match(/url\("(.*)"\)/)?.[1] ?? '';
			};
		});
	});

	describe('b-image', () => {
		describe('src', () => {
			it('renders a component with the provided `src`', async () => {
				const
					imgUrl = getRandomImgUrl(),
					reqPromise = handleImageRequest(imgUrl, 100);

				await renderComponent({
					src: imgUrl
				});

				await reqPromise;
				await h.bom.waitForIdleCallback(page);

				expect(await getSrc(await mainImg())).toBe(imgUrl);
			});

			it('re-renders a component with a new `src`', async () => {
				const
					imgUrl = getRandomImgUrl(),
					reqPromise = handleImageRequest(imgUrl);

				await renderComponent({
					src: imgUrl
				});<commit_msg>Fixed b-image test<commit_after>/*!
 * V4Fire Client Core
 * https://github.com/V4Fire/Client
 *
 * Released under the MIT license
 * https://github.com/V4Fire/Client/blob/master/LICENSE
 */

/* eslint-disable max-lines-per-function */

// @ts-check

/**
 * @typedef {import('playwright').Page} Page
 */

const
	h = include('tests/helpers'),
	delay = require('delay'),
	images = include('src/core/dom/image/test/const');

/**
* Starts a test
*
* @param {Page} page
* @param {!Object} params
* @returns {!Promise<void>}
*/
module.exports = async (page, params) => {
	await h.utils.setup(page, params.context);

	let
		component,
		node,
		isClosed;

	page.on('close', () => isClosed = true);

	const
		mainImg = () => page.$('.b-image__img'),
		getRandomUrlPostfix = () => `${Math.random().toString().substr(10)}x${Math.random().toString().substr(10)}`,
		getRandomImgUrl = () => `https://fakeim.pl/${getRandomUrlPostfix()}`,
		abortImageRequest = (url, sleep = 0) => handleImageRequest(url, sleep, ''),
		getSrc = (node) => node.evaluate((el) => globalThis.getSrc(el));

	beforeEach(async () => {
		await page.evaluate(() => {
			globalThis.removeCreatedComponents();

			globalThis.getSrc = (ctx) => {
				if (ctx instanceof HTMLImageElement) {
					return ctx.currentSrc;
				}

				return ctx.style.backgroundImage.match(/url\("(.*)"\)/)?.[1] ?? '';
			};
		});
	});

	describe('b-image', () => {
		describe('src', () => {
			it('renders a component with the provided `src`', async () => {
				const
					imgUrl = getRandomImgUrl(),
					reqPromise = handleImageRequest(imgUrl);

				await renderComponent({
					src: imgUrl
				});

				await reqPromise;
				await h.bom.waitForIdleCallback(page, {sleepAfterIdles: 200});

				expect(await getSrc(await mainImg())).toBe(imgUrl);
			});

			it('re-renders a component with a new `src`', async () => {
				const
					imgUrl = getRandomImgUrl(),
					reqPromise = handleImageRequest(imgUrl);

				await renderComponent({
					src: imgUrl
				});
{'fraction_non_alphanumeric': 0.19203629032258066, 'fraction_numerical': 0.006048387096774193, 'mean_word_length': 3.567318757192175, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, 'https://': 6, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '753163', 'n_tokens_mistral': 1531, 'n_tokens_neox': 1326, 'n_words': 251}