From 9a47611bc55d0fb9064f790ebb101aa7fe9ad0dc Mon Sep 17 00:00:00 2001 From: sumeyyeKurtulus Date: Mon, 28 Oct 2024 08:11:35 +0300 Subject: [PATCH] update: form prop util tests where the injection cycle is used --- .../src/tests/form-props.util.spec.ts | 6 +- .../extensible/src/tests/props.util.spec.ts | 199 +++++++++++++++++- .../extensible/src/tests/state.util.spec.ts | 26 ++- 3 files changed, 218 insertions(+), 13 deletions(-) diff --git a/npm/ng-packs/packages/components/extensible/src/tests/form-props.util.spec.ts b/npm/ng-packs/packages/components/extensible/src/tests/form-props.util.spec.ts index 123912cb9a..ae3698757d 100644 --- a/npm/ng-packs/packages/components/extensible/src/tests/form-props.util.spec.ts +++ b/npm/ng-packs/packages/components/extensible/src/tests/form-props.util.spec.ts @@ -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', }, }); @@ -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', }); }); }); diff --git a/npm/ng-packs/packages/components/extensible/src/tests/props.util.spec.ts b/npm/ng-packs/packages/components/extensible/src/tests/props.util.spec.ts index fc2b9a3282..ce9a439133 100644 --- a/npm/ng-packs/packages/components/extensible/src/tests/props.util.spec.ts +++ b/npm/ng-packs/packages/components/extensible/src/tests/props.util.spec.ts @@ -1,3 +1,11 @@ +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, @@ -5,7 +13,6 @@ import { EntityPropsFactory, } from '../lib/models/entity-props'; import { PropData } from '../lib/models/props'; -import { createExtraPropertyValueResolver, mergeWithDefaultProps } from '../lib/utils/props.util'; class MockPropData extends PropData { getInjected: PropData['getInjected']; @@ -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'); + }); + }); }); diff --git a/npm/ng-packs/packages/components/extensible/src/tests/state.util.spec.ts b/npm/ng-packs/packages/components/extensible/src/tests/state.util.spec.ts index 006ea1101e..de6e5d73c6 100644 --- a/npm/ng-packs/packages/components/extensible/src/tests/state.util.spec.ts +++ b/npm/ng-packs/packages/components/extensible/src/tests/state.util.spec.ts @@ -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'; @@ -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', ); @@ -29,7 +36,7 @@ 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({}); }); @@ -37,7 +44,11 @@ describe('State Utils', () => { 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(); @@ -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();