summaryrefslogtreecommitdiff
path: root/spec/frontend/blame/blame_redirect_spec.js
blob: beb10139b3af3dbb3a1aef863b357ef03ae5c528 (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
import redirectToCorrectPage from '~/blame/blame_redirect';
import { setHTMLFixture, resetHTMLFixture } from 'helpers/fixtures';
import { createAlert } from '~/flash';

jest.mock('~/flash');

describe('Blame page redirect', () => {
  beforeEach(() => {
    global.window = Object.create(window);
    const url = 'https://gitlab.com/flightjs/Flight/-/blame/master/file.json';
    Object.defineProperty(window, 'location', {
      writable: true,
      value: {
        href: url,
        hash: '',
        search: '',
      },
    });

    setHTMLFixture(`<div class="js-per-page" data-per-page="1000"></div>`);
  });

  afterEach(() => {
    createAlert.mockClear();
    resetHTMLFixture();
  });

  it('performs redirect to further pages when needed', () => {
    window.location.hash = '#L1001';
    redirectToCorrectPage();
    expect(window.location.href).toMatch('?page=2');
  });

  it('performs redirect back to first page when needed', () => {
    window.location.href = 'https://gitlab.com/flightjs/Flight/-/blame/master/file.json';
    window.location.search = '?page=200';
    window.location.hash = '#L999';
    redirectToCorrectPage();
    expect(window.location.href).toMatch('?page=1');
  });

  it('doesn`t perform redirect when the line is still on page 1', () => {
    window.location.hash = '#L1000';
    redirectToCorrectPage();
    expect(window.location.href).not.toMatch('?page');
  });

  it('doesn`t perform redirect when "no_pagination" param is present', () => {
    window.location.href = 'https://gitlab.com/flightjs/Flight/-/blame/master/file.json';
    window.location.search = '?no_pagination=true';
    window.location.hash = '#L1001';
    redirectToCorrectPage();
    expect(window.location.href).not.toMatch('?page');
  });

  it('doesn`t perform redirect when perPage is not present', () => {
    setHTMLFixture(`<div class="js-per-page"></div>`);
    window.location.hash = '#L1001';
    redirectToCorrectPage();
    expect(window.location.href).not.toMatch('?page');
  });

  it('shows alert with a message', () => {
    window.location.hash = '#L1001';
    redirectToCorrectPage();
    expect(createAlert).toHaveBeenCalledWith({
      message: 'Please wait a few moments while we load the file history for this line.',
    });
  });
});