summaryrefslogtreecommitdiff
path: root/spec/frontend/releases/components/release_block_assets_spec.js
blob: a85532a811847e3c7cf08bbb24b73e80d43f7673 (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
import { mount } from '@vue/test-utils';
import { GlCollapse } from '@gitlab/ui';
import ReleaseBlockAssets from '~/releases/components/release_block_assets.vue';
import { ASSET_LINK_TYPE } from '~/releases/constants';
import { trimText } from 'helpers/text_helper';
import { assets } from '../mock_data';
import { cloneDeep } from 'lodash';

describe('Release block assets', () => {
  let wrapper;
  let defaultProps;

  // A map of types to the expected section heading text
  const sections = {
    [ASSET_LINK_TYPE.IMAGE]: 'Images',
    [ASSET_LINK_TYPE.PACKAGE]: 'Packages',
    [ASSET_LINK_TYPE.RUNBOOK]: 'Runbooks',
    [ASSET_LINK_TYPE.OTHER]: 'Other',
  };

  const createComponent = (propsData = defaultProps) => {
    wrapper = mount(ReleaseBlockAssets, {
      provide: {
        glFeatures: { releaseAssetLinkType: true },
      },
      propsData,
    });
  };

  const findSectionHeading = type =>
    wrapper.findAll('h5').filter(h5 => h5.text() === sections[type]);

  beforeEach(() => {
    defaultProps = { assets: cloneDeep(assets) };
  });

  describe('with default props', () => {
    beforeEach(() => createComponent());

    const findAccordionButton = () => wrapper.find('[data-testid="accordion-button"]');

    it('renders an "Assets" accordion with the asset count', () => {
      const accordionButton = findAccordionButton();

      expect(accordionButton.exists()).toBe(true);
      expect(trimText(accordionButton.text())).toBe('Assets 5');
    });

    it('renders the accordion as expanded by default', () => {
      const accordion = wrapper.find(GlCollapse);

      expect(accordion.exists()).toBe(true);
      expect(accordion.isVisible()).toBe(true);
    });

    it('renders sources with the expected text and URL', () => {
      defaultProps.assets.sources.forEach(s => {
        const sourceLink = wrapper.find(`li>a[href="${s.url}"]`);

        expect(sourceLink.exists()).toBe(true);
        expect(sourceLink.text()).toBe(`Source code (${s.format})`);
      });
    });

    it('renders a heading for each assets type (except sources)', () => {
      Object.keys(sections).forEach(type => {
        const sectionHeadings = findSectionHeading(type);

        expect(sectionHeadings).toHaveLength(1);
      });
    });

    it('renders asset links with the expected text and URL', () => {
      defaultProps.assets.links.forEach(l => {
        const sourceLink = wrapper.find(`li>a[href="${l.directAssetUrl}"]`);

        expect(sourceLink.exists()).toBe(true);
        expect(sourceLink.text()).toBe(l.name);
      });
    });
  });

  describe("when a release doesn't have a link with a certain asset type", () => {
    const typeToExclude = ASSET_LINK_TYPE.IMAGE;

    beforeEach(() => {
      defaultProps.assets.links = defaultProps.assets.links.filter(
        l => l.linkType !== typeToExclude,
      );
      createComponent(defaultProps);
    });

    it('does not render a section heading if there are no links of that type', () => {
      const sectionHeadings = findSectionHeading(typeToExclude);

      expect(sectionHeadings).toHaveLength(0);
    });
  });

  describe('sources', () => {
    const testSources = ({ shouldSourcesBeRendered }) => {
      assets.sources.forEach(s => {
        expect(wrapper.find(`a[href="${s.url}"]`).exists()).toBe(shouldSourcesBeRendered);
      });
    };

    describe('when the release has sources', () => {
      beforeEach(() => {
        createComponent(defaultProps);
      });

      it('renders sources', () => {
        testSources({ shouldSourcesBeRendered: true });
      });
    });

    describe('when the release does not have sources', () => {
      beforeEach(() => {
        delete defaultProps.assets.sources;
        createComponent(defaultProps);
      });

      it('does not render any sources', () => {
        testSources({ shouldSourcesBeRendered: false });
      });
    });
  });

  describe('external vs internal links', () => {
    const containsExternalSourceIndicator = () =>
      wrapper.contains('[data-testid="external-link-indicator"]');

    describe('when a link is external', () => {
      beforeEach(() => {
        defaultProps.assets.sources = [];
        defaultProps.assets.links = [
          {
            ...defaultProps.assets.links[0],
            external: true,
          },
        ];
        createComponent(defaultProps);
      });

      it('renders the link with an "external source" indicator', () => {
        expect(containsExternalSourceIndicator()).toBe(true);
      });
    });

    describe('when a link is internal', () => {
      beforeEach(() => {
        defaultProps.assets.sources = [];
        defaultProps.assets.links = [
          {
            ...defaultProps.assets.links[0],
            external: false,
          },
        ];
        createComponent(defaultProps);
      });

      it('renders the link without the "external source" indicator', () => {
        expect(containsExternalSourceIndicator()).toBe(false);
      });
    });
  });
});