2019-08-30 01:34:00 -04:00
|
|
|
import MockAdapter from 'axios-mock-adapter';
|
|
|
|
import $ from 'jquery';
|
|
|
|
import { TEST_HOST } from 'helpers/test_constants';
|
2019-09-27 16:59:41 -04:00
|
|
|
import sanitize from 'sanitize-html';
|
2019-12-09 07:07:58 -05:00
|
|
|
import ProjectFindFile from '~/project_find_file';
|
|
|
|
import axios from '~/lib/utils/axios_utils';
|
2019-09-27 16:59:41 -04:00
|
|
|
|
|
|
|
jest.mock('sanitize-html', () => jest.fn(val => val));
|
2019-08-30 01:34:00 -04:00
|
|
|
|
|
|
|
const BLOB_URL_TEMPLATE = `${TEST_HOST}/namespace/project/blob/master`;
|
|
|
|
const FILE_FIND_URL = `${TEST_HOST}/namespace/project/files/master?format=json`;
|
|
|
|
const FIND_TREE_URL = `${TEST_HOST}/namespace/project/tree/master`;
|
|
|
|
const TEMPLATE = `<div class="file-finder-holder tree-holder js-file-finder" data-blob-url-template="${BLOB_URL_TEMPLATE}" data-file-find-url="${FILE_FIND_URL}" data-find-tree-url="${FIND_TREE_URL}">
|
|
|
|
<input class="file-finder-input" id="file_find" />
|
|
|
|
<div class="tree-content-holder">
|
|
|
|
<div class="table-holder">
|
|
|
|
<table class="files-slider tree-table">
|
|
|
|
<tbody />
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
describe('ProjectFindFile', () => {
|
|
|
|
let element;
|
|
|
|
let mock;
|
|
|
|
|
|
|
|
const getProjectFindFileInstance = () =>
|
|
|
|
new ProjectFindFile(element, {
|
|
|
|
url: FILE_FIND_URL,
|
|
|
|
treeUrl: FIND_TREE_URL,
|
|
|
|
blobUrlTemplate: BLOB_URL_TEMPLATE,
|
|
|
|
});
|
|
|
|
|
|
|
|
const findFiles = () =>
|
|
|
|
element
|
|
|
|
.find('.tree-table tr')
|
|
|
|
.toArray()
|
|
|
|
.map(el => ({
|
|
|
|
text: el.textContent,
|
|
|
|
href: el.querySelector('a').href,
|
|
|
|
}));
|
|
|
|
|
2019-09-27 16:59:41 -04:00
|
|
|
const files = [
|
2019-12-17 04:07:48 -05:00
|
|
|
{ path: 'fileA.txt', escaped: 'fileA.txt' },
|
|
|
|
{ path: 'fileB.txt', escaped: 'fileB.txt' },
|
|
|
|
{ path: 'fi#leC.txt', escaped: 'fi%23leC.txt' },
|
|
|
|
{ path: 'folderA/fileD.txt', escaped: 'folderA/fileD.txt' },
|
|
|
|
{ path: 'folder#B/fileE.txt', escaped: 'folder%23B/fileE.txt' },
|
|
|
|
{ path: 'folde?rC/fil#F.txt', escaped: 'folde%3FrC/fil%23F.txt' },
|
2019-09-27 16:59:41 -04:00
|
|
|
];
|
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
beforeEach(done => {
|
2019-08-30 01:34:00 -04:00
|
|
|
// Create a mock adapter for stubbing axios API requests
|
|
|
|
mock = new MockAdapter(axios);
|
|
|
|
|
|
|
|
element = $(TEMPLATE);
|
2019-12-17 04:07:48 -05:00
|
|
|
mock.onGet(FILE_FIND_URL).replyOnce(200, files.map(x => x.path));
|
2019-09-27 16:59:41 -04:00
|
|
|
getProjectFindFileInstance(); // This triggers a load / axios call + subsequent render in the constructor
|
2019-12-17 04:07:48 -05:00
|
|
|
|
|
|
|
setImmediate(done);
|
2019-08-30 01:34:00 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
// Reset the mock adapter
|
|
|
|
mock.restore();
|
2019-09-27 16:59:41 -04:00
|
|
|
sanitize.mockClear();
|
2019-08-30 01:34:00 -04:00
|
|
|
});
|
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
it('loads and renders elements from remote server', () => {
|
|
|
|
expect(findFiles()).toEqual(
|
|
|
|
files.map(({ path, escaped }) => ({
|
|
|
|
text: path,
|
|
|
|
href: `${BLOB_URL_TEMPLATE}/${escaped}`,
|
|
|
|
})),
|
|
|
|
);
|
2019-08-30 01:34:00 -04:00
|
|
|
});
|
2019-09-27 16:59:41 -04:00
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
it('sanitizes search text', () => {
|
2019-09-27 16:59:41 -04:00
|
|
|
const searchText = element.find('.file-finder-input').val();
|
|
|
|
|
2019-12-17 04:07:48 -05:00
|
|
|
expect(sanitize).toHaveBeenCalledTimes(1);
|
|
|
|
expect(sanitize).toHaveBeenCalledWith(searchText);
|
2019-09-27 16:59:41 -04:00
|
|
|
});
|
2019-08-30 01:34:00 -04:00
|
|
|
});
|