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

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2020-04-08 14:13:33 +05:30
import JSZip from 'jszip';
import SketchLoader from '~/blob/sketch';
jest.mock('jszip');
describe('Sketch viewer', () => {
beforeEach(() => {
loadFixtures('static/sketch_viewer.html');
});
describe('with error message', () => {
2021-03-08 18:12:59 +05:30
beforeEach((done) => {
2020-04-08 14:13:33 +05:30
jest.spyOn(SketchLoader.prototype, 'getZipFile').mockImplementation(
() =>
new Promise((resolve, reject) => {
reject();
done();
}),
);
return new SketchLoader(document.getElementById('js-sketch-viewer'));
});
it('renders error message', () => {
expect(document.querySelector('#js-sketch-viewer p')).not.toBeNull();
expect(document.querySelector('#js-sketch-viewer p').textContent.trim()).toContain(
'Cannot show preview.',
);
});
it('removes the loading icon', () => {
expect(document.querySelector('.js-loading-icon')).toBeNull();
});
});
describe('success', () => {
2021-03-08 18:12:59 +05:30
beforeEach((done) => {
2020-04-08 14:13:33 +05:30
const loadAsyncMock = {
files: {
'previews/preview.png': {
async: jest.fn(),
},
},
};
loadAsyncMock.files['previews/preview.png'].async.mockImplementation(
() =>
2021-03-08 18:12:59 +05:30
new Promise((resolve) => {
2020-04-08 14:13:33 +05:30
resolve('foo');
done();
}),
);
jest.spyOn(SketchLoader.prototype, 'getZipFile').mockResolvedValue();
jest.spyOn(JSZip, 'loadAsync').mockResolvedValue(loadAsyncMock);
return new SketchLoader(document.getElementById('js-sketch-viewer'));
});
it('does not render error message', () => {
expect(document.querySelector('#js-sketch-viewer p')).toBeNull();
});
it('removes the loading icon', () => {
expect(document.querySelector('.js-loading-icon')).toBeNull();
});
it('renders preview img', () => {
const img = document.querySelector('#js-sketch-viewer img');
expect(img).not.toBeNull();
expect(img.classList.contains('img-fluid')).toBeTruthy();
});
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');
});
});
});