Skip to content

Instantly share code, notes, and snippets.

@e-oz
Last active August 1, 2023 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save e-oz/f8b15341cfa6a863d1f9f8a60cd77677 to your computer and use it in GitHub Desktop.
Save e-oz/f8b15341cfa6a863d1f9f8a60cd77677 to your computer and use it in GitHub Desktop.
import { Component, signal } from '@angular/core';
import { ComponentFixture, TestBed } from '@angular/core/testing';
import { IfHasPermissionDirective } from './if-has-permission.directive';
import { Permission, PermissionsService } from '../permissions.service';
const $allowedPermissions = signal<Permission[]>([]);
class PermissionsServiceMock {
hasPermission(permission: Permission) {
return $allowedPermissions().includes(permission);
}
}
@Component({
selector: 'test-if-has-permission',
standalone: true,
imports: [IfHasPermissionDirective],
template: `
<ng-template #falseTemplate>
<p>Does not have permission</p>
</ng-template>
<div *ifHasPermission="'READ'; else falseTemplate">
<p>Content that requires read permission</p>
</div>
<div *ifHasPermission="'CREATE'; else falseTemplate">
<p>Content that requires create permission</p>
</div>
<div *ifHasPermission="'DELETE'">
<p>Content that requires delete permission</p>
</div>
`,
})
class TestComponent {
}
describe('IfHasPermissionDirective', () => {
let component: TestComponent;
let fixture: ComponentFixture<TestComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [TestComponent],
providers: [{
provide: PermissionsService,
useClass: PermissionsServiceMock,
}],
}).compileComponents();
});
beforeEach(() => {
fixture = TestBed.createComponent(TestComponent);
component = fixture.componentInstance;
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('should render content with all permissions', () => {
$allowedPermissions.set(['READ', 'CREATE', 'DELETE']);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('Content that requires read permission');
expect(fixture.nativeElement.textContent).toContain('Content that requires create permission');
expect(fixture.nativeElement.textContent).toContain('Content that requires delete permission');
expect(fixture.nativeElement.textContent).not.toContain('Does not have permission');
});
it('should render content with read permission only', () => {
$allowedPermissions.set(['READ']);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).toContain('Content that requires read permission');
expect(fixture.nativeElement.textContent).not.toContain('Content that requires create permission');
expect(fixture.nativeElement.textContent).not.toContain('Content that requires delete permission');
expect(fixture.nativeElement.textContent).toContain('Does not have permission');
});
it('should not render content without permission', () => {
$allowedPermissions.set([]);
fixture.detectChanges();
expect(fixture.nativeElement.textContent).not.toContain('Content that requires read permission');
expect(fixture.nativeElement.textContent).not.toContain('Content that requires create permission');
expect(fixture.nativeElement.textContent).not.toContain('Content that requires delete permission');
expect(fixture.nativeElement.textContent).toContain('Does not have permission');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment