summaryrefslogtreecommitdiff
path: root/spec/frontend/code_navigation/components/app_spec.js
blob: f2f97092c5afd81fecfa2f1793e226cd60d3436b (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
import { shallowMount } from '@vue/test-utils';
import Vue from 'vue';
import Vuex from 'vuex';
import App from '~/code_navigation/components/app.vue';
import Popover from '~/code_navigation/components/popover.vue';
import createState from '~/code_navigation/store/state';

const setInitialData = jest.fn();
const fetchData = jest.fn();
const showDefinition = jest.fn();
let wrapper;

Vue.use(Vuex);

function factory(initialState = {}, props = {}) {
  const store = new Vuex.Store({
    state: {
      ...createState(),
      ...initialState,
      definitionPathPrefix: 'https://test.com/blob/main',
    },
    actions: {
      setInitialData,
      fetchData,
      showDefinition,
    },
  });

  wrapper = shallowMount(App, { store, propsData: { ...props } });
}

describe('Code navigation app component', () => {
  afterEach(() => {
    wrapper.destroy();
  });

  it('sets initial data on mount if the correct props are passed', () => {
    const codeNavigationPath = 'code/nav/path.js';
    const path = 'blob/path.js';
    const definitionPathPrefix = 'path/prefix';
    const wrapTextNodes = true;

    factory(
      {},
      { codeNavigationPath, blobPath: path, pathPrefix: definitionPathPrefix, wrapTextNodes },
    );

    expect(setInitialData).toHaveBeenCalledWith(expect.anything(), {
      blobs: [{ codeNavigationPath, path }],
      definitionPathPrefix,
      wrapTextNodes,
    });
  });

  it('fetches data on mount', () => {
    factory();

    expect(fetchData).toHaveBeenCalled();
  });

  it('hides popover when no definition set', () => {
    factory();

    expect(wrapper.find(Popover).exists()).toBe(false);
  });

  it('renders popover when definition set', () => {
    factory({
      currentDefinition: { hover: 'console' },
      currentDefinitionPosition: { x: 0 },
      currentBlobPath: 'index.js',
    });

    expect(wrapper.find(Popover).exists()).toBe(true);
  });

  it('calls showDefinition when clicking blob viewer', () => {
    setFixtures('<div class="blob-viewer"></div>');

    factory();

    document.querySelector('.blob-viewer').click();

    expect(showDefinition).toHaveBeenCalled();
  });
});