summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/components/edit_feature_flag_spec.js
blob: e2717b98ea93835d5dd9e9f659684db09d2d5b03 (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
import { GlToggle, GlAlert } from '@gitlab/ui';
import { createLocalVue, shallowMount } from '@vue/test-utils';
import MockAdapter from 'axios-mock-adapter';
import Vuex from 'vuex';
import { mockTracking } from 'helpers/tracking_helper';
import { TEST_HOST } from 'spec/test_constants';
import EditFeatureFlag from '~/feature_flags/components/edit_feature_flag.vue';
import Form from '~/feature_flags/components/form.vue';
import { LEGACY_FLAG, NEW_VERSION_FLAG } from '~/feature_flags/constants';
import createStore from '~/feature_flags/store/edit';
import axios from '~/lib/utils/axios_utils';

const localVue = createLocalVue();
localVue.use(Vuex);

const userCalloutId = 'feature_flags_new_version';
const userCalloutsPath = `${TEST_HOST}/user_callouts`;

describe('Edit feature flag form', () => {
  let wrapper;
  let mock;

  const store = createStore({
    path: '/feature_flags',
    endpoint: `${TEST_HOST}/feature_flags.json`,
  });

  const factory = (opts = {}) => {
    if (wrapper) {
      wrapper.destroy();
      wrapper = null;
    }
    wrapper = shallowMount(EditFeatureFlag, {
      localVue,
      store,
      provide: {
        showUserCallout: true,
        userCalloutId,
        userCalloutsPath,
        ...opts,
      },
    });
  };

  beforeEach((done) => {
    mock = new MockAdapter(axios);
    mock.onGet(`${TEST_HOST}/feature_flags.json`).replyOnce(200, {
      id: 21,
      iid: 5,
      active: true,
      created_at: '2019-01-17T17:27:39.778Z',
      updated_at: '2019-01-17T17:27:39.778Z',
      name: 'feature_flag',
      description: '',
      version: LEGACY_FLAG,
      edit_path: '/h5bp/html5-boilerplate/-/feature_flags/21/edit',
      destroy_path: '/h5bp/html5-boilerplate/-/feature_flags/21',
      scopes: [
        {
          id: 21,
          active: false,
          environment_scope: '*',
          created_at: '2019-01-17T17:27:39.778Z',
          updated_at: '2019-01-17T17:27:39.778Z',
        },
      ],
    });
    factory();
    setImmediate(() => done());
  });

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

  const findAlert = () => wrapper.find(GlAlert);
  const findWarningGlAlert = () =>
    wrapper.findAll(GlAlert).filter((c) => c.props('variant') === 'warning');

  it('should display the iid', () => {
    expect(wrapper.find('h3').text()).toContain('^5');
  });

  it('should render the toggle', () => {
    expect(wrapper.find(GlToggle).exists()).toBe(true);
  });

  it('should set the value of the toggle to whether or not the flag is active', () => {
    expect(wrapper.find(GlToggle).props('value')).toBe(true);
  });

  it('should alert users the flag is read only', () => {
    expect(findAlert().text()).toContain('GitLab is moving to a new way of managing feature flags');
  });

  describe('with error', () => {
    it('should render the error', () => {
      store.dispatch('receiveUpdateFeatureFlagError', { message: ['The name is required'] });
      return wrapper.vm.$nextTick(() => {
        const warningGlAlert = findWarningGlAlert();
        expect(warningGlAlert.at(1).exists()).toEqual(true);
        expect(warningGlAlert.at(1).text()).toContain('The name is required');
      });
    });
  });

  describe('without error', () => {
    it('renders form title', () => {
      expect(wrapper.text()).toContain('^5 feature_flag');
    });

    it('should render feature flag form', () => {
      expect(wrapper.find(Form).exists()).toEqual(true);
    });

    it('should set the version of the form from the feature flag', () => {
      expect(wrapper.find(Form).props('version')).toBe(LEGACY_FLAG);

      mock.resetHandlers();

      mock.onGet(`${TEST_HOST}/feature_flags.json`).replyOnce(200, {
        id: 21,
        iid: 5,
        active: true,
        created_at: '2019-01-17T17:27:39.778Z',
        updated_at: '2019-01-17T17:27:39.778Z',
        name: 'feature_flag',
        description: '',
        version: NEW_VERSION_FLAG,
        edit_path: '/h5bp/html5-boilerplate/-/feature_flags/21/edit',
        destroy_path: '/h5bp/html5-boilerplate/-/feature_flags/21',
        strategies: [],
      });

      factory();

      return axios.waitForAll().then(() => {
        expect(wrapper.find(Form).props('version')).toBe(NEW_VERSION_FLAG);
      });
    });

    it('should track when the toggle is clicked', () => {
      const toggle = wrapper.find(GlToggle);
      const spy = mockTracking('_category_', toggle.element, jest.spyOn);

      toggle.trigger('click');

      expect(spy).toHaveBeenCalledWith('_category_', 'click_button', {
        label: 'feature_flag_toggle',
      });
    });
  });
});