summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/components/strategies/percent_rollout_spec.js
blob: 696b3b2e4c9dc972d0374574bf3fa6b10879c45c (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
import { mount } from '@vue/test-utils';
import { GlFormInput } from '@gitlab/ui';
import PercentRollout from '~/feature_flags/components/strategies/percent_rollout.vue';
import ParameterFormGroup from '~/feature_flags/components/strategies/parameter_form_group.vue';
import { PERCENT_ROLLOUT_GROUP_ID } from '~/feature_flags/constants';
import { percentRolloutStrategy } from '../../mock_data';

const DEFAULT_PROPS = {
  strategy: percentRolloutStrategy,
};

describe('~/feature_flags/components/strategies/percent_rollout.vue', () => {
  let wrapper;
  let input;
  let formGroup;

  const factory = (props = {}) =>
    mount(PercentRollout, { propsData: { ...DEFAULT_PROPS, ...props } });

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

    wrapper = null;
  });

  describe('with valid percentage', () => {
    beforeEach(() => {
      wrapper = factory();

      input = wrapper.find(GlFormInput);
      formGroup = wrapper.find(ParameterFormGroup);
    });

    it('displays the current value', () => {
      expect(input.element.value).toBe(percentRolloutStrategy.parameters.percentage);
    });

    it('emits a change when the value changes', async () => {
      input.setValue('75');
      await wrapper.vm.$nextTick();
      expect(wrapper.emitted('change')).toEqual([
        [{ parameters: { percentage: '75', groupId: PERCENT_ROLLOUT_GROUP_ID } }],
      ]);
    });

    it('does not show errors', () => {
      expect(formGroup.attributes('state')).toBe('true');
    });
  });

  describe('with percentage that is out of range', () => {
    beforeEach(() => {
      wrapper = factory({ strategy: { parameters: { percentage: '101' } } });

      input = wrapper.find(GlFormInput);
      formGroup = wrapper.find(ParameterFormGroup);
    });

    it('shows errors', () => {
      expect(formGroup.attributes('state')).toBeUndefined();
    });
  });

  describe('with percentage that is not an integer number', () => {
    beforeEach(() => {
      wrapper = factory({ strategy: { parameters: { percentage: '3.14' } } });

      input = wrapper.find(GlFormInput);
      formGroup = wrapper.find(ParameterFormGroup);
    });

    it('shows errors', () => {
      expect(formGroup.attributes('state')).toBeUndefined();
    });
  });
});