1 import { Component } from '@angular/core';
2 import { ComponentFixture, TestBed } from '@angular/core/testing';
4 import { Permissions } from '~/app/shared/models/permissions';
5 import { AuthStorageService } from '~/app/shared/services/auth-storage.service';
6 import { configureTestBed } from '~/testing/unit-test-helper';
7 import { AuthStorageDirective } from './auth-storage.directive';
9 template: `<div id="permitted" *cdScope="condition; matchAll: matchAll"></div>`
11 export class AuthStorageDirectiveTestComponent {
12 condition: string | string[] | object;
16 describe('AuthStorageDirective', () => {
17 let fixture: ComponentFixture<AuthStorageDirectiveTestComponent>;
18 let component: AuthStorageDirectiveTestComponent;
19 let getPermissionsSpy: jasmine.Spy;
23 declarations: [AuthStorageDirective, AuthStorageDirectiveTestComponent],
24 providers: [AuthStorageService]
28 getPermissionsSpy = spyOn(TestBed.inject(AuthStorageService), 'getPermissions');
29 getPermissionsSpy.and.returnValue(new Permissions({ osd: ['read'], rgw: ['read'] }));
30 fixture = TestBed.createComponent(AuthStorageDirectiveTestComponent);
31 el = fixture.debugElement.nativeElement;
32 component = fixture.componentInstance;
35 it('should show div on valid condition', () => {
37 component.condition = 'rgw';
38 fixture.detectChanges();
39 expect(el.querySelector('#permitted')).toBeTruthy();
42 component.condition = ['osd', 'rgw'];
43 fixture.detectChanges();
44 expect(el.querySelector('#permitted')).toBeTruthy();
47 component.condition = { rgw: ['read'], osd: ['read'] };
48 fixture.detectChanges();
49 expect(el.querySelector('#permitted')).toBeTruthy();
52 it('should show div with loose matching', () => {
53 component.matchAll = false;
54 fixture.detectChanges();
57 component.condition = ['configOpt', 'osd', 'rgw'];
58 fixture.detectChanges();
59 expect(el.querySelector('#permitted')).toBeTruthy();
62 component.condition = { rgw: ['read', 'update', 'fake'], osd: ['read'] };
63 fixture.detectChanges();
64 expect(el.querySelector('#permitted')).toBeTruthy();
67 it('should not show div on invalid condition', () => {
69 component.condition = 'fake';
70 fixture.detectChanges();
71 expect(el.querySelector('#permitted')).toBeFalsy();
74 component.condition = ['configOpt', 'osd', 'rgw'];
75 fixture.detectChanges();
76 expect(el.querySelector('#permitted')).toBeFalsy();
79 component.condition = { rgw: ['read', 'update'], osd: ['read'] };
80 fixture.detectChanges();
81 expect(el.querySelector('#permitted')).toBeFalsy();
84 it('should hide div on condition change', () => {
85 component.condition = 'osd';
86 fixture.detectChanges();
87 expect(el.querySelector('#permitted')).toBeTruthy();
89 component.condition = 'grafana';
90 fixture.detectChanges();
91 expect(el.querySelector('#permitted')).toBeFalsy();
94 it('should hide div on permission change', () => {
95 component.condition = ['osd'];
96 fixture.detectChanges();
97 expect(el.querySelector('#permitted')).toBeTruthy();
99 getPermissionsSpy.and.returnValue(new Permissions({}));
100 component.condition = 'osd';
101 fixture.detectChanges();
102 expect(el.querySelector('#permitted')).toBeFalsy();