Skip to content

Testing

AlexKhymenko edited this page May 31, 2018 · 11 revisions

Overview

  1. Directive stubs

Directive stub

For better testing experience library provides with 2 predefined directives stubs NgxPermissionsAllowStubDirective, NgxPermissionsRestrictStubDirective

NgxPermissionsAllowStubDirective will always resolve in true(user always have all permissions);

NgxPermissionsRestrictStubDirective will always resolve in false (user have no permissions);

Just import and add NgxPermissionsAllowStubDirective or NgxPermissionsRestrictStubDirective to declarations of configureTestingModule


import { NgxPermissionsAllowStubDirective} from 'ngx-permissions';

TestBed.configureTestingModule({
             declarations: [YourComp, NgxPermissionsAllowStubDirective]
         });

That's it.

Full Example of usage

describe('Testing you component', () => {
    @Component({selector: 'test-comp',
        template: '<ng-template [ngxPermissionsExcept]="'ADMIN'"><div>123</div></ng-template>'})
    class TestComp {
        data: any;
    }

    let fixture: any;
    let comp;
    beforeEach(() => {
        TestBed.configureTestingModule({declarations: [TestComp, NgxPermissionsAllowStubDirective]});
        fixture = TestBed.createComponent(TestComp);
        comp = fixture.componentInstance;
    });

    it ('Should show the component', fakeAsync(() => {
        detectChanges(fixture);
        let content = fixture.debugElement.nativeElement.querySelector('div');

        expect(content).toBeTruthy();
        expect(content.innerHTML).toEqual('123');
    }));
});

function detectChanges(fixture) {
    tick();
    fixture.detectChanges();
}
Clone this wiki locally