summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/components/preview/navigator_spec.js
blob: 576d2fae003ed4312dd3ee2df276a355aba15e3a (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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
import Vue from 'vue';
import ClientsideNavigator from '~/ide/components/preview/navigator.vue';
import mountComponent from 'spec/helpers/vue_mount_component_helper';

describe('IDE clientside preview navigator', () => {
  let vm;
  let Component;
  let manager;

  beforeAll(() => {
    Component = Vue.extend(ClientsideNavigator);
  });

  beforeEach(() => {
    manager = {
      bundlerURL: gl.TEST_HOST,
      iframe: { src: '' },
    };

    vm = mountComponent(Component, {
      manager,
    });
  });

  afterEach(() => {
    vm.$destroy();
  });

  it('renders readonly URL bar', () => {
    expect(vm.$el.querySelector('input[readonly]').value).toBe('/');
  });

  it('disables back button when navigationStack is empty', () => {
    expect(vm.$el.querySelector('.ide-navigator-btn')).toHaveAttr('disabled');
    expect(vm.$el.querySelector('.ide-navigator-btn').classList).toContain('disabled-content');
  });

  it('disables forward button when forwardNavigationStack is empty', () => {
    vm.forwardNavigationStack = [];

    expect(vm.$el.querySelectorAll('.ide-navigator-btn')[1]).toHaveAttr('disabled');
    expect(vm.$el.querySelectorAll('.ide-navigator-btn')[1].classList).toContain(
      'disabled-content',
    );
  });

  it('calls back method when clicking back button', done => {
    vm.navigationStack.push('/test');
    vm.navigationStack.push('/test2');
    spyOn(vm, 'back');

    vm.$nextTick(() => {
      vm.$el.querySelector('.ide-navigator-btn').click();

      expect(vm.back).toHaveBeenCalled();

      done();
    });
  });

  it('calls forward method when clicking forward button', done => {
    vm.forwardNavigationStack.push('/test');
    spyOn(vm, 'forward');

    vm.$nextTick(() => {
      vm.$el.querySelectorAll('.ide-navigator-btn')[1].click();

      expect(vm.forward).toHaveBeenCalled();

      done();
    });
  });

  describe('onUrlChange', () => {
    it('updates the path', () => {
      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      expect(vm.path).toBe('/url');
    });

    it('sets currentBrowsingIndex 0 if not already set', () => {
      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      expect(vm.currentBrowsingIndex).toBe(0);
    });

    it('increases currentBrowsingIndex if path doesnt match', () => {
      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url2`,
      });

      expect(vm.currentBrowsingIndex).toBe(1);
    });

    it('does not increase currentBrowsingIndex if path matches', () => {
      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      expect(vm.currentBrowsingIndex).toBe(0);
    });

    it('pushes path into navigation stack', () => {
      vm.onUrlChange({
        url: `${gl.TEST_HOST}/url`,
      });

      expect(vm.navigationStack).toEqual(['/url']);
    });
  });

  describe('back', () => {
    beforeEach(() => {
      vm.path = '/test2';
      vm.currentBrowsingIndex = 1;
      vm.navigationStack.push('/test');
      vm.navigationStack.push('/test2');

      spyOn(vm, 'visitPath');

      vm.back();
    });

    it('visits the last entry in navigationStack', () => {
      expect(vm.visitPath).toHaveBeenCalledWith('/test');
    });

    it('adds last entry to forwardNavigationStack', () => {
      expect(vm.forwardNavigationStack).toEqual(['/test2']);
    });

    it('clears navigation stack if currentBrowsingIndex is 1', () => {
      expect(vm.navigationStack).toEqual([]);
    });

    it('sets currentBrowsingIndex to null is currentBrowsingIndex is 1', () => {
      expect(vm.currentBrowsingIndex).toBe(null);
    });
  });

  describe('forward', () => {
    it('calls visitPath with first entry in forwardNavigationStack', () => {
      spyOn(vm, 'visitPath');

      vm.forwardNavigationStack.push('/test');
      vm.forwardNavigationStack.push('/test2');

      vm.forward();

      expect(vm.visitPath).toHaveBeenCalledWith('/test');
    });
  });

  describe('refresh', () => {
    it('calls refresh with current path', () => {
      spyOn(vm, 'visitPath');

      vm.path = '/test';

      vm.refresh();

      expect(vm.visitPath).toHaveBeenCalledWith('/test');
    });
  });

  describe('visitPath', () => {
    it('updates iframe src with passed in path', () => {
      vm.visitPath('/testpath');

      expect(manager.iframe.src).toBe(`${gl.TEST_HOST}/testpath`);
    });
  });
});