summaryrefslogtreecommitdiff
path: root/spec/frontend/terms/components/app_spec.js
blob: f1dbc004da8f3a772e75ad8a39433983e8cb9a3c (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
import $ from 'jquery';
import { merge } from 'lodash';
import { GlIntersectionObserver } from '@gitlab/ui';
import { nextTick } from 'vue';

import { mountExtended } from 'helpers/vue_test_utils_helper';
import { FLASH_TYPES, FLASH_CLOSED_EVENT } from '~/flash';
import { isLoggedIn } from '~/lib/utils/common_utils';
import TermsApp from '~/terms/components/app.vue';

jest.mock('~/lib/utils/csrf', () => ({ token: 'mock-csrf-token' }));
jest.mock('~/lib/utils/common_utils');

describe('TermsApp', () => {
  let wrapper;
  let renderGFMSpy;

  const defaultProvide = {
    terms: 'foo bar',
    paths: {
      accept: '/-/users/terms/1/accept',
      decline: '/-/users/terms/1/decline',
      root: '/',
    },
    permissions: {
      canAccept: true,
      canDecline: true,
    },
  };

  const createComponent = (provide = {}) => {
    wrapper = mountExtended(TermsApp, {
      provide: merge({}, defaultProvide, provide),
    });
  };

  beforeEach(() => {
    renderGFMSpy = jest.spyOn($.fn, 'renderGFM');
    isLoggedIn.mockReturnValue(true);
  });

  afterEach(() => {
    wrapper.destroy();
  });

  const findFormWithAction = (path) => wrapper.find(`form[action="${path}"]`);
  const findButton = (path) => findFormWithAction(path).find('button[type="submit"]');
  const findScrollableViewport = () => wrapper.findByTestId('scrollable-viewport');

  const expectFormWithSubmitButton = (buttonText, path) => {
    const form = findFormWithAction(path);
    const submitButton = findButton(path);

    expect(form.exists()).toBe(true);
    expect(submitButton.exists()).toBe(true);
    expect(submitButton.text()).toBe(buttonText);
    expect(
      form
        .find('input[type="hidden"][name="authenticity_token"][value="mock-csrf-token"]')
        .exists(),
    ).toBe(true);
  };

  it('renders terms of service as markdown', () => {
    createComponent();

    expect(wrapper.findByText(defaultProvide.terms).exists()).toBe(true);
    expect(renderGFMSpy).toHaveBeenCalled();
  });

  describe('accept button', () => {
    it('is disabled until user scrolls to the bottom of the terms', async () => {
      createComponent();

      expect(findButton(defaultProvide.paths.accept).attributes('disabled')).toBe('disabled');

      wrapper.findComponent(GlIntersectionObserver).vm.$emit('appear');

      await nextTick();

      expect(findButton(defaultProvide.paths.accept).attributes('disabled')).toBeUndefined();
    });

    describe('when user has permissions to accept', () => {
      it('renders form and button to accept terms', () => {
        createComponent();

        expectFormWithSubmitButton(TermsApp.i18n.accept, defaultProvide.paths.accept);
      });
    });

    describe('when user does not have permissions to accept', () => {
      it('renders continue button', () => {
        createComponent({ permissions: { canAccept: false } });

        expect(wrapper.findByText(TermsApp.i18n.continue).exists()).toBe(true);
      });
    });
  });

  describe('decline button', () => {
    describe('when user has permissions to decline', () => {
      it('renders form and button to decline terms', () => {
        createComponent();

        expectFormWithSubmitButton(TermsApp.i18n.decline, defaultProvide.paths.decline);
      });
    });

    describe('when user does not have permissions to decline', () => {
      it('does not render decline button', () => {
        createComponent({ permissions: { canDecline: false } });

        expect(wrapper.findByText(TermsApp.i18n.decline).exists()).toBe(false);
      });
    });
  });

  it('sets height of scrollable viewport', () => {
    jest.spyOn(document.documentElement, 'scrollHeight', 'get').mockImplementation(() => 800);
    jest.spyOn(document.documentElement, 'clientHeight', 'get').mockImplementation(() => 600);

    createComponent();

    expect(findScrollableViewport().attributes('style')).toBe('max-height: calc(100vh - 200px);');
  });

  describe('when flash is closed', () => {
    let flashEl;

    beforeEach(() => {
      flashEl = document.createElement('div');
      flashEl.classList.add(`flash-${FLASH_TYPES.ALERT}`);
      document.body.appendChild(flashEl);
    });

    afterEach(() => {
      document.body.innerHTML = '';
    });

    it('recalculates height of scrollable viewport', () => {
      jest.spyOn(document.documentElement, 'scrollHeight', 'get').mockImplementation(() => 800);
      jest.spyOn(document.documentElement, 'clientHeight', 'get').mockImplementation(() => 600);

      createComponent();

      expect(findScrollableViewport().attributes('style')).toBe('max-height: calc(100vh - 200px);');

      jest.spyOn(document.documentElement, 'scrollHeight', 'get').mockImplementation(() => 700);
      jest.spyOn(document.documentElement, 'clientHeight', 'get').mockImplementation(() => 600);

      flashEl.dispatchEvent(new Event(FLASH_CLOSED_EVENT));

      expect(findScrollableViewport().attributes('style')).toBe('max-height: calc(100vh - 100px);');
    });
  });

  describe('when user is signed out', () => {
    beforeEach(() => {
      isLoggedIn.mockReturnValue(false);
    });

    it('does not show any buttons', () => {
      createComponent();

      expect(wrapper.findByText(TermsApp.i18n.accept).exists()).toBe(false);
      expect(wrapper.findByText(TermsApp.i18n.decline).exists()).toBe(false);
      expect(wrapper.findByText(TermsApp.i18n.continue).exists()).toBe(false);
    });
  });
});