Skip to content

Commit

Permalink
HXCS-3659 remove duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano Costa authored and Adriano Costa committed May 10, 2024
1 parent 2f0d604 commit 1ae8785
Showing 1 changed file with 54 additions and 156 deletions.
210 changes: 54 additions & 156 deletions lib/core/feature-flags/src/lib/services/debug-features.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('DebugFeaturesService', () => {
setItem: () => {}
};

const getServiceWithDebugMode = (debugMode: boolean) => {
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
DebugFeaturesService,
Expand All @@ -48,179 +48,77 @@ describe('DebugFeaturesService', () => {
{ provide: OverridableFeaturesServiceToken, useClass: DummyFeaturesService }
]
});
const featureService = TestBed.inject(DebugFeaturesService);
featureService.enable(debugMode);
return featureService;
};
service = TestBed.inject(DebugFeaturesService);
});

describe('in debug mode', () => {
beforeEach(() => {
service = getServiceWithDebugMode(true);
});
it('should return false for isOn$ when flag is enabled', (done) => {
const flagKey = 'feature1';

it('should be in debug mode', (done) => {
service.isEnabled().subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should return false for isOn$ when flag is enabled', (done) => {
const flagKey = 'feature1';

service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should return false for isOn$ when flag is disabled', (done) => {
const flagKey = 'feature2';

service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should return true for isOff$ when flag is enabled', (done) => {
const flagKey = 'feature3';

service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});

it('should return true for isOff$ when flag is disabled', (done) => {
const flagKey = 'feature4';

service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});

it('should always reset specified flags', () => {
const flagsToReset = {
feature1: true
};
const writableFeaturesServiceToken = TestBed.inject(WritableFeaturesServiceToken);
const spy = spyOn(writableFeaturesServiceToken, 'resetFlags');
service.resetFlags(flagsToReset);
});

expect(spy).toHaveBeenCalled();
});
it('should return false for isOn$ when flag is disabled', (done) => {
const flagKey = 'feature2';

it('should get the flags as an observable', (done) => {
service.getFlags$().subscribe((flags) => {
expect(flags).toEqual({});
service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should get the flags snapshot', () => {
const flags = service.getFlagsSnapshot();
expect(flags).toEqual({});
});

it('should return the storageFeaturesService key with -override postfix', () => {
expect(service.storageKey).toBe('feature-flags-override');
});
});

describe('not in debug mode', () => {
beforeEach(() => {
service = getServiceWithDebugMode(false);
});
it('should not be in debug mode', (done) => {
service.isEnabled().subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
it('should return true for isOff$ when flag is enabled', (done) => {
const flagKey = 'feature3';

service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});

it('should return false for isOn$ when flag is enabled', (done) => {
const flagKey = 'feature1';

service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should return false for isOn$ when flag is disabled', (done) => {
const flagKey = 'feature2';

service
.isOn$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeFalse();
done();
});
});

it('should return true for isOff$ when flag is enabled', (done) => {
const flagKey = 'feature3';

service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});

it('should return true for isOff$ when flag is disabled', (done) => {
const flagKey = 'feature4';

service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});

it('should always reset specified flags', () => {
const flagsToReset = {
feature1: true
};
const writableFeaturesServiceToken = TestBed.inject(WritableFeaturesServiceToken);
const spy = spyOn(writableFeaturesServiceToken, 'resetFlags');
service.resetFlags(flagsToReset);
});

expect(spy).toHaveBeenCalled();
});
it('should return true for isOff$ when flag is disabled', (done) => {
const flagKey = 'feature4';

it('should get the flags as an observable', (done) => {
service.getFlags$().subscribe((flags) => {
expect(flags).toEqual({});
service
.isOff$(flagKey)
.pipe(take(1))
.subscribe((isEnabled) => {
expect(isEnabled).toBeTrue();
done();
});
});
});

it('should get the flags snapshot', () => {
const flags = service.getFlagsSnapshot();
it('should always reset specified flags', () => {
const flagsToReset = {
feature1: true
};
const writableFeaturesServiceToken = TestBed.inject(WritableFeaturesServiceToken);
const spy = spyOn(writableFeaturesServiceToken, 'resetFlags');
service.resetFlags(flagsToReset);

expect(spy).toHaveBeenCalled();
});

it('should get the flags as an observable', (done) => {
service.getFlags$().subscribe((flags) => {
expect(flags).toEqual({});
done();
});
});

it('should get the flags snapshot', () => {
const flags = service.getFlagsSnapshot();
expect(flags).toEqual({});
});
});

0 comments on commit 1ae8785

Please sign in to comment.