From c4842b917bc89dd4d0c08300ca8a0a6e515a8660 Mon Sep 17 00:00:00 2001 From: Matthew Irish Date: Mon, 25 Mar 2019 15:01:36 -0500 Subject: [PATCH] recompute has-permissions helper when permissions service attributes change --- ui/app/helpers/has-permission.js | 10 ++++++ .../helpers/has-permission-test.js | 31 +++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 ui/tests/integration/helpers/has-permission-test.js diff --git a/ui/app/helpers/has-permission.js b/ui/app/helpers/has-permission.js index d850a85a3c88..24e878be4e76 100644 --- a/ui/app/helpers/has-permission.js +++ b/ui/app/helpers/has-permission.js @@ -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); diff --git a/ui/tests/integration/helpers/has-permission-test.js b/ui/tests/integration/helpers/has-permission-test.js new file mode 100644 index 000000000000..c5a943d62d96 --- /dev/null +++ b/ui/tests/integration/helpers/has-permission-test.js @@ -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'); + }); +});