Skip to content

Commit

Permalink
Ui/ttl wrap (#9632)
Browse files Browse the repository at this point in the history
* Use TtlPicker2 in wrap-ttl component
* Fix tests around wrap-ttl
  • Loading branch information
chelshaw authored Jul 30, 2020
1 parent 090086f commit f5db8b4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 39 deletions.
34 changes: 13 additions & 21 deletions ui/app/components/wrap-ttl.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default Component.extend({
onChange: null,
wrapResponse: true,

ttl: null,
ttl: '30m',

wrapTTL: computed('wrapResponse', 'ttl', function() {
const { wrapResponse, ttl } = this.getProperties('wrapResponse', 'ttl');
Expand All @@ -27,30 +27,22 @@ export default Component.extend({

layout: hbs`
<div class="field">
<div class="b-checkbox">
<input
id="wrap-response"
class="styled"
name="wrap-response"
type="checkbox"
checked={{wrapResponse}}
onchange={{action 'changedValue' 'wrapResponse'}}
/>
<label for="wrap-response" class="is-label">
Wrap response
</label>
</div>
{{#if wrapResponse}}
{{ttl-picker data-test-wrap-ttl-picker=true labelText='Wrap TTL' onChange=(action (mut ttl))}}
{{/if}}
{{ttl-picker2
data-test-wrap-ttl-picker=true
label='Wrap response'
helperTextDisabled='Will not wrap response'
helperTextEnabled='Will wrap response with a lease of'
enableTTL=wrapResponse
initialValue=ttl
onChange=(action 'changedValue')
}}
</div>
`,

actions: {
changedValue(key, event) {
const { type, value, checked } = event.target;
const val = type === 'checkbox' ? checked : value;
set(this, key, val);
changedValue(ttlObj) {
set(this, 'wrapResponse', ttlObj.enabled);
set(this, 'ttl', `${ttlObj.seconds}s`);
get(this, 'onChange')(get(this, 'wrapTTL'));
},
},
Expand Down
6 changes: 2 additions & 4 deletions ui/tests/integration/components/transit-key-actions-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@ module('Integration | Component | transit key actions', function(hooks) {
const response = { keys: { a: 'key' } };
this.set('storeService.keyActionReturnVal', response);
await setupExport.call(this);
await click('#wrap-response');
await triggerEvent('#wrap-response', 'change');
await click('[data-test-toggle-label="Wrap response"]');
await click('button[type="submit"]');
assert.dom('.modal.is-active').exists('Modal opens after export');
assert.deepEqual(
Expand All @@ -295,8 +294,7 @@ module('Integration | Component | transit key actions', function(hooks) {
const response = { keys: { a: 'key' } };
this.set('storeService.keyActionReturnVal', response);
await setupExport.call(this);
await click('#wrap-response');
await triggerEvent('#wrap-response', 'change');
await click('[data-test-toggle-label="Wrap response"]');
await click('#exportVersion');
await triggerEvent('#exportVersion', 'change');
await click('button[type="submit"]');
Expand Down
25 changes: 11 additions & 14 deletions ui/tests/integration/components/wrap-ttl-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
import { render, click, fillIn, blur, find, triggerEvent } from '@ember/test-helpers';
import { render, click, fillIn, find } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
import waitForError from 'vault/tests/helpers/wait-for-error';

Expand All @@ -24,28 +24,25 @@ module('Integration | Component | wrap ttl', function(hooks) {
test('it renders', async function(assert) {
await render(hbs`{{wrap-ttl onChange=(action onChange)}}`);
assert.equal(this.lastOnChangeCall, '30m', 'calls onChange with 30m default on first render');
assert.equal(find('label[for=wrap-response]').textContent.trim(), 'Wrap response');
// await this.pauseTest();
assert.equal(
find('label[for="toggle-Wrapresponse"] .ttl-picker-label').textContent.trim(),
'Wrap response'
);
});

test('it nulls out value when you uncheck wrapResponse', async function(assert) {
await render(hbs`{{wrap-ttl onChange=(action onChange)}}`);
await click('#wrap-response');
await triggerEvent('#wrap-response', 'change');
await click('[data-test-toggle-label="Wrap response"]');
assert.equal(this.lastOnChangeCall, null, 'calls onChange with null');
});

test('it sends value changes to onChange handler', async function(assert) {
await render(hbs`{{wrap-ttl onChange=(action onChange)}}`);

await fillIn('[data-test-wrap-ttl-picker] input', '20');
assert.equal(this.lastOnChangeCall, '20m', 'calls onChange correctly on time input');

// for testing purposes we need to input unit first because it keeps seconds value
await fillIn('[data-test-select="ttl-unit"]', 'h');
await blur('[data-test-select="ttl-unit"]');
assert.equal(this.lastOnChangeCall, '20h', 'calls onChange correctly on unit change');

await fillIn('[data-test-select="ttl-unit"]', 'd');
await blur('[data-test-select="ttl-unit"]');
assert.equal(this.lastOnChangeCall, '480h', 'converts days to hours correctly');
assert.equal(this.lastOnChangeCall, '1800s', 'calls onChange correctly on time input');
await fillIn('[data-test-ttl-value="Wrap response"]', '20');
assert.equal(this.lastOnChangeCall, '72000s', 'calls onChange correctly on unit change');
});
});

0 comments on commit f5db8b4

Please sign in to comment.