diff --git a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts index 3ae113c948..93339bcbcc 100644 --- a/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts +++ b/npm/ng-packs/packages/core/src/lib/tests/auth.guard.spec.ts @@ -1,4 +1,4 @@ -import { createServiceFactory, SpectatorService } from '@ngneat/spectator'; +import { createServiceFactory, SpectatorService } from '@ngneat/spectator/jest'; import { AuthGuard } from '../guards/auth.guard'; import { OAuthService } from 'angular-oauth2-oidc'; import { RouterModule, UrlTree, Router } from '@angular/router'; diff --git a/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts b/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts new file mode 100644 index 0000000000..b358005837 --- /dev/null +++ b/npm/ng-packs/packages/core/src/lib/tests/permission.guard.spec.ts @@ -0,0 +1,43 @@ +import { createServiceFactory, SpectatorService, SpyObject } from '@ngneat/spectator/jest'; +import { Store } from '@ngxs/store'; +import { of } from 'rxjs'; +import { PermissionGuard } from '../guards/permission.guard'; +import { RestOccurError } from '../actions'; + +describe('PermissionGuard', () => { + let spectator: SpectatorService; + let guard: PermissionGuard; + let store: SpyObject; + + const createService = createServiceFactory({ + service: PermissionGuard, + mocks: [Store], + }); + + beforeEach(() => { + spectator = createService(); + guard = spectator.service; + store = spectator.get(Store); + }); + + it('should return true when the grantedPolicy is true', done => { + store.select.andReturn(of(true)); + const spy = jest.spyOn(store, 'dispatch'); + guard.canActivate({ data: { requiredPolicy: '' } } as any).subscribe(res => { + expect(res).toBe(true); + expect(spy.mock.calls).toHaveLength(0); + done(); + }); + }); + + it('should return false and dispatch RestOccurError when the grantedPolicy is false', done => { + store.select.andReturn(of(false)); + const spy = jest.spyOn(store, 'dispatch'); + guard.canActivate({ data: { requiredPolicy: '' } } as any).subscribe(res => { + expect(res).toBe(false); + expect(spy.mock.calls[0][0] instanceof RestOccurError).toBeTruthy(); + expect((spy.mock.calls[0][0] as RestOccurError).payload).toEqual({ status: 403 }); + done(); + }); + }); +});