|
3 | 3 | // mocked out to allow `common/utils/utils` to load for real. |
4 | 4 | jest.mock('common/stores/account-store', () => ({})) |
5 | 5 | jest.mock('common/stores/project-store', () => ({})) |
6 | | -jest.mock('common/store', () => ({ getStore: () => ({}) })) |
| 6 | +jest.mock('common/store', () => ({ |
| 7 | + getStore: () => ({ getState: () => ({}) }), |
| 8 | +})) |
7 | 9 | jest.mock('@flagsmith/flagsmith', () => ({ |
8 | 10 | getValue: (_key: string, opts: { fallback: unknown }) => opts.fallback, |
9 | 11 | })) |
@@ -96,3 +98,67 @@ describe('validateRule', () => { |
96 | 98 | expect(Utils.validateRule(rule)).toBe(false) |
97 | 99 | }) |
98 | 100 | }) |
| 101 | + |
| 102 | +describe('getPlanPermission', () => { |
| 103 | + beforeEach(() => { |
| 104 | + jest.spyOn(Utils, 'isEnterpriseImage').mockReturnValue(false) |
| 105 | + jest.spyOn(Utils, 'isSaas').mockReturnValue(true) |
| 106 | + }) |
| 107 | + afterEach(() => jest.restoreAllMocks()) |
| 108 | + |
| 109 | + it.each([ |
| 110 | + ['scale-up', 'AUDIT', true], |
| 111 | + ['enterprise', 'AUDIT', true], |
| 112 | + ['start-up', 'AUDIT', false], |
| 113 | + ['free', 'AUDIT', false], |
| 114 | + ['enterprise', 'SCIM', true], |
| 115 | + ['scale-up', 'SCIM', false], |
| 116 | + ] as const)( |
| 117 | + 'hardcoded features: plan=%s feature=%s => %s', |
| 118 | + (plan, feature, expected) => { |
| 119 | + expect(Utils.getPlanPermission(plan, feature)).toBe(expected) |
| 120 | + }, |
| 121 | + ) |
| 122 | + |
| 123 | + describe('WAREHOUSE with legacy array value', () => { |
| 124 | + it('allows free plan when value includes "free"', () => { |
| 125 | + jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue(['free']) |
| 126 | + expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(true) |
| 127 | + }) |
| 128 | + |
| 129 | + it('blocks free plan when value is empty', () => { |
| 130 | + jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue([]) |
| 131 | + expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(false) |
| 132 | + }) |
| 133 | + |
| 134 | + it('allows scale-up when value includes "scale-up"', () => { |
| 135 | + jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue(['scale-up']) |
| 136 | + expect(Utils.getPlanPermission('scale-up', 'WAREHOUSE')).toBe(true) |
| 137 | + }) |
| 138 | + }) |
| 139 | + |
| 140 | + describe('WAREHOUSE with object value', () => { |
| 141 | + it('allows free plan when allowed_plans includes "free"', () => { |
| 142 | + jest.spyOn(Utils, 'getFlagsmithJSONValue').mockReturnValue({ |
| 143 | + allowed_plans: ['free'], |
| 144 | + auto_connect_warehouse: true, |
| 145 | + }) |
| 146 | + expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(true) |
| 147 | + }) |
| 148 | + |
| 149 | + it('blocks free plan when allowed_plans does not include "free"', () => { |
| 150 | + jest |
| 151 | + .spyOn(Utils, 'getFlagsmithJSONValue') |
| 152 | + .mockReturnValue({ allowed_plans: ['scale-up'] }) |
| 153 | + expect(Utils.getPlanPermission('free', 'WAREHOUSE')).toBe(false) |
| 154 | + }) |
| 155 | + |
| 156 | + it('falls back to enterprise with malformed value', () => { |
| 157 | + jest |
| 158 | + .spyOn(Utils, 'getFlagsmithJSONValue') |
| 159 | + .mockReturnValue({ allowed_plans: 'not-an-array' }) |
| 160 | + expect(Utils.getPlanPermission('enterprise', 'WAREHOUSE')).toBe(true) |
| 161 | + expect(Utils.getPlanPermission('scale-up', 'WAREHOUSE')).toBe(false) |
| 162 | + }) |
| 163 | + }) |
| 164 | +}) |
0 commit comments