-
-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pipe): adds pure
ablePure
pipe in @casl/angular
Closes #276
- Loading branch information
Showing
8 changed files
with
150 additions
and
126 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { PureAbility } from '@casl/ability' | ||
import { TestBed } from '@angular/core/testing' | ||
import { createApp, createComponent, configureTestingModule, Post } from './spec_helper' | ||
|
||
const AppWithCanPipe = createApp('{{ post | can: \'read\' }}') | ||
const AppWithAblePipe = createApp('{{ \'read\' | able: post }}') | ||
const AppWithAblePurePipe = createApp('{{ \'read\' | ablePure: post | async }}') | ||
|
||
describe('Ability pipes', () => { | ||
let fixture | ||
let ability | ||
let post | ||
|
||
afterEach(() => { | ||
if (fixture) { | ||
fixture.destroy() | ||
} | ||
}) | ||
|
||
describe('module', () => { | ||
it('provides deprecated impure `can` pipe', () => { | ||
configureTestingModule([AppWithCanPipe]) | ||
fixture = createComponent(AppWithCanPipe) | ||
expect(fixture.nativeElement.textContent).to.equal('false') | ||
}) | ||
|
||
it('provides impure `able` pipe', () => { | ||
configureTestingModule([AppWithAblePipe]) | ||
fixture = createComponent(AppWithAblePipe) | ||
expect(fixture.nativeElement.textContent).to.equal('false') | ||
}) | ||
}) | ||
|
||
describe('`can` pipe', () => { | ||
behavesLikeAbilityPipe(AppWithCanPipe) | ||
}) | ||
|
||
describe('`able` pipe', () => { | ||
behavesLikeAbilityPipe(AppWithAblePipe) | ||
}) | ||
|
||
describe('`ablePure` pipe', () => { | ||
behavesLikeAbilityPipe(AppWithAblePurePipe) | ||
}) | ||
|
||
function behavesLikeAbilityPipe(App) { | ||
beforeEach(() => { | ||
configureTestingModule([App]) | ||
ability = TestBed.inject(PureAbility) | ||
post = new Post({ author: 'me' }) | ||
}) | ||
|
||
it('updates template when `ability` is updated', () => { | ||
fixture = createComponent(App, { post }) | ||
ability.update([{ subject: Post.name, action: 'read' }]) | ||
fixture.detectChanges() | ||
|
||
expect(fixture.nativeElement.textContent).to.equal('true') | ||
}) | ||
|
||
describe('when abilities depends on object attribute', () => { | ||
beforeEach(() => { | ||
ability.update([{ subject: Post.name, action: 'read', conditions: { author: 'me' } }]) | ||
fixture = createComponent(App, { post }) | ||
fixture.detectChanges() | ||
}) | ||
|
||
it('returns `true` if object attribute equals to specified value', () => { | ||
expect(fixture.nativeElement.textContent).to.equal('true') | ||
}) | ||
|
||
if (App !== AppWithAblePurePipe) { | ||
it('updates template when object attribute is changed', () => { | ||
post.author = 'not me' | ||
fixture.detectChanges() | ||
|
||
expect(fixture.nativeElement.textContent).to.equal('false') | ||
}) | ||
} | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
packages/casl-angular/src/module.ts → packages/casl-angular/src/AbilityModule.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export * from './can'; | ||
export * from './module'; | ||
export * from './pipes'; | ||
export * from './AbilityModule'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters