Skip to content

Commit

Permalink
update: form prop util tests where the injection cycle is used
Browse files Browse the repository at this point in the history
  • Loading branch information
sumeyyeKurtulus committed Oct 28, 2024
1 parent 358a586 commit 9a47611
Show file tree
Hide file tree
Showing 3 changed files with 218 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ describe('Form Prop Utils', () => {
extraProperties: {
bool: true,
date: '03/30/2002',
dateTime: '2002-03-30 13:30:59Z',
dateTime: '2002-03-30 13:30:59',
time: '13:30:59',
},
});
Expand All @@ -117,8 +117,8 @@ describe('Form Prop Utils', () => {
expect(extraPropertiesGroup.value).toEqual({
bool: true,
date: '2002-03-30',
dateTime: '2002-03-30T13:30:59.000Z',
time: '13:30',
dateTime: '2002-03-30T13:30:59.000',
time: '13:30:59',
});
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import { ConfigStateService, PermissionService } from '@abp/ng.core';
import { Injector } from '@angular/core';
import {
checkPolicies,
createExtraPropertyValueResolver,
mergeWithDefaultProps,
} from '../lib/utils/props.util';
import { ObjectExtensions } from '../lib/models/object-extensions';
import {
EntityProp,
EntityPropContributorCallbacks,
EntityPropDefaults,
EntityPropsFactory,
} from '../lib/models/entity-props';
import { PropData } from '../lib/models/props';
import { createExtraPropertyValueResolver, mergeWithDefaultProps } from '../lib/utils/props.util';

class MockPropData<R = any> extends PropData<R> {
getInjected: PropData<R>['getInjected'];
Expand Down Expand Up @@ -69,4 +76,194 @@ describe('Entity Prop Utils', () => {
expect(entityProps.get('y').props.toString()).toBe('3 <-> 2');
});
});

describe('#checkPolicies', () => {
let injector: Injector;
let configState: ConfigStateService;
let permissionService: PermissionService;

beforeEach(() => {
jest.clearAllMocks();

configState = {
getGlobalFeatureIsEnabled: jest.fn().mockReturnValue(false),
getFeatureIsEnabled: jest.fn().mockReturnValue(false),
} as any;

permissionService = {
getGrantedPolicy: jest.fn().mockReturnValue(false),
} as any;

injector = {
get: jest.fn().mockImplementation(service => {
if (service === ConfigStateService) {
return configState;
}
if (service === PermissionService) {
return permissionService;
}
return null;
}),
} as any;
});

it('should keep property when all permissions and features are satisfied', () => {
(permissionService.getGrantedPolicy as jest.Mock).mockReturnValue(true);
(configState.getGlobalFeatureIsEnabled as jest.Mock).mockReturnValue(true);
(configState.getFeatureIsEnabled as jest.Mock).mockReturnValue(true);

const properties: ObjectExtensions.EntityExtensionProperties = {
property1: {
policy: {
permissions: { permissionNames: ['Permission1', 'Permission2'], requiresAll: true },
globalFeatures: { features: ['GlobalFeature1'], requiresAll: true },
features: { features: ['Feature1'], requiresAll: true },
},
displayName: undefined,
api: undefined,
ui: undefined,
attributes: [],
configuration: undefined,
defaultValue: undefined,
},
};

checkPolicies(injector, properties);

expect(properties.property1).toBeDefined();
expect(permissionService.getGrantedPolicy).toHaveBeenCalledWith('Permission1');
expect(permissionService.getGrantedPolicy).toHaveBeenCalledWith('Permission2');
expect(configState.getGlobalFeatureIsEnabled).toHaveBeenCalledWith('GlobalFeature1');
expect(configState.getFeatureIsEnabled).toHaveBeenCalledWith('Feature1');
});

it('should keep property when no permissions and features are satisfied', () => {
(permissionService.getGrantedPolicy as jest.Mock).mockReturnValue(false);
(configState.getGlobalFeatureIsEnabled as jest.Mock).mockReturnValue(false);
(configState.getFeatureIsEnabled as jest.Mock).mockReturnValue(false);

const properties: ObjectExtensions.EntityExtensionProperties = {
property1: {
policy: {
permissions: { permissionNames: ['Permission1', 'Permission2'], requiresAll: true },
globalFeatures: { features: ['GlobalFeature1'], requiresAll: true },
features: { features: ['Feature1'], requiresAll: true },
},
displayName: undefined,
api: undefined,
ui: undefined,
attributes: [],
configuration: undefined,
defaultValue: undefined,
},
};

checkPolicies(injector, properties);

expect(properties.property1).toBeUndefined();
expect(permissionService.getGrantedPolicy).toHaveBeenCalledWith('Permission1');
expect(permissionService.getGrantedPolicy).not.toHaveBeenCalledWith('Permission2');
expect(configState.getGlobalFeatureIsEnabled).not.toHaveBeenCalledWith('GlobalFeature1');
expect(configState.getFeatureIsEnabled).not.toHaveBeenCalledWith('Feature1');
});

it('should delete property when only some permissions are granted', () => {
(permissionService.getGrantedPolicy as jest.Mock).mockImplementation(
permission => permission === 'Permission1',
);
(configState.getGlobalFeatureIsEnabled as jest.Mock).mockReturnValue(true);
(configState.getFeatureIsEnabled as jest.Mock).mockReturnValue(true);

const properties: ObjectExtensions.EntityExtensionProperties = {
property1: {
policy: {
permissions: { permissionNames: ['Permission1', 'Permission2'], requiresAll: true },
globalFeatures: { features: ['GlobalFeature1'], requiresAll: true },
features: { features: ['Feature1'], requiresAll: true },
},
displayName: undefined,
api: undefined,
ui: undefined,
attributes: [],
configuration: undefined,
defaultValue: undefined,
},
};

checkPolicies(injector, properties);

expect(properties.property1).toBeUndefined();
expect(permissionService.getGrantedPolicy).toHaveBeenCalledWith('Permission1');
expect(permissionService.getGrantedPolicy).toHaveBeenCalledWith('Permission2');
});

it('should delete property when some global features are disabled', () => {
(permissionService.getGrantedPolicy as jest.Mock).mockReturnValue(true);
(configState.getGlobalFeatureIsEnabled as jest.Mock).mockImplementation(feature =>
feature === 'GlobalFeature2' ? false : true,
);
(configState.getFeatureIsEnabled as jest.Mock).mockReturnValue(true);

const properties: ObjectExtensions.EntityExtensionProperties = {
property1: {
policy: {
permissions: { permissionNames: ['Permission1'], requiresAll: true },
globalFeatures: { features: ['GlobalFeature1', 'GlobalFeature2'], requiresAll: true },
features: { features: ['Feature1'], requiresAll: true },
},
displayName: undefined,
api: undefined,
ui: undefined,
attributes: [],
configuration: undefined,
defaultValue: undefined,
},
};

checkPolicies(injector, properties);

expect(properties.property1).toBeUndefined();
expect(configState.getGlobalFeatureIsEnabled).toHaveBeenCalledWith('GlobalFeature1');
expect(configState.getGlobalFeatureIsEnabled).toHaveBeenCalledWith('GlobalFeature2');
});

it('should keep property when all permissions are granted but only some features are required', () => {
(permissionService.getGrantedPolicy as jest.Mock).mockImplementation(
permission => permission === 'Permission1' || permission === 'Permission2',
);
(configState.getFeatureIsEnabled as jest.Mock).mockImplementation(
feature => feature === 'Feature1',
);
(configState.getGlobalFeatureIsEnabled as jest.Mock).mockReturnValue(true);

const properties: ObjectExtensions.EntityExtensionProperties = {
property1: {
policy: {
permissions: { permissionNames: ['Permission1', 'Permission2'], requiresAll: false },
features: {
features: ['Feature1', 'Feature2', 'Feature3'],
requiresAll: false,
},
globalFeatures: { features: ['GlobalFeature1'], requiresAll: true },
},
displayName: undefined,
api: undefined,
ui: undefined,
attributes: [],
configuration: undefined,
defaultValue: undefined,
},
};

checkPolicies(injector, properties);

expect(properties.property1).toBeDefined();

expect(configState.getFeatureIsEnabled).toHaveBeenCalledWith('Feature1');
expect(configState.getFeatureIsEnabled).not.toHaveBeenCalledWith('Feature2');
expect(configState.getFeatureIsEnabled).not.toHaveBeenCalledWith('Feature3');

expect(configState.getGlobalFeatureIsEnabled).toHaveBeenCalledWith('GlobalFeature1');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ConfigStateService, PermissionService } from '@abp/ng.core';
import { ConfigStateService } from '@abp/ng.core';
import { firstValueFrom, lastValueFrom, of } from 'rxjs';
import { take } from 'rxjs/operators';
import { ePropType } from '../lib/enums/props.enum';
Expand All @@ -9,18 +9,25 @@ import {
getObjectExtensionEntitiesFromStore,
mapEntitiesToContributors,
} from '../lib/utils/state.util';
import { Injector } from '@angular/core';

const fakeAppConfigService = { get: () => of(createMockState()) } as any;
const fakeLocalizationService = { get: () => of(createMockState()) } as any;
const configState = new ConfigStateService(fakeAppConfigService, fakeLocalizationService, false);
configState.refreshAppState();
const permissionService = new PermissionService(configState);

describe('State Utils', () => {
let injector: Injector;
beforeEach(() => {
injector = {
get: jest.fn().mockReturnValue(configState),
};
});

describe('#getObjectExtensionEntitiesFromStore', () => {
it('should return observable entities of an existing module', async () => {
const objectExtensionEntitiesFromStore$ = getObjectExtensionEntitiesFromStore(
configState,
injector,
'Identity',
);

Expand All @@ -29,15 +36,19 @@ describe('State Utils', () => {
});

it('should return observable empty object if module does not exist', async () => {
const entities = await getObjectExtensionEntitiesFromStore(configState, 'Saas').toPromise();
const entities = await getObjectExtensionEntitiesFromStore(injector, 'Saas').toPromise();
expect(entities).toEqual({});
});

it('should not emit when object extensions do not exist', done => {
const emptyConfigState = new ConfigStateService(null, null, false);
const emit = jest.fn();

getObjectExtensionEntitiesFromStore(emptyConfigState, 'Identity').subscribe(emit);
injector = {
get: jest.fn().mockReturnValue(emptyConfigState),
};

getObjectExtensionEntitiesFromStore(injector, 'Identity').subscribe(emit);

setTimeout(() => {
expect(emit).not.toHaveBeenCalled();
Expand All @@ -49,10 +60,7 @@ describe('State Utils', () => {
describe('#mapEntitiesToContributors', () => {
it('should return contributors from given entities', async () => {
const contributors = await lastValueFrom(
of(createMockEntities()).pipe(
mapEntitiesToContributors(configState, permissionService, 'AbpIdentity'),
take(1),
),
of(createMockEntities()).pipe(mapEntitiesToContributors(injector, 'AbpIdentity'), take(1)),
);

const propList = new EntityPropList();
Expand Down

0 comments on commit 9a47611

Please sign in to comment.