summaryrefslogtreecommitdiff
path: root/spec/javascripts/avatar_helper_spec.js
blob: c1ef08e0f1b5901b4e31d880bc225bac728d4a10 (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
import { TEST_HOST } from 'spec/test_constants';
import { getFirstCharacterCapitalized } from '~/lib/utils/text_utility';
import {
  DEFAULT_SIZE_CLASS,
  IDENTICON_BG_COUNT,
  renderAvatar,
  renderIdenticon,
  getIdenticonBackgroundClass,
  getIdenticonTitle,
} from '~/helpers/avatar_helper';

function matchAll(str) {
  return new RegExp(`^${str}$`);
}

describe('avatar_helper', () => {
  describe('getIdenticonBackgroundClass', () => {
    it('returns identicon bg class from id', () => {
      expect(getIdenticonBackgroundClass(1)).toEqual('bg2');
    });

    it(`wraps around if id is bigger than ${IDENTICON_BG_COUNT}`, () => {
      expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT + 4)).toEqual('bg5');
      expect(getIdenticonBackgroundClass(IDENTICON_BG_COUNT * 5 + 6)).toEqual('bg7');
    });
  });

  describe('getIdenticonTitle', () => {
    it('returns identicon title from name', () => {
      expect(getIdenticonTitle('Lorem')).toEqual('L');
      expect(getIdenticonTitle('dolar-sit-amit')).toEqual('D');
      expect(getIdenticonTitle('%-with-special-chars')).toEqual('%');
    });

    it('returns space if name is falsey', () => {
      expect(getIdenticonTitle('')).toEqual(' ');
      expect(getIdenticonTitle(null)).toEqual(' ');
    });
  });

  describe('renderIdenticon', () => {
    it('renders with the first letter as title and bg based on id', () => {
      const entity = {
        id: IDENTICON_BG_COUNT + 3,
        name: 'Xavior',
      };
      const options = {
        sizeClass: 's32',
      };

      const result = renderIdenticon(entity, options);

      expect(result).toHaveClass(`identicon ${options.sizeClass} bg4`);
      expect(result).toHaveText(matchAll(getFirstCharacterCapitalized(entity.name)));
    });

    it('renders with defaults, if no options are given', () => {
      const entity = {
        id: 1,
        name: 'tanuki',
      };

      const result = renderIdenticon(entity);

      expect(result).toHaveClass(`identicon ${DEFAULT_SIZE_CLASS} bg2`);
      expect(result).toHaveText(matchAll(getFirstCharacterCapitalized(entity.name)));
    });
  });

  describe('renderAvatar', () => {
    it('renders an image with the avatarUrl', () => {
      const avatarUrl = `${TEST_HOST}/not-real-assets/test.png`;

      const result = renderAvatar({
        avatar_url: avatarUrl,
      });

      expect(result).toBeMatchedBy('img');
      expect(result).toHaveAttr('src', avatarUrl);
      expect(result).toHaveClass(DEFAULT_SIZE_CLASS);
    });

    it('renders an identicon if no avatarUrl', () => {
      const entity = {
        id: 1,
        name: 'walrus',
      };
      const options = {
        sizeClass: 's16',
      };

      const result = renderAvatar(entity, options);

      expect(result).toHaveClass(`identicon ${options.sizeClass} bg2`);
      expect(result).toHaveText(matchAll(getFirstCharacterCapitalized(entity.name)));
    });
  });
});