2020-04-02 14:08:11 -04:00
|
|
|
import fileUpload, { getFilename } from '~/lib/utils/file_upload';
|
2018-11-19 10:03:58 -05:00
|
|
|
|
|
|
|
describe('File upload', () => {
|
|
|
|
beforeEach(() => {
|
|
|
|
setFixtures(`
|
|
|
|
<form>
|
|
|
|
<button class="js-button" type="button">Click me!</button>
|
|
|
|
<input type="text" class="js-input" />
|
|
|
|
<span class="js-filename"></span>
|
|
|
|
</form>
|
|
|
|
`);
|
2018-12-10 20:08:48 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
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');
|
|
|
|
|
2019-12-02 19:06:28 -05:00
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2018-12-10 20:08:48 -05:00
|
|
|
|
|
|
|
btn.click();
|
|
|
|
|
|
|
|
expect(input.click).toHaveBeenCalled();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('updates file name text', () => {
|
|
|
|
const input = document.querySelector('.js-input');
|
2018-11-19 10:03:58 -05:00
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
input.value = 'path/to/file/index.js';
|
|
|
|
|
|
|
|
input.dispatchEvent(new CustomEvent('change'));
|
|
|
|
|
|
|
|
expect(document.querySelector('.js-filename').textContent).toEqual('index.js');
|
|
|
|
});
|
2018-11-19 10:03:58 -05:00
|
|
|
});
|
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
it('fails gracefully when there is no matching button', () => {
|
2018-11-19 10:03:58 -05:00
|
|
|
const input = document.querySelector('.js-input');
|
2018-12-10 20:08:48 -05:00
|
|
|
const btn = document.querySelector('.js-button');
|
|
|
|
fileUpload('.js-not-button', '.js-input');
|
2018-11-19 10:03:58 -05:00
|
|
|
|
2019-12-02 19:06:28 -05:00
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2018-11-19 10:03:58 -05:00
|
|
|
|
|
|
|
btn.click();
|
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
expect(input.click).not.toHaveBeenCalled();
|
2018-11-19 10:03:58 -05:00
|
|
|
});
|
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
it('fails gracefully when there is no matching input', () => {
|
2018-11-19 10:03:58 -05:00
|
|
|
const input = document.querySelector('.js-input');
|
2018-12-10 20:08:48 -05:00
|
|
|
const btn = document.querySelector('.js-button');
|
|
|
|
fileUpload('.js-button', '.js-not-input');
|
2018-11-19 10:03:58 -05:00
|
|
|
|
2019-12-02 19:06:28 -05:00
|
|
|
jest.spyOn(input, 'click').mockReturnValue();
|
2018-11-19 10:03:58 -05:00
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
btn.click();
|
2018-11-19 10:03:58 -05:00
|
|
|
|
2018-12-10 20:08:48 -05:00
|
|
|
expect(input.click).not.toHaveBeenCalled();
|
2018-11-19 10:03:58 -05:00
|
|
|
});
|
|
|
|
});
|
2020-04-02 14:08:11 -04:00
|
|
|
|
|
|
|
describe('getFilename', () => {
|
|
|
|
it('returns first value correctly', () => {
|
|
|
|
const event = {
|
|
|
|
clipboardData: {
|
|
|
|
getData: () => 'test.png\rtest.txt',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(getFilename(event)).toBe('test.png');
|
|
|
|
});
|
|
|
|
});
|