debian-mirror-gitlab/spec/javascripts/blob/sketch/index_spec.js

121 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-08-17 22:00:37 +05:30
/* eslint-disable no-new, promise/catch-or-return */
import JSZip from 'jszip';
import SketchLoader from '~/blob/sketch';
describe('Sketch viewer', () => {
const generateZipFileArrayBuffer = (zipFile, resolve, done) => {
2018-12-13 13:39:08 +05:30
zipFile.generateAsync({ type: 'arrayBuffer' }).then(content => {
resolve(content);
setTimeout(() => {
done();
}, 100);
});
2017-08-17 22:00:37 +05:30
};
2019-05-18 00:54:41 +05:30
preloadFixtures('static/sketch_viewer.html');
2017-08-17 22:00:37 +05:30
beforeEach(() => {
2019-05-18 00:54:41 +05:30
loadFixtures('static/sketch_viewer.html');
2017-08-17 22:00:37 +05:30
});
describe('with error message', () => {
2018-12-13 13:39:08 +05:30
beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake(
() =>
new Promise((resolve, reject) => {
reject();
setTimeout(() => {
done();
});
}),
);
2017-08-17 22:00:37 +05:30
new SketchLoader(document.getElementById('js-sketch-viewer'));
});
it('renders error message', () => {
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#js-sketch-viewer p')).not.toBeNull();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#js-sketch-viewer p').textContent.trim()).toContain(
'Cannot show preview.',
);
2017-08-17 22:00:37 +05:30
});
it('removes render the loading icon', () => {
2018-12-13 13:39:08 +05:30
expect(document.querySelector('.js-loading-icon')).toBeNull();
2017-08-17 22:00:37 +05:30
});
});
describe('success', () => {
2018-12-13 13:39:08 +05:30
beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake(
() =>
new Promise(resolve => {
const zipFile = new JSZip();
zipFile
.folder('previews')
.file(
'preview.png',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAA1JREFUeNoBAgD9/wAAAAIAAVMrnDAAAAAASUVORK5CYII=',
{
base64: true,
},
);
generateZipFileArrayBuffer(zipFile, resolve, done);
}),
);
2017-08-17 22:00:37 +05:30
new SketchLoader(document.getElementById('js-sketch-viewer'));
});
it('does not render error message', () => {
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#js-sketch-viewer p')).toBeNull();
2017-08-17 22:00:37 +05:30
});
it('removes render the loading icon', () => {
2018-12-13 13:39:08 +05:30
expect(document.querySelector('.js-loading-icon')).toBeNull();
2017-08-17 22:00:37 +05:30
});
it('renders preview img', () => {
const img = document.querySelector('#js-sketch-viewer img');
expect(img).not.toBeNull();
2018-11-08 19:23:39 +05:30
expect(img.classList.contains('img-fluid')).toBeTruthy();
2017-08-17 22:00:37 +05:30
});
it('renders link to image', () => {
const img = document.querySelector('#js-sketch-viewer img');
const link = document.querySelector('#js-sketch-viewer a');
expect(link.href).toBe(img.src);
expect(link.target).toBe('_blank');
});
});
describe('incorrect file', () => {
2018-12-13 13:39:08 +05:30
beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake(
() =>
new Promise(resolve => {
const zipFile = new JSZip();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
generateZipFileArrayBuffer(zipFile, resolve, done);
}),
);
2017-08-17 22:00:37 +05:30
new SketchLoader(document.getElementById('js-sketch-viewer'));
});
it('renders error message', () => {
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#js-sketch-viewer p')).not.toBeNull();
2017-08-17 22:00:37 +05:30
2018-12-13 13:39:08 +05:30
expect(document.querySelector('#js-sketch-viewer p').textContent.trim()).toContain(
'Cannot show preview.',
);
2017-08-17 22:00:37 +05:30
});
});
});