2024-04-23 00:43:36 +02:00
|
|
|
import { $, $$, removeEl } from '../utils/dom';
|
|
|
|
import { assertNotNull, assertNotUndefined } from '../utils/assert';
|
|
|
|
|
2024-04-30 20:44:26 +02:00
|
|
|
import { fetchMock } from '../../test/fetch-mock';
|
2024-04-23 00:43:36 +02:00
|
|
|
import { fixEventListeners } from '../../test/fix-event-listeners';
|
|
|
|
import { fireEvent, waitFor } from '@testing-library/dom';
|
|
|
|
import { promises } from 'fs';
|
|
|
|
import { join } from 'path';
|
|
|
|
|
|
|
|
import { setupImageUpload } from '../upload';
|
|
|
|
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const scrapeResponse = {
|
|
|
|
description: 'test',
|
|
|
|
images: [
|
2024-04-30 20:44:26 +02:00
|
|
|
{ url: 'http://localhost/images/1', camo_url: 'http://localhost/images/1' },
|
|
|
|
{ url: 'http://localhost/images/2', camo_url: 'http://localhost/images/2' },
|
2024-04-23 00:43:36 +02:00
|
|
|
],
|
|
|
|
source_url: 'http://localhost/images',
|
|
|
|
author_name: 'test',
|
|
|
|
};
|
|
|
|
const nullResponse = null;
|
|
|
|
const errorResponse = {
|
|
|
|
errors: ['Error 1', 'Error 2'],
|
|
|
|
};
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
|
|
|
|
describe('Image upload form', () => {
|
|
|
|
let mockPng: File;
|
|
|
|
let mockWebm: File;
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
beforeAll(async () => {
|
2024-04-23 00:43:36 +02:00
|
|
|
const mockPngPath = join(__dirname, 'upload-test.png');
|
|
|
|
const mockWebmPath = join(__dirname, 'upload-test.webm');
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
mockPng = new File([(await promises.readFile(mockPngPath, { encoding: null })).buffer], 'upload-test.png', {
|
|
|
|
type: 'image/png',
|
|
|
|
});
|
|
|
|
mockWebm = new File([(await promises.readFile(mockWebmPath, { encoding: null })).buffer], 'upload-test.webm', {
|
|
|
|
type: 'video/webm',
|
|
|
|
});
|
2024-04-23 00:43:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
fetchMock.enableMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
fetchMock.disableMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
fixEventListeners(window);
|
|
|
|
|
|
|
|
let form: HTMLFormElement;
|
|
|
|
let imgPreviews: HTMLDivElement;
|
|
|
|
let fileField: HTMLInputElement;
|
|
|
|
let remoteUrl: HTMLInputElement;
|
|
|
|
let scraperError: HTMLDivElement;
|
|
|
|
let fetchButton: HTMLButtonElement;
|
|
|
|
let tagsEl: HTMLTextAreaElement;
|
|
|
|
let sourceEl: HTMLInputElement;
|
|
|
|
let descrEl: HTMLTextAreaElement;
|
|
|
|
|
2024-04-30 20:44:26 +02:00
|
|
|
const assertFetchButtonIsDisabled = () => {
|
|
|
|
if (!fetchButton.hasAttribute('disabled')) throw new Error('fetchButton is not disabled');
|
|
|
|
};
|
|
|
|
|
2024-04-23 00:43:36 +02:00
|
|
|
beforeEach(() => {
|
2024-07-03 22:54:14 +02:00
|
|
|
document.documentElement.insertAdjacentHTML(
|
|
|
|
'beforeend',
|
|
|
|
`
|
2024-04-23 00:43:36 +02:00
|
|
|
<form action="/images">
|
|
|
|
<div id="js-image-upload-previews"></div>
|
|
|
|
<input id="image_image" name="image[image]" type="file" class="js-scraper" />
|
|
|
|
<input id="image_scraper_url" name="image[scraper_url]" type="url" class="js-scraper" />
|
|
|
|
<button id="js-scraper-preview" type="button">Fetch</button>
|
|
|
|
<div class="field-error-js hidden js-scraper"></div>
|
|
|
|
|
|
|
|
<input id="image_sources_0_source" name="image[sources][0][source]" type="text" class="js-source-url" />
|
|
|
|
<textarea id="image_tag_input" name="image[tag_input]" class="js-image-tags-input"></textarea>
|
|
|
|
<textarea id="image_description" name="image[description]" class="js-image-descr-input"></textarea>
|
|
|
|
</form>
|
2024-07-03 22:54:14 +02:00
|
|
|
`,
|
|
|
|
);
|
2024-04-23 00:43:36 +02:00
|
|
|
|
|
|
|
form = assertNotNull($<HTMLFormElement>('form'));
|
|
|
|
imgPreviews = assertNotNull($<HTMLDivElement>('#js-image-upload-previews'));
|
|
|
|
fileField = assertNotUndefined($$<HTMLInputElement>('.js-scraper')[0]);
|
|
|
|
remoteUrl = assertNotUndefined($$<HTMLInputElement>('.js-scraper')[1]);
|
|
|
|
scraperError = assertNotUndefined($$<HTMLInputElement>('.js-scraper')[2]);
|
|
|
|
tagsEl = assertNotNull($<HTMLTextAreaElement>('.js-image-tags-input'));
|
|
|
|
sourceEl = assertNotNull($<HTMLInputElement>('.js-source-url'));
|
|
|
|
descrEl = assertNotNull($<HTMLTextAreaElement>('.js-image-descr-input'));
|
|
|
|
fetchButton = assertNotNull($<HTMLButtonElement>('#js-scraper-preview'));
|
|
|
|
|
|
|
|
setupImageUpload();
|
|
|
|
fetchMock.resetMocks();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
removeEl(form);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should disable fetch button on empty source', () => {
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.input(remoteUrl, { target: { value: '' } });
|
2024-04-23 00:43:36 +02:00
|
|
|
expect(fetchButton.disabled).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should enable fetch button on non-empty source', () => {
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.input(remoteUrl, { target: { value: 'http://localhost/images/1' } });
|
2024-04-23 00:43:36 +02:00
|
|
|
expect(fetchButton.disabled).toBe(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a preview element when an image file is uploaded', () => {
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.change(fileField, { target: { files: [mockPng] } });
|
|
|
|
return waitFor(() => {
|
|
|
|
assertFetchButtonIsDisabled();
|
|
|
|
expect(imgPreviews.querySelectorAll('img')).toHaveLength(1);
|
|
|
|
});
|
2024-04-23 00:43:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should create a preview element when a Matroska video file is uploaded', () => {
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.change(fileField, { target: { files: [mockWebm] } });
|
|
|
|
return waitFor(() => {
|
|
|
|
assertFetchButtonIsDisabled();
|
|
|
|
expect(imgPreviews.querySelectorAll('video')).toHaveLength(1);
|
|
|
|
});
|
2024-04-23 00:43:36 +02:00
|
|
|
});
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
it('should block navigation away after an image file is attached, but not after form submission', async () => {
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.change(fileField, { target: { files: [mockPng] } });
|
|
|
|
await waitFor(() => {
|
|
|
|
assertFetchButtonIsDisabled();
|
|
|
|
expect(imgPreviews.querySelectorAll('img')).toHaveLength(1);
|
|
|
|
});
|
2024-04-23 00:43:36 +02:00
|
|
|
|
|
|
|
const failedUnloadEvent = new Event('beforeunload', { cancelable: true });
|
|
|
|
expect(fireEvent(window, failedUnloadEvent)).toBe(false);
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
await new Promise<void>((resolve) => {
|
|
|
|
form.addEventListener('submit', (event) => {
|
2024-04-23 00:43:36 +02:00
|
|
|
event.preventDefault();
|
|
|
|
resolve();
|
|
|
|
});
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.submit(form);
|
2024-04-23 00:43:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const succeededUnloadEvent = new Event('beforeunload', { cancelable: true });
|
|
|
|
expect(fireEvent(window, succeededUnloadEvent)).toBe(true);
|
|
|
|
});
|
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
it('should scrape images when the fetch button is clicked', async () => {
|
2024-04-23 00:43:36 +02:00
|
|
|
fetchMock.mockResolvedValue(new Response(JSON.stringify(scrapeResponse), { status: 200 }));
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.input(remoteUrl, { target: { value: 'http://localhost/images/1' } });
|
2024-04-23 00:43:36 +02:00
|
|
|
|
2024-07-03 22:54:14 +02:00
|
|
|
await new Promise<void>((resolve) => {
|
2024-04-23 00:43:36 +02:00
|
|
|
tagsEl.addEventListener('addtag', (event: Event) => {
|
2024-04-30 20:44:26 +02:00
|
|
|
expect((event as CustomEvent).detail).toEqual({ name: 'artist:test' });
|
2024-04-23 00:43:36 +02:00
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
|
|
|
|
fireEvent.keyDown(remoteUrl, { keyCode: 13 });
|
|
|
|
});
|
|
|
|
|
|
|
|
await waitFor(() => expect(fetch).toHaveBeenCalledTimes(1));
|
|
|
|
await waitFor(() => expect(imgPreviews.querySelectorAll('img')).toHaveLength(2));
|
|
|
|
|
|
|
|
expect(scraperError.innerHTML).toEqual('');
|
|
|
|
expect(sourceEl.value).toEqual('http://localhost/images');
|
|
|
|
expect(descrEl.value).toEqual('test');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show null scrape result', () => {
|
|
|
|
fetchMock.mockResolvedValue(new Response(JSON.stringify(nullResponse), { status: 200 }));
|
|
|
|
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.input(remoteUrl, { target: { value: 'http://localhost/images/1' } });
|
|
|
|
fireEvent.click(fetchButton);
|
2024-04-23 00:43:36 +02:00
|
|
|
|
|
|
|
return waitFor(() => {
|
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(imgPreviews.querySelectorAll('img')).toHaveLength(0);
|
|
|
|
expect(scraperError.innerText).toEqual('No image found at that address.');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should show error scrape result', () => {
|
|
|
|
fetchMock.mockResolvedValue(new Response(JSON.stringify(errorResponse), { status: 200 }));
|
|
|
|
|
2024-04-30 20:44:26 +02:00
|
|
|
fireEvent.input(remoteUrl, { target: { value: 'http://localhost/images/1' } });
|
|
|
|
fireEvent.click(fetchButton);
|
2024-04-23 00:43:36 +02:00
|
|
|
|
|
|
|
return waitFor(() => {
|
|
|
|
expect(fetch).toHaveBeenCalledTimes(1);
|
|
|
|
expect(imgPreviews.querySelectorAll('img')).toHaveLength(0);
|
|
|
|
expect(scraperError.innerText).toEqual('Error 1 Error 2');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|