Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recompute has-permissions helper on permission service attr change #6473

Merged
merged 1 commit into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions ui/app/helpers/has-permission.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import Helper from '@ember/component/helper';
import { inject as service } from '@ember/service';
import { observer } from '@ember/object';

export default Helper.extend({
permissions: service(),
onPermissionsChange: observer(
'permissions.exactPaths',
'permissions.globPaths',
'permissions.canViewAll',
function() {
this.recompute();
}
),

compute([route], { routeParams, capability }) {
let permissions = this.permissions;
return permissions.hasNavPermission(route, routeParams, capability);
Expand Down
31 changes: 31 additions & 0 deletions ui/tests/integration/helpers/has-permission-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render } from '@ember/test-helpers';
import { run } from '@ember/runloop';
import hbs from 'htmlbars-inline-precompile';
import Service from '@ember/service';

const Permissions = Service.extend({
globPaths: null,
hasNavPermission() {
return this.globPaths ? true : false;
},
});

module('Integration | Helper | has-permission', function(hooks) {
setupRenderingTest(hooks);
hooks.beforeEach(function() {
this.owner.register('service:permissions', Permissions);
this.permissions = this.owner.lookup('service:permissions');
});

test('it renders', async function(assert) {
await render(hbs`{{#if (has-permission)}}Yes{{else}}No{{/if}}`);

assert.equal(this.element.textContent.trim(), 'No');
await run(() => {
this.permissions.set('globPaths', { 'test/': { capabilities: ['update'] } });
});
assert.equal(this.element.textContent.trim(), 'Yes', 'the helper re-computes when globPaths changes');
});
});