2022-07-16 23:28:13 +05:30
|
|
|
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
|
2023-05-27 22:25:52 +05:30
|
|
|
import fileUpload, {
|
|
|
|
getFilename,
|
|
|
|
validateImageName,
|
|
|
|
validateFileFromAllowList,
|
|
|
|
} from '~/lib/utils/file_upload';
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
describe('File upload', () => {
|
|
|
|
beforeEach(() => {
|
2022-07-16 23:28:13 +05:30
|
|
|
setHTMLFixture(`
|
2019-02-15 15:39:39 +05:30
|
|
|
<form>
|
|
|
|
<button class="js-button" type="button">Click me!</button>
|
|
|
|
<input type="text" class="js-input" />
|
|
|
|
<span class="js-filename"></span>
|
|
|
|
</form>
|
|
|
|
`);
|
2019-03-02 22:35:43 +05:30
|
|
|
});
|
|
|
|
|
2022-07-16 23:28:13 +05:30
|
|
|
afterEach(() => {
|
|
|
|
resetHTMLFixture();
|
|
|
|
});
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
describe('when there is a matching button and input', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
fileUpload('.js-button', '.js-input');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('clicks file input after clicking button', () => {
|
|
|
|
const btn = document.querySelector('.js-button');
|
|
|
|
const input = document.querySelector('.js-input');
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2019-03-02 22:35:43 +05:30
|
|
|
|
|
|
|
btn.click();
|
|
|
|
|
|
|
|
expect(input.click).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates file name text', () => {
|
|
|
|
const input = document.querySelector('.js-input');
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
input.value = 'path/to/file/index.js';
|
|
|
|
|
|
|
|
input.dispatchEvent(new CustomEvent('change'));
|
|
|
|
|
|
|
|
expect(document.querySelector('.js-filename').textContent).toEqual('index.js');
|
|
|
|
});
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it('fails gracefully when there is no matching button', () => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const input = document.querySelector('.js-input');
|
2019-03-02 22:35:43 +05:30
|
|
|
const btn = document.querySelector('.js-button');
|
|
|
|
fileUpload('.js-not-button', '.js-input');
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2019-02-15 15:39:39 +05:30
|
|
|
|
|
|
|
btn.click();
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(input.click).not.toHaveBeenCalled();
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
it('fails gracefully when there is no matching input', () => {
|
2019-02-15 15:39:39 +05:30
|
|
|
const input = document.querySelector('.js-input');
|
2019-03-02 22:35:43 +05:30
|
|
|
const btn = document.querySelector('.js-button');
|
|
|
|
fileUpload('.js-button', '.js-not-input');
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
btn.click();
|
2019-02-15 15:39:39 +05:30
|
|
|
|
2019-03-02 22:35:43 +05:30
|
|
|
expect(input.click).not.toHaveBeenCalled();
|
2019-02-15 15:39:39 +05:30
|
|
|
});
|
|
|
|
});
|
2020-04-22 19:07:51 +05:30
|
|
|
|
|
|
|
describe('getFilename', () => {
|
2021-12-11 22:18:48 +05:30
|
|
|
it('returns file name', () => {
|
|
|
|
const file = new File([], 'test.jpg');
|
|
|
|
|
|
|
|
expect(getFilename(file)).toBe('test.jpg');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('file name validator', () => {
|
|
|
|
it('validate file name', () => {
|
|
|
|
const file = new File([], 'test.jpg');
|
|
|
|
|
|
|
|
expect(validateImageName(file)).toBe('test.jpg');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('illegal file name should be rename to image.png', () => {
|
|
|
|
const file = new File([], 'test<.png');
|
|
|
|
|
|
|
|
expect(validateImageName(file)).toBe('image.png');
|
2020-04-22 19:07:51 +05:30
|
|
|
});
|
|
|
|
});
|
2023-05-27 22:25:52 +05:30
|
|
|
|
|
|
|
describe('validateFileFromAllowList', () => {
|
|
|
|
it('returns true if the file type is in the allowed list', () => {
|
|
|
|
const allowList = ['.foo', '.bar'];
|
|
|
|
const fileName = 'file.foo';
|
|
|
|
|
|
|
|
expect(validateFileFromAllowList(fileName, allowList)).toBe(true);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns false if the file type is in the allowed list', () => {
|
|
|
|
const allowList = ['.foo', '.bar'];
|
|
|
|
const fileName = 'file.baz';
|
|
|
|
|
|
|
|
expect(validateFileFromAllowList(fileName, allowList)).toBe(false);
|
|
|
|
});
|
|
|
|
});
|