summaryrefslogtreecommitdiff
path: root/spec/frontend/set_status_modal/user_profile_set_status_wrapper_spec.js
blob: a4a2a86dc73bd83db750cea6b63e6fa2f46fd09a (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
import { nextTick } from 'vue';
import { cloneDeep } from 'lodash';
import { mountExtended } from 'helpers/vue_test_utils_helper';
import UserProfileSetStatusWrapper from '~/set_status_modal/user_profile_set_status_wrapper.vue';
import SetStatusForm from '~/set_status_modal/set_status_form.vue';
import { TIME_RANGES_WITH_NEVER, NEVER_TIME_RANGE } from '~/set_status_modal/constants';

describe('UserProfileSetStatusWrapper', () => {
  let wrapper;

  const defaultProvide = {
    fields: {
      emoji: { name: 'user[status][emoji]', id: 'user_status_emoji', value: '8ball' },
      message: { name: 'user[status][message]', id: 'user_status_message', value: 'foo bar' },
      availability: {
        name: 'user[status][availability]',
        id: 'user_status_availability',
        value: 'busy',
      },
      clearStatusAfter: {
        name: 'user[status][clear_status_after]',
        id: 'user_status_clear_status_after',
        value: '2022-09-03 03:06:26 UTC',
      },
    },
  };

  const createComponent = ({ provide = {} } = {}) => {
    wrapper = mountExtended(UserProfileSetStatusWrapper, {
      provide: {
        ...defaultProvide,
        ...provide,
      },
    });
  };

  const findInput = (name) => wrapper.find(`[name="${name}"]`);
  const findSetStatusForm = () => wrapper.findComponent(SetStatusForm);

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

  it('renders `SetStatusForm` component and passes expected props', () => {
    createComponent();

    expect(cloneDeep(findSetStatusForm().props())).toMatchObject({
      defaultEmoji: 'speech_balloon',
      emoji: defaultProvide.fields.emoji.value,
      message: defaultProvide.fields.message.value,
      availability: true,
      clearStatusAfter: null,
      currentClearStatusAfter: defaultProvide.fields.clearStatusAfter.value,
    });
  });

  it.each`
    input
    ${'emoji'}
    ${'message'}
    ${'availability'}
  `('renders hidden $input input with value set', ({ input }) => {
    createComponent();

    expect(findInput(defaultProvide.fields[input].name).attributes('value')).toBe(
      defaultProvide.fields[input].value,
    );
  });

  describe('when clear status after has previously been set', () => {
    describe('when clear status after dropdown is not set', () => {
      it('does not render hidden clear status after input', () => {
        createComponent();

        expect(findInput(defaultProvide.fields.clearStatusAfter.name).exists()).toBe(false);
      });
    });

    describe('when clear status after dropdown is set to `Never`', () => {
      it('renders hidden clear status after input with value unset', async () => {
        createComponent();

        findSetStatusForm().vm.$emit('clear-status-after-click', NEVER_TIME_RANGE);

        await nextTick();

        expect(
          findInput(defaultProvide.fields.clearStatusAfter.name).attributes('value'),
        ).toBeUndefined();
      });
    });

    describe('when clear status after dropdown is set to a time range', () => {
      it('renders hidden clear status after input with value set', async () => {
        createComponent();

        findSetStatusForm().vm.$emit('clear-status-after-click', TIME_RANGES_WITH_NEVER[1]);

        await nextTick();

        expect(findInput(defaultProvide.fields.clearStatusAfter.name).attributes('value')).toBe(
          TIME_RANGES_WITH_NEVER[1].shortcut,
        );
      });
    });
  });

  describe('when emoji is changed', () => {
    it('updates hidden emoji input value', async () => {
      createComponent();

      const newEmoji = 'basketball';

      findSetStatusForm().vm.$emit('emoji-click', newEmoji);

      await nextTick();

      expect(findInput(defaultProvide.fields.emoji.name).attributes('value')).toBe(newEmoji);
    });
  });

  describe('when message is changed', () => {
    it('updates hidden message input value', async () => {
      createComponent();

      const newMessage = 'foo bar baz';

      findSetStatusForm().vm.$emit('message-input', newMessage);

      await nextTick();

      expect(findInput(defaultProvide.fields.message.name).attributes('value')).toBe(newMessage);
    });
  });
});