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

131 lines
3 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');
2017-03-23 08:37:24 -04:00
beforeEach(() => {
loadFixtures('static/notebook_viewer.html');
2017-03-23 08:37:24 -04:00
});
it('shows loading icon', () => {
renderNotebook();
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.loading')).not.toBeNull();
2017-03-23 08:37:24 -04:00
});
describe('successful response', () => {
2017-12-15 02:10:34 -05:00
let mock;
2018-10-17 03:13:26 -04:00
beforeEach(done => {
2017-12-15 02:10:34 -05:00
mock = new MockAdapter(axios);
mock.onGet('/test').reply(200, {
2018-10-17 03:13:26 -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', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.loading')).toBeNull();
2017-03-23 08:37:24 -04:00
});
it('renders the notebook', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.md')).not.toBeNull();
2017-03-23 08:37:24 -04:00
});
it('renders the markdown cell', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('h1')).not.toBeNull();
2017-03-23 08:37:24 -04:00
2018-10-17 03:13:26 -04:00
expect(document.querySelector('h1').textContent.trim()).toBe('test');
2017-03-23 08:37:24 -04:00
});
it('highlights code', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.token')).not.toBeNull();
2017-03-23 08:37:24 -04:00
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.language-python')).not.toBeNull();
2017-03-23 08:37:24 -04:00
});
});
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-10-17 03:13:26 -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', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.loading')).toBeNull();
2017-03-23 08:37:24 -04:00
});
it('shows error message', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.md').textContent.trim()).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
2018-10-17 03:13:26 -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', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.loading')).toBeNull();
2017-03-23 08:37:24 -04:00
});
it('shows error message', () => {
2018-10-17 03:13:26 -04:00
expect(document.querySelector('.md').textContent.trim()).toBe(
'An error occurred whilst loading the file. Please try again later.',
);
2017-03-23 08:37:24 -04:00
});
});
});