diff_file_spec.js 2.99 KB
Newer Older
Felipe Artur's avatar
Felipe Artur committed
1 2 3 4 5 6 7 8 9 10 11 12 13
import Vue from 'vue';
import DiffFileComponent from '~/diffs/components/diff_file.vue';
import store from '~/mr_notes/stores';
import { createComponentWithStore } from 'spec/helpers/vue_mount_component_helper';
import diffFileMockData from '../mock_data/diff_file';

describe('DiffFile', () => {
  let vm;
  const getDiffFileMock = () => Object.assign({}, diffFileMockData);

  beforeEach(() => {
    vm = createComponentWithStore(Vue.extend(DiffFileComponent), store, {
      file: getDiffFileMock(),
14
      canCurrentUserFork: false,
Felipe Artur's avatar
Felipe Artur committed
15 16 17 18 19 20 21 22 23 24
    }).$mount();
  });

  describe('template', () => {
    it('should render component with file header, file content components', () => {
      const el = vm.$el;
      const { fileHash, filePath } = diffFileMockData;

      expect(el.id).toEqual(fileHash);
      expect(el.classList.contains('diff-file')).toEqual(true);
Tim Zallmann's avatar
Tim Zallmann committed
25

Felipe Artur's avatar
Felipe Artur committed
26 27 28 29
      expect(el.querySelectorAll('.diff-content.hidden').length).toEqual(0);
      expect(el.querySelector('.js-file-title')).toBeDefined();
      expect(el.querySelector('.file-title-name').innerText.indexOf(filePath) > -1).toEqual(true);
      expect(el.querySelector('.js-syntax-highlight')).toBeDefined();
Tim Zallmann's avatar
Tim Zallmann committed
30 31 32 33 34 35 36

      expect(vm.file.renderIt).toEqual(false);
      vm.file.renderIt = true;

      vm.$nextTick(() => {
        expect(el.querySelectorAll('.line_content').length > 5).toEqual(true);
      });
Felipe Artur's avatar
Felipe Artur committed
37 38 39 40
    });

    describe('collapsed', () => {
      it('should not have file content', done => {
41
        expect(vm.$el.querySelectorAll('.diff-content').length).toEqual(1);
Felipe Artur's avatar
Felipe Artur committed
42 43
        expect(vm.file.collapsed).toEqual(false);
        vm.file.collapsed = true;
Tim Zallmann's avatar
Tim Zallmann committed
44
        vm.file.renderIt = true;
Felipe Artur's avatar
Felipe Artur committed
45 46

        vm.$nextTick(() => {
47
          expect(vm.$el.querySelectorAll('.diff-content').length).toEqual(0);
Felipe Artur's avatar
Felipe Artur committed
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 91 92 93 94 95 96

          done();
        });
      });

      it('should have collapsed text and link', done => {
        vm.file.collapsed = true;

        vm.$nextTick(() => {
          expect(vm.$el.innerText).toContain('This diff is collapsed');
          expect(vm.$el.querySelectorAll('.js-click-to-expand').length).toEqual(1);

          done();
        });
      });

      it('should have loading icon while loading a collapsed diffs', done => {
        vm.file.collapsed = true;
        vm.isLoadingCollapsedDiff = true;

        vm.$nextTick(() => {
          expect(vm.$el.querySelectorAll('.diff-content.loading').length).toEqual(1);

          done();
        });
      });
    });
  });

  describe('too large diff', () => {
    it('should have too large warning and blob link', done => {
      const BLOB_LINK = '/file/view/path';
      vm.file.tooLarge = true;
      vm.file.viewPath = BLOB_LINK;

      vm.$nextTick(() => {
        expect(vm.$el.innerText).toContain(
          'This source diff could not be displayed because it is too large',
        );
        expect(vm.$el.querySelector('.js-too-large-diff')).toBeDefined();
        expect(vm.$el.querySelector('.js-too-large-diff a').href.indexOf(BLOB_LINK) > -1).toEqual(
          true,
        );

        done();
      });
    });
  });
});