Welcome to mirror list, hosted at ThFree Co, Russian Federation.

user_avatar_spec.js « components « edit « profile « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c4f74d6bfbe7b60c118271a6e7d6555422fa746 (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
import { nextTick } from 'vue';
import jQuery from 'jquery';
import { GlAvatar, GlAvatarLink, GlSprintf } from '@gitlab/ui';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { avatarI18n } from '~/profile/edit/constants';
import { loadCSSFile } from '~/lib/utils/css_utils';

import UserAvatar from '~/profile/edit/components/user_avatar.vue';

const glCropDataMock = jest.fn().mockImplementation(() => ({
  getBlob: jest.fn(),
}));

const jQueryMock = {
  glCrop: jest.fn().mockReturnValue({
    data: glCropDataMock,
  }),
};

jest.mock(`~/lib/utils/css_utils`);
jest.mock('jquery');

describe('Edit User Avatar', () => {
  let wrapper;

  beforeEach(() => {
    jQuery.mockImplementation(() => jQueryMock);
  });

  const defaultProvides = {
    avatarUrl: '/-/profile/avatarUrl',
    brandProfileImageGuidelines: '',
    cropperCssPath: '',
    hasAvatar: true,
    gravatarEnabled: true,
    gravatarLink: {
      hostname: 'gravatar.com',
      url: 'gravatar.com',
    },
    profileAvatarPath: '/profile/avatar',
  };

  const createComponent = (provides = {}) => {
    wrapper = shallowMountExtended(UserAvatar, {
      provide: {
        ...defaultProvides,
        ...provides,
      },
      attachTo: document.body,
    });
  };

  const findAvatar = () => wrapper.findComponent(GlAvatar);
  const findAvatarLink = () => wrapper.findComponent(GlAvatarLink);
  const findHelpText = () => wrapper.findComponent(GlSprintf).attributes('message');
  const findRemoveAvatarButton = () => wrapper.findByTestId('remove-avatar-button');

  describe('renders correctly', () => {
    it('under default condition', async () => {
      createComponent();
      await nextTick();

      expect(jQueryMock.glCrop).toHaveBeenCalledWith({
        filename: '.js-avatar-filename',
        previewImage: '.avatar-image .gl-avatar',
        modalCrop: '.modal-profile-crop',
        pickImageEl: '.js-choose-user-avatar-button',
        uploadImageBtn: '.js-upload-user-avatar',
        modalCropImg: expect.any(HTMLImageElement),
        onBlobChange: expect.any(Function),
      });
      expect(glCropDataMock).toHaveBeenCalledWith('glcrop');
      expect(loadCSSFile).toHaveBeenCalledWith(defaultProvides.cropperCssPath);
      const avatar = findAvatar();

      expect(avatar.exists()).toBe(true);
      expect(avatar.attributes('src')).toBe(defaultProvides.avatarUrl);
      expect(findAvatarLink().attributes('href')).toBe(defaultProvides.avatarUrl);

      const removeAvatarButton = findRemoveAvatarButton();
      expect(removeAvatarButton.exists()).toBe(true);
      expect(removeAvatarButton.attributes('href')).toBe(defaultProvides.profileAvatarPath);
    });

    describe('when user has avatar', () => {
      describe('while gravatar is enabled', () => {
        it('shows help text for change or remove avatar', () => {
          createComponent({
            gravatarEnabled: true,
          });

          expect(findHelpText()).toBe(avatarI18n.changeOrRemoveAvatar);
        });
      });
      describe('while gravatar is disabled', () => {
        it('shows help text for change avatar', () => {
          createComponent({
            gravatarEnabled: false,
          });

          expect(findHelpText()).toBe(avatarI18n.changeAvatar);
        });
      });
    });

    describe('when user does not have an avatar', () => {
      describe('while gravatar is enabled', () => {
        it('shows help text for upload or change avatar', () => {
          createComponent({
            gravatarEnabled: true,
            hasAvatar: false,
          });
          expect(findHelpText()).toBe(avatarI18n.uploadOrChangeAvatar);
        });
      });

      describe('while gravatar is disabled', () => {
        it('shows help text for upload avatar', () => {
          createComponent({
            gravatarEnabled: false,
            hasAvatar: false,
          });
          expect(findHelpText()).toBe(avatarI18n.uploadAvatar);
          expect(findRemoveAvatarButton().exists()).toBe(false);
        });
      });
    });
  });

  it('can render profile image guidelines', () => {
    const brandProfileImageGuidelines = 'brandProfileImageGuidelines';
    createComponent({
      brandProfileImageGuidelines,
    });

    expect(wrapper.findByTestId('brand-profile-image-guidelines').text()).toBe(
      brandProfileImageGuidelines,
    );
  });
});