|
| 1 | +import { Config } from 'src/libs/config' |
| 2 | +import { getAllFeatureFlags, getFeatureFlag } from 'src/libs/ajax/FeatureFlag' |
| 3 | + |
| 4 | +describe('FeatureFlag ajax', () => { |
| 5 | + let fetchStub: ReturnType<typeof cy.stub> |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + cy.initApplicationConfig() |
| 9 | + cy.stub(Config, 'getApiUrl').resolves('') |
| 10 | + cy.window().then((win) => { |
| 11 | + fetchStub = cy.stub(win, 'fetch') |
| 12 | + }) |
| 13 | + }) |
| 14 | + |
| 15 | + afterEach(() => { |
| 16 | + cy.window().then(() => { |
| 17 | + fetchStub.restore() |
| 18 | + }) |
| 19 | + }) |
| 20 | + |
| 21 | + it('getAllFeatureFlags returns a map of flags', () => { |
| 22 | + const response = { featureAlpha: 'on', featureBeta: 'off' } |
| 23 | + cy.window().then((win) => { |
| 24 | + fetchStub.resolves(new win.Response(JSON.stringify(response), { status: 200, headers: { 'content-type': 'application/json' } })) |
| 25 | + cy.wrap(getAllFeatureFlags()).should('deep.equal', response) |
| 26 | + }) |
| 27 | + }) |
| 28 | + |
| 29 | + it('getFeatureFlag returns the per-key value when available', () => { |
| 30 | + cy.window().then((win) => { |
| 31 | + fetchStub.resolves(new win.Response(JSON.stringify('enabled'), { status: 200, headers: { 'content-type': 'application/json' } })) |
| 32 | + cy.wrap(getFeatureFlag('someFlag')).should('equal', 'enabled') |
| 33 | + }) |
| 34 | + }) |
| 35 | + |
| 36 | + it('getFeatureFlag returns undefined when per-key endpoint errors', () => { |
| 37 | + cy.window().then(() => { |
| 38 | + fetchStub.rejects(new Error('Not found')) |
| 39 | + cy.wrap(getFeatureFlag('missingFlag')).should('be.undefined') |
| 40 | + }) |
| 41 | + }) |
| 42 | +}) |
0 commit comments