helpers_spec.js 5.8 KB
Newer Older
1
import _ from 'underscore';
2 3 4 5 6 7 8 9
import {
  mapToScopesViewModel,
  mapFromScopesViewModel,
  createNewEnvironmentScope,
} from 'ee/feature_flags/store/modules/helpers';
import {
  ROLLOUT_STRATEGY_ALL_USERS,
  ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
10
  ROLLOUT_STRATEGY_USER_ID,
11 12 13 14
  PERCENT_ROLLOUT_GROUP_ID,
  INTERNAL_ID_PREFIX,
  DEFAULT_PERCENT_ROLLOUT,
} from 'ee/feature_flags/constants';
15 16

describe('feature flags helpers spec', () => {
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
  describe('mapToScopesViewModel', () => {
    it('converts the data object from the Rails API into something more usable by Vue', () => {
      const input = [
        {
          id: 3,
          environment_scope: 'environment_scope',
          active: true,
          can_update: true,
          protected: true,
          strategies: [
            {
              name: ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
              parameters: {
                percentage: '56',
              },
            },
33 34 35 36 37 38
            {
              name: ROLLOUT_STRATEGY_USER_ID,
              parameters: {
                userIds: '123,234',
              },
            },
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
          ],

          _destroy: true,
        },
      ];

      const expected = [
        {
          id: 3,
          environmentScope: 'environment_scope',
          active: true,
          canUpdate: true,
          protected: true,
          rolloutStrategy: ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
          rolloutPercentage: '56',
54
          rolloutUserIds: ['123', '234'],
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
          shouldBeDestroyed: true,
        },
      ];

      const actual = mapToScopesViewModel(input);

      expect(actual).toEqual(expected);
    });

    it('returns Boolean properties even when their Rails counterparts were not provided (are `undefined`)', () => {
      const input = [
        {
          id: 3,
          environment_scope: 'environment_scope',
        },
      ];

      const [result] = mapToScopesViewModel(input);

      expect(result).toEqual(
        expect.objectContaining({
          active: false,
          canUpdate: false,
          protected: false,
          shouldBeDestroyed: false,
        }),
      );
    });

    it('returns an empty array if null or undefined is provided as a parameter', () => {
      expect(mapToScopesViewModel(null)).toEqual([]);
      expect(mapToScopesViewModel(undefined)).toEqual([]);
    });
  });

  describe('mapFromScopesViewModel', () => {
    it('converts the object emitted from the Vue component into an object than is in the right format to be submitted to the Rails API', () => {
      const input = {
        name: 'name',
        description: 'description',
        scopes: [
96
          {
97 98
            id: 4,
            environmentScope: 'environmentScope',
99
            active: true,
100 101 102 103 104
            canUpdate: true,
            protected: true,
            shouldBeDestroyed: true,
            rolloutStrategy: ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
            rolloutPercentage: '48',
105
            rolloutUserIds: ['123', '234'],
106
          },
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
        ],
      };

      const expected = {
        operations_feature_flag: {
          name: 'name',
          description: 'description',
          scopes_attributes: [
            {
              id: 4,
              environment_scope: 'environmentScope',
              active: true,
              can_update: true,
              protected: true,
              _destroy: true,
              strategies: [
                {
                  name: ROLLOUT_STRATEGY_PERCENT_ROLLOUT,
                  parameters: {
                    groupId: PERCENT_ROLLOUT_GROUP_ID,
                    percentage: '48',
                  },
                },
130 131 132 133 134 135
                {
                  name: ROLLOUT_STRATEGY_USER_ID,
                  parameters: {
                    userIds: '123,234',
                  },
                },
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
              ],
            },
          ],
        },
      };

      const actual = mapFromScopesViewModel(input);

      expect(actual).toEqual(expected);
    });

    it('should strip out internal IDs', () => {
      const input = {
        scopes: [{ id: 3 }, { id: _.uniqueId(INTERNAL_ID_PREFIX) }],
      };

      const result = mapFromScopesViewModel(input);
      const [realId, internalId] = result.operations_feature_flag.scopes_attributes;

      expect(realId.id).toBe(3);
      expect(internalId.id).toBeUndefined();
    });

    it('returns scopes_attributes as [] if param.scopes is null or undefined', () => {
      let {
        operations_feature_flag: { scopes_attributes: actualScopes },
      } = mapFromScopesViewModel({ scopes: null });

      expect(actualScopes).toEqual([]);

      ({
        operations_feature_flag: { scopes_attributes: actualScopes },
      } = mapFromScopesViewModel({ scopes: undefined }));

      expect(actualScopes).toEqual([]);
    });
  });

  describe('createNewEnvironmentScope', () => {
    it('should return a new environment scope object populated with the default options', () => {
      const expected = {
        environmentScope: '',
        active: false,
        id: expect.stringContaining(INTERNAL_ID_PREFIX),
        rolloutStrategy: ROLLOUT_STRATEGY_ALL_USERS,
        rolloutPercentage: DEFAULT_PERCENT_ROLLOUT,
182
        rolloutUserIds: [],
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
      };

      const actual = createNewEnvironmentScope();

      expect(actual).toEqual(expected);
    });

    it('should return a new environment scope object with overrides applied', () => {
      const overrides = {
        environmentScope: 'environmentScope',
        active: true,
      };

      const expected = {
        environmentScope: 'environmentScope',
        active: true,
        id: expect.stringContaining(INTERNAL_ID_PREFIX),
        rolloutStrategy: ROLLOUT_STRATEGY_ALL_USERS,
        rolloutPercentage: DEFAULT_PERCENT_ROLLOUT,
202
        rolloutUserIds: [],
203
      };
204

205
      const actual = createNewEnvironmentScope(overrides);
206

207
      expect(actual).toEqual(expected);
208 209 210
    });
  });
});