summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/components/strategy_parameters_spec.js
blob: a024384e62319295c14ecc22c665eed1ddc1b83f (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
import { shallowMount } from '@vue/test-utils';
import { last } from 'lodash';
import {
  ROLLOUT_STRATEGY_ALL_USERS,
  ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
  ROLLOUT_STRATEGY_USER_ID,
  ROLLOUT_STRATEGY_GITLAB_USER_LIST,
} from '~/feature_flags/constants';
import Default from '~/feature_flags/components/strategies/default.vue';
import GitlabUserList from '~/feature_flags/components/strategies/gitlab_user_list.vue';
import PercentRollout from '~/feature_flags/components/strategies/percent_rollout.vue';
import UsersWithId from '~/feature_flags/components/strategies/users_with_id.vue';
import StrategyParameters from '~/feature_flags/components/strategy_parameters.vue';
import { allUsersStrategy } from '../mock_data';

const DEFAULT_PROPS = {
  strategy: allUsersStrategy,
};

describe('~/feature_flags/components/strategy_parameters.vue', () => {
  let wrapper;

  const factory = (props = {}) =>
    shallowMount(StrategyParameters, {
      propsData: {
        ...DEFAULT_PROPS,
        ...props,
      },
    });

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

    wrapper = null;
  });

  describe.each`
    name                                 | component
    ${ROLLOUT_STRATEGY_ALL_USERS}        | ${Default}
    ${ROLLOUT_STRATEGY_PERCENT_ROLLOUT}  | ${PercentRollout}
    ${ROLLOUT_STRATEGY_USER_ID}          | ${UsersWithId}
    ${ROLLOUT_STRATEGY_GITLAB_USER_LIST} | ${GitlabUserList}
  `('with $name', ({ name, component }) => {
    let strategy;

    beforeEach(() => {
      strategy = { name, parameters: {} };
      wrapper = factory({ strategy });
    });

    it('should show the correct component', () => {
      expect(wrapper.contains(component)).toBe(true);
    });

    it('should emit changes from the lower component', () => {
      const strategyParameterWrapper = wrapper.find(component);

      strategyParameterWrapper.vm.$emit('change', { parameters: { foo: 'bar' } });

      expect(last(wrapper.emitted('change'))).toEqual([
        {
          name,
          parameters: { foo: 'bar' },
        },
      ]);
    });
  });

  describe('pass through props', () => {
    it('should pass through any extra props that might be needed', () => {
      const strategy = {
        name: ROLLOUT_STRATEGY_USER_ID,
      };
      wrapper = factory({
        strategy,
      });

      expect(wrapper.find(UsersWithId).props('strategy')).toEqual(strategy);
    });
  });
});