summaryrefslogtreecommitdiff
path: root/spec/frontend/feature_flags/store/helpers_spec.js
blob: 2a6211c8cc12ea53bf73d27dbcf6bcfea434eb53 (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
155
156
157
158
159
160
161
162
163
164
165
166
import { NEW_VERSION_FLAG } from '~/feature_flags/constants';
import { mapStrategiesToViewModel, mapStrategiesToRails } from '~/feature_flags/store/helpers';

describe('feature flags helpers spec', () => {
  describe('mapStrategiesToViewModel', () => {
    it('should map rails casing to view model casing', () => {
      expect(
        mapStrategiesToViewModel([
          {
            id: '1',
            name: 'default',
            parameters: {},
            scopes: [
              {
                environment_scope: '*',
                id: '1',
              },
            ],
          },
        ]),
      ).toEqual([
        {
          id: '1',
          name: 'default',
          parameters: {},
          shouldBeDestroyed: false,
          scopes: [
            {
              shouldBeDestroyed: false,
              environmentScope: '*',
              id: '1',
            },
          ],
        },
      ]);
    });

    it('inserts spaces between user ids', () => {
      const [strategy] = mapStrategiesToViewModel([
        {
          id: '1',
          name: 'userWithId',
          parameters: { userIds: 'user1,user2,user3' },
          scopes: [],
        },
      ]);

      expect(strategy.parameters).toEqual({ userIds: 'user1, user2, user3' });
    });
  });

  describe('mapStrategiesToRails', () => {
    it('should map rails casing to view model casing', () => {
      expect(
        mapStrategiesToRails({
          name: 'test',
          description: 'test description',
          active: true,
          strategies: [
            {
              id: '1',
              name: 'default',
              parameters: {},
              shouldBeDestroyed: true,
              scopes: [
                {
                  environmentScope: '*',
                  id: '1',
                  shouldBeDestroyed: true,
                },
              ],
            },
          ],
        }),
      ).toEqual({
        operations_feature_flag: {
          name: 'test',
          description: 'test description',
          active: true,
          version: NEW_VERSION_FLAG,
          strategies_attributes: [
            {
              id: '1',
              name: 'default',
              parameters: {},
              _destroy: true,
              scopes_attributes: [
                {
                  environment_scope: '*',
                  id: '1',
                  _destroy: true,
                },
              ],
            },
          ],
        },
      });
    });

    it('should insert a default * scope if there are none', () => {
      expect(
        mapStrategiesToRails({
          name: 'test',
          description: 'test description',
          active: true,
          strategies: [
            {
              id: '1',
              name: 'default',
              parameters: {},
              scopes: [],
            },
          ],
        }),
      ).toEqual({
        operations_feature_flag: {
          name: 'test',
          description: 'test description',
          active: true,
          version: NEW_VERSION_FLAG,
          strategies_attributes: [
            {
              id: '1',
              name: 'default',
              parameters: {},
              scopes_attributes: [
                {
                  environment_scope: '*',
                },
              ],
            },
          ],
        },
      });
    });

    it('removes white space between user ids', () => {
      const result = mapStrategiesToRails({
        name: 'test',
        active: true,
        strategies: [
          {
            id: '1',
            name: 'userWithId',
            parameters: { userIds: 'user1, user2, user3' },
            scopes: [],
          },
        ],
      });

      const strategyAttrs = result.operations_feature_flag.strategies_attributes[0];

      expect(strategyAttrs.parameters).toEqual({ userIds: 'user1,user2,user3' });
    });

    it('preserves the value of active', () => {
      const result = mapStrategiesToRails({
        name: 'test',
        active: false,
        strategies: [],
      });

      expect(result.operations_feature_flag.active).toBe(false);
    });
  });
});