|
|
|
@ -2,10 +2,12 @@ import { createDirectiveFactory, SpectatorDirective } from '@ngneat/spectator/je |
|
|
|
import { Subject } from 'rxjs'; |
|
|
|
import { PermissionDirective } from '../directives/permission.directive'; |
|
|
|
import { PermissionService } from '../services'; |
|
|
|
import { ChangeDetectorRef } from '@angular/core'; |
|
|
|
|
|
|
|
describe('PermissionDirective', () => { |
|
|
|
let spectator: SpectatorDirective<PermissionDirective>; |
|
|
|
let directive: PermissionDirective; |
|
|
|
let cdr: ChangeDetectorRef; |
|
|
|
const grantedPolicy$ = new Subject<boolean>(); |
|
|
|
const createDirective = createDirectiveFactory({ |
|
|
|
directive: PermissionDirective, |
|
|
|
@ -30,7 +32,7 @@ describe('PermissionDirective', () => { |
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.query('#test-element')).toBeTruthy(); |
|
|
|
grantedPolicy$.next(false); |
|
|
|
// expect(spectator.query('#test-element')).toBeFalsy(); // TODO: change detection problem should be fixed
|
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
@ -41,6 +43,7 @@ describe('PermissionDirective', () => { |
|
|
|
{ hostProps: { condition: '' } }, |
|
|
|
); |
|
|
|
directive = spectator.directive; |
|
|
|
cdr = (directive as any).cdRef as ChangeDetectorRef; |
|
|
|
}); |
|
|
|
|
|
|
|
it('should be created', () => { |
|
|
|
@ -55,8 +58,22 @@ describe('PermissionDirective', () => { |
|
|
|
grantedPolicy$.next(false); |
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.queryAll('#test-element')).toHaveLength(1); |
|
|
|
}); |
|
|
|
|
|
|
|
it('should call detect changes method', () => { |
|
|
|
const detectChanges = jest.spyOn(cdr, 'detectChanges'); |
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
spectator.setHostInput({ condition: 'test' }); |
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.query('#test-element')).toBeTruthy(); |
|
|
|
expect(detectChanges).toHaveBeenCalled(); |
|
|
|
grantedPolicy$.next(false); |
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
expect(detectChanges).toHaveBeenCalled(); |
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.queryAll('#test-element')).toHaveLength(1); |
|
|
|
expect(detectChanges).toHaveBeenCalled(); |
|
|
|
}); |
|
|
|
|
|
|
|
describe('#subscription', () => { |
|
|
|
@ -70,4 +87,35 @@ describe('PermissionDirective', () => { |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
describe('with runChangeDetection Input', () => { |
|
|
|
beforeEach(() => { |
|
|
|
spectator = createDirective( |
|
|
|
'<div id="test-element" *abpPermission="condition;runChangeDetection:false">Testing Permission Directive</div>', |
|
|
|
{ hostProps: { condition: '' } }, |
|
|
|
); |
|
|
|
directive = spectator.directive; |
|
|
|
cdr = (directive as any).cdRef as ChangeDetectorRef; |
|
|
|
}); |
|
|
|
it('should not call detectChanges method', () => { |
|
|
|
const detectChanges = jest.spyOn(cdr, 'detectChanges'); |
|
|
|
const markForCheck = jest.spyOn(cdr, 'markForCheck'); |
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
spectator.setHostInput({ condition: 'test' }); |
|
|
|
|
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.query('#test-element')).toBeTruthy(); |
|
|
|
expect(detectChanges).not.toHaveBeenCalled(); |
|
|
|
expect(markForCheck).toHaveBeenCalled(); |
|
|
|
|
|
|
|
grantedPolicy$.next(false); |
|
|
|
expect(spectator.query('#test-element')).toBeFalsy(); |
|
|
|
expect(detectChanges).not.toHaveBeenCalled(); |
|
|
|
expect(markForCheck).toHaveBeenCalled(); |
|
|
|
|
|
|
|
grantedPolicy$.next(true); |
|
|
|
expect(spectator.queryAll('#test-element')).toHaveLength(1); |
|
|
|
expect(detectChanges).not.toHaveBeenCalled(); |
|
|
|
expect(markForCheck).toHaveBeenCalled(); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|