summaryrefslogtreecommitdiff
path: root/spec/frontend/repository/components/last_commit_spec.js
blob: ccba0982c2604bf57efdbf3a6a5d4c3a3e6f66c1 (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
import { shallowMount } from '@vue/test-utils';
import { GlLoadingIcon } from '@gitlab/ui';
import LastCommit from '~/repository/components/last_commit.vue';
import UserAvatarLink from '~/vue_shared/components/user_avatar/user_avatar_link.vue';

let vm;

function createCommitData(data = {}) {
  const defaultData = {
    sha: '123456789',
    title: 'Commit title',
    titleHtml: 'Commit title',
    message: 'Commit message',
    webPath: '/commit/123',
    authoredDate: '2019-01-01',
    author: {
      name: 'Test',
      avatarUrl: 'https://test.com',
      webPath: '/test',
    },
    pipeline: {
      detailedStatus: {
        detailsPath: 'https://test.com/pipeline',
        icon: 'failed',
        tooltip: 'failed',
        text: 'failed',
        group: {},
      },
    },
  };
  return Object.assign(defaultData, data);
}

function factory(commit = createCommitData(), loading = false) {
  vm = shallowMount(LastCommit, {
    mocks: {
      $apollo: {
        queries: {
          commit: {
            loading: true,
          },
        },
      },
    },
  });
  vm.setData({ commit });
  vm.vm.$apollo.queries.commit.loading = loading;
}

const emptyMessageClass = 'font-italic';

describe('Repository last commit component', () => {
  afterEach(() => {
    vm.destroy();
  });

  it.each`
    loading  | label
    ${true}  | ${'shows'}
    ${false} | ${'hides'}
  `('$label when loading icon $loading is true', ({ loading }) => {
    factory(createCommitData(), loading);

    return vm.vm.$nextTick(() => {
      expect(vm.find(GlLoadingIcon).exists()).toBe(loading);
    });
  });

  it('renders commit widget', () => {
    factory();

    return vm.vm.$nextTick(() => {
      expect(vm.element).toMatchSnapshot();
    });
  });

  it('renders short commit ID', () => {
    factory();

    return vm.vm.$nextTick(() => {
      expect(vm.find('[data-testid="last-commit-id-label"]').text()).toEqual('12345678');
    });
  });

  it('hides pipeline components when pipeline does not exist', () => {
    factory(createCommitData({ pipeline: null }));

    return vm.vm.$nextTick(() => {
      expect(vm.find('.js-commit-pipeline').exists()).toBe(false);
    });
  });

  it('renders pipeline components', () => {
    factory();

    return vm.vm.$nextTick(() => {
      expect(vm.find('.js-commit-pipeline').exists()).toBe(true);
    });
  });

  it('hides author component when author does not exist', () => {
    factory(createCommitData({ author: null }));

    return vm.vm.$nextTick(() => {
      expect(vm.find('.js-user-link').exists()).toBe(false);
      expect(vm.find(UserAvatarLink).exists()).toBe(false);
    });
  });

  it('does not render description expander when description is null', () => {
    factory(createCommitData({ descriptionHtml: null }));

    return vm.vm.$nextTick(() => {
      expect(vm.find('.text-expander').exists()).toBe(false);
      expect(vm.find('.commit-row-description').exists()).toBe(false);
    });
  });

  it('expands commit description when clicking expander', () => {
    factory(createCommitData({ descriptionHtml: 'Test description' }));

    return vm.vm
      .$nextTick()
      .then(() => {
        vm.find('.text-expander').vm.$emit('click');
        return vm.vm.$nextTick();
      })
      .then(() => {
        expect(vm.find('.commit-row-description').isVisible()).toBe(true);
        expect(vm.find('.text-expander').classes('open')).toBe(true);
      });
  });

  it('renders the signature HTML as returned by the backend', () => {
    factory(createCommitData({ signatureHtml: '<button>Verified</button>' }));

    return vm.vm.$nextTick().then(() => {
      expect(vm.element).toMatchSnapshot();
    });
  });

  it('sets correct CSS class if the commit message is empty', () => {
    factory(createCommitData({ message: '' }));

    return vm.vm.$nextTick().then(() => {
      expect(vm.find('.item-title').classes()).toContain(emptyMessageClass);
    });
  });
});