Commit 13da9216 authored by Kushal Pandya's avatar Kushal Pandya

Merge branch '194207-migrate-sketch-index-spec-to-jest' into 'master'

Migrate sketch index_spec to jest

See merge request gitlab-org/gitlab!26889
parents 0b6ed03c a22f54ba
/* eslint-disable no-new, promise/catch-or-return */
import JSZip from 'jszip'; import JSZip from 'jszip';
import SketchLoader from '~/blob/sketch'; import SketchLoader from '~/blob/sketch';
describe('Sketch viewer', () => { jest.mock('jszip');
const generateZipFileArrayBuffer = (zipFile, resolve, done) => {
zipFile.generateAsync({ type: 'arrayBuffer' }).then(content => {
resolve(content);
setTimeout(() => {
done();
}, 100);
});
};
describe('Sketch viewer', () => {
preloadFixtures('static/sketch_viewer.html'); preloadFixtures('static/sketch_viewer.html');
beforeEach(() => { beforeEach(() => {
loadFixtures('static/sketch_viewer.html'); loadFixtures('static/sketch_viewer.html');
window.URL = {
createObjectURL: jest.fn(() => 'http://foo/bar'),
};
});
afterEach(() => {
window.URL = {};
}); });
describe('with error message', () => { describe('with error message', () => {
beforeEach(done => { beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake( jest.spyOn(SketchLoader.prototype, 'getZipFile').mockImplementation(
() => () =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
reject(); reject();
done();
setTimeout(() => {
done();
});
}), }),
); );
new SketchLoader(document.getElementById('js-sketch-viewer')); return new SketchLoader(document.getElementById('js-sketch-viewer'));
}); });
it('renders error message', () => { it('renders error message', () => {
...@@ -43,39 +38,39 @@ describe('Sketch viewer', () => { ...@@ -43,39 +38,39 @@ describe('Sketch viewer', () => {
); );
}); });
it('removes render the loading icon', () => { it('removes the loading icon', () => {
expect(document.querySelector('.js-loading-icon')).toBeNull(); expect(document.querySelector('.js-loading-icon')).toBeNull();
}); });
}); });
describe('success', () => { describe('success', () => {
beforeEach(done => { beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake( const loadAsyncMock = {
files: {
'previews/preview.png': {
async: jest.fn(),
},
},
};
loadAsyncMock.files['previews/preview.png'].async.mockImplementation(
() => () =>
new Promise(resolve => { new Promise(resolve => {
const zipFile = new JSZip(); resolve('foo');
zipFile done();
.folder('previews')
.file(
'preview.png',
'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAMAAAAoyzS7AAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAA1JREFUeNoBAgD9/wAAAAIAAVMrnDAAAAAASUVORK5CYII=',
{
base64: true,
},
);
generateZipFileArrayBuffer(zipFile, resolve, done);
}), }),
); );
new SketchLoader(document.getElementById('js-sketch-viewer')); 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', () => { it('does not render error message', () => {
expect(document.querySelector('#js-sketch-viewer p')).toBeNull(); expect(document.querySelector('#js-sketch-viewer p')).toBeNull();
}); });
it('removes render the loading icon', () => { it('removes the loading icon', () => {
expect(document.querySelector('.js-loading-icon')).toBeNull(); expect(document.querySelector('.js-loading-icon')).toBeNull();
}); });
...@@ -94,27 +89,4 @@ describe('Sketch viewer', () => { ...@@ -94,27 +89,4 @@ describe('Sketch viewer', () => {
expect(link.target).toBe('_blank'); expect(link.target).toBe('_blank');
}); });
}); });
describe('incorrect file', () => {
beforeEach(done => {
spyOn(SketchLoader.prototype, 'getZipFile').and.callFake(
() =>
new Promise(resolve => {
const zipFile = new JSZip();
generateZipFileArrayBuffer(zipFile, resolve, done);
}),
);
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.',
);
});
});
}); });
// this file can't be migrated to jest because it relies on the browser to perform integration tests:
// see: https://gitlab.com/gitlab-org/gitlab/-/issues/194207#note_301878738
import { FIXTURES_PATH } from 'spec/test_constants'; import { FIXTURES_PATH } from 'spec/test_constants';
import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer'; import BalsamiqViewer from '~/blob/balsamiq/balsamiq_viewer';
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment