summaryrefslogtreecommitdiff
path: root/spec/frontend/notebook/cells/code_spec.js
blob: 33dabe2b6dcce363ae8396acb0419c398087f136 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import Vue from 'vue';
import CodeComponent from '~/notebook/cells/code.vue';

const Component = Vue.extend(CodeComponent);

describe('Code component', () => {
  let vm;
  let json;

  beforeEach(() => {
    json = getJSONFixture('blob/notebook/basic.json');
  });

  const setupComponent = cell => {
    const comp = new Component({
      propsData: {
        cell,
      },
    });
    comp.$mount();
    return comp;
  };

  describe('without output', () => {
    beforeEach(done => {
      vm = setupComponent(json.cells[0]);

      setImmediate(() => {
        done();
      });
    });

    it('does not render output prompt', () => {
      expect(vm.$el.querySelectorAll('.prompt').length).toBe(1);
    });
  });

  describe('with output', () => {
    beforeEach(done => {
      vm = setupComponent(json.cells[2]);

      setImmediate(() => {
        done();
      });
    });

    it('does not render output prompt', () => {
      expect(vm.$el.querySelectorAll('.prompt').length).toBe(2);
    });

    it('renders output cell', () => {
      expect(vm.$el.querySelector('.output')).toBeDefined();
    });
  });

  describe('with string for output', () => {
    // NBFormat Version 4.1 allows outputs.text to be a string
    beforeEach(() => {
      const cell = json.cells[2];
      cell.outputs[0].text = cell.outputs[0].text.join('');

      vm = setupComponent(cell);
      return vm.$nextTick();
    });

    it('does not render output prompt', () => {
      expect(vm.$el.querySelectorAll('.prompt').length).toBe(2);
    });

    it('renders output cell', () => {
      expect(vm.$el.querySelector('.output')).toBeDefined();
    });
  });

  describe('with string for cell.source', () => {
    beforeEach(() => {
      const cell = json.cells[0];
      cell.source = cell.source.join('');

      vm = setupComponent(cell);
      return vm.$nextTick();
    });

    it('renders the same input as when cell.source is an array', () => {
      const expected = "console.log('test')";

      expect(vm.$el.querySelector('.input').innerText).toContain(expected);
    });
  });
});