gitlab-org--gitlab-foss/spec/javascripts/blob/notebook/index_spec.js

151 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-12-15 02:10:34 -05:00
import MockAdapter from 'axios-mock-adapter';
import axios from '~/lib/utils/axios_utils';
2017-03-23 08:37:24 -04:00
import renderNotebook from '~/blob/notebook';
describe('iPython notebook renderer', () => {
preloadFixtures('static/notebook_viewer.html.raw');
beforeEach(() => {
loadFixtures('static/notebook_viewer.html.raw');
});
it('shows loading icon', () => {
renderNotebook();
expect(
document.querySelector('.loading'),
).not.toBeNull();
});
describe('successful response', () => {
2017-12-15 02:10:34 -05:00
let mock;
beforeEach((done) => {
mock = new MockAdapter(axios);
mock.onGet('/test').reply(200, {
2017-03-23 08:37:24 -04:00
cells: [{
cell_type: 'markdown',
source: ['# test'],
}, {
cell_type: 'code',
execution_count: 1,
source: [
'def test(str)',
' return str',
],
outputs: [],
}],
2017-12-15 02:10:34 -05:00
});
2017-03-23 08:37:24 -04:00
renderNotebook();
setTimeout(() => {
done();
});
});
afterEach(() => {
mock.restore();
2017-03-23 08:37:24 -04:00
});
it('does not show loading icon', () => {
expect(
document.querySelector('.loading'),
).toBeNull();
});
it('renders the notebook', () => {
expect(
document.querySelector('.md'),
).not.toBeNull();
});
it('renders the markdown cell', () => {
expect(
document.querySelector('h1'),
).not.toBeNull();
expect(
document.querySelector('h1').textContent.trim(),
).toBe('test');
});
it('highlights code', () => {
expect(
document.querySelector('.token'),
2017-03-23 08:37:24 -04:00
).not.toBeNull();
expect(
document.querySelector('.language-python'),
2017-03-23 08:37:24 -04:00
).not.toBeNull();
});
});
describe('error in JSON response', () => {
2017-12-15 02:10:34 -05:00
let mock;
2017-03-23 08:37:24 -04:00
2018-05-28 07:41:30 -04:00
beforeEach(done => {
2017-12-15 02:10:34 -05:00
mock = new MockAdapter(axios);
2018-05-28 07:41:30 -04:00
mock
.onGet('/test')
.reply(() =>
// eslint-disable-next-line prefer-promise-reject-errors
Promise.reject({ status: 200, data: '{ "cells": [{"cell_type": "markdown"} }' }),
);
2017-03-23 08:37:24 -04:00
renderNotebook();
setTimeout(() => {
done();
});
});
afterEach(() => {
mock.restore();
2017-03-23 08:37:24 -04:00
});
it('does not show loading icon', () => {
expect(
document.querySelector('.loading'),
).toBeNull();
});
it('shows error message', () => {
expect(
document.querySelector('.md').textContent.trim(),
2017-09-25 16:02:08 -04:00
).toBe('An error occurred whilst parsing the file.');
2017-03-23 08:37:24 -04:00
});
});
describe('error getting file', () => {
2017-12-15 02:10:34 -05:00
let mock;
2017-03-23 08:37:24 -04:00
beforeEach((done) => {
2017-12-15 02:10:34 -05:00
mock = new MockAdapter(axios);
mock.onGet('/test').reply(500, '');
2017-03-23 08:37:24 -04:00
renderNotebook();
setTimeout(() => {
done();
});
});
afterEach(() => {
mock.restore();
2017-03-23 08:37:24 -04:00
});
it('does not show loading icon', () => {
expect(
document.querySelector('.loading'),
).toBeNull();
});
it('shows error message', () => {
expect(
document.querySelector('.md').textContent.trim(),
2017-09-25 16:02:08 -04:00
).toBe('An error occurred whilst loading the file. Please try again later.');
2017-03-23 08:37:24 -04:00
});
});
});