summaryrefslogtreecommitdiff
path: root/spec/frontend/vue_shared/components/expand_button_spec.js
blob: 7874658cc0f3955089d25d45028e34b85c8f192a (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
import { mount } from '@vue/test-utils';
import Vue from 'vue';
import ExpandButton from '~/vue_shared/components/expand_button.vue';

const text = {
  expanded: 'Expanded!',
  short: 'Short',
};

describe('Expand button', () => {
  let wrapper;

  const expanderPrependEl = () => wrapper.find('.js-text-expander-prepend');
  const expanderAppendEl = () => wrapper.find('.js-text-expander-append');

  const factory = (options = {}) => {
    wrapper = mount(ExpandButton, {
      ...options,
    });
  };

  beforeEach(() => {
    factory({
      slots: {
        expanded: `<p>${text.expanded}</p>`,
      },
    });
  });

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

  it('renders the prepended collapse button', () => {
    expect(expanderPrependEl().isVisible()).toBe(true);
    expect(expanderAppendEl().isVisible()).toBe(false);
  });

  it('renders no text when short text is not provided', () => {
    expect(wrapper.find(ExpandButton).text()).toBe('');
  });

  it('does not render expanded text', () => {
    expect(wrapper.find(ExpandButton).text().trim()).not.toBe(text.short);
  });

  describe('when short text is provided', () => {
    beforeEach(() => {
      factory({
        slots: {
          expanded: `<p>${text.expanded}</p>`,
          short: `<p>${text.short}</p>`,
        },
      });
    });

    it('renders short text', () => {
      expect(wrapper.find(ExpandButton).text().trim()).toBe(text.short);
    });

    it('renders button before text', () => {
      expect(expanderPrependEl().isVisible()).toBe(true);
      expect(expanderAppendEl().isVisible()).toBe(false);
      expect(wrapper.find(ExpandButton).element).toMatchSnapshot();
    });
  });

  describe('on click', () => {
    beforeEach((done) => {
      expanderPrependEl().trigger('click');
      Vue.nextTick(done);
    });

    afterEach(() => {
      expanderAppendEl().trigger('click');
    });

    it('renders only the append collapse button', () => {
      expect(expanderAppendEl().isVisible()).toBe(true);
      expect(expanderPrependEl().isVisible()).toBe(false);
    });

    it('renders the expanded text', () => {
      expect(wrapper.find(ExpandButton).text()).toContain(text.expanded);
    });

    describe('when short text is provided', () => {
      beforeEach((done) => {
        factory({
          slots: {
            expanded: `<p>${text.expanded}</p>`,
            short: `<p>${text.short}</p>`,
          },
        });

        expanderPrependEl().trigger('click');
        Vue.nextTick(done);
      });

      it('only renders expanded text', () => {
        expect(wrapper.find(ExpandButton).text().trim()).toBe(text.expanded);
      });

      it('renders button after text', () => {
        expect(expanderPrependEl().isVisible()).toBe(false);
        expect(expanderAppendEl().isVisible()).toBe(true);
        expect(wrapper.find(ExpandButton).element).toMatchSnapshot();
      });
    });
  });

  describe('append button', () => {
    beforeEach((done) => {
      expanderPrependEl().trigger('click');
      Vue.nextTick(done);
    });

    it('clicking hides itself and shows prepend', () => {
      expect(expanderAppendEl().isVisible()).toBe(true);
      expanderAppendEl().trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(expanderPrependEl().isVisible()).toBe(true);
      });
    });

    it('clicking hides expanded text', () => {
      expect(wrapper.find(ExpandButton).text().trim()).toBe(text.expanded);
      expanderAppendEl().trigger('click');

      return wrapper.vm.$nextTick().then(() => {
        expect(wrapper.find(ExpandButton).text().trim()).not.toBe(text.expanded);
      });
    });

    describe('when short text is provided', () => {
      beforeEach((done) => {
        factory({
          slots: {
            expanded: `<p>${text.expanded}</p>`,
            short: `<p>${text.short}</p>`,
          },
        });

        expanderPrependEl().trigger('click');
        Vue.nextTick(done);
      });

      it('clicking reveals short text', () => {
        expect(wrapper.find(ExpandButton).text().trim()).toBe(text.expanded);
        expanderAppendEl().trigger('click');

        return wrapper.vm.$nextTick().then(() => {
          expect(wrapper.find(ExpandButton).text().trim()).toBe(text.short);
        });
      });
    });
  });
});