2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-09-25 16:28:26 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import { setupRenderingTest } from 'ember-qunit';
|
2023-05-01 22:15:41 +00:00
|
|
|
import { render, focus, triggerKeyEvent, typeIn, fillIn } from '@ember/test-helpers';
|
2018-06-14 18:52:00 +00:00
|
|
|
import { create } from 'ember-cli-page-object';
|
|
|
|
import hbs from 'htmlbars-inline-precompile';
|
2023-05-01 22:15:41 +00:00
|
|
|
import sinon from 'sinon';
|
2018-06-14 18:52:00 +00:00
|
|
|
import maskedInput from 'vault/tests/pages/components/masked-input';
|
|
|
|
|
|
|
|
const component = create(maskedInput);
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Integration | Component | masked input', function (hooks) {
|
2018-09-25 16:28:26 +00:00
|
|
|
setupRenderingTest(hooks);
|
2018-06-14 18:52:00 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders', async function (assert) {
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput />`);
|
|
|
|
assert.dom('[data-test-masked-input]').exists('shows masked input');
|
2018-09-25 16:28:26 +00:00
|
|
|
assert.ok(component.textareaIsPresent);
|
2023-05-01 16:43:05 +00:00
|
|
|
assert.dom('[data-test-textarea]').hasClass('masked-font', 'it renders an input with obscure font');
|
|
|
|
assert.notOk(component.copyButtonIsPresent, 'does not render copy button by default');
|
|
|
|
assert.notOk(component.downloadButtonIsPresent, 'does not render download button by default');
|
2018-06-14 18:52:00 +00:00
|
|
|
|
2023-05-01 16:43:05 +00:00
|
|
|
await component.toggleMasked();
|
|
|
|
assert.dom('.masked-value').doesNotHaveClass('masked-font', 'it unmasks when show button is clicked');
|
|
|
|
await component.toggleMasked();
|
|
|
|
assert.dom('.masked-value').hasClass('masked-font', 'it remasks text when button is clicked');
|
2021-04-22 14:58:37 +00:00
|
|
|
});
|
|
|
|
|
2023-05-01 16:43:05 +00:00
|
|
|
test('it renders correctly when displayOnly', async function (assert) {
|
2021-04-22 14:58:37 +00:00
|
|
|
this.set('value', 'value');
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput @displayOnly={{true}} @value={{this.value}} />`);
|
2021-04-22 14:58:37 +00:00
|
|
|
|
2023-05-01 16:43:05 +00:00
|
|
|
assert.dom('.masked-value').hasClass('masked-font', 'value has obscured font');
|
|
|
|
assert.notOk(component.textareaIsPresent, 'it does not render a textarea when displayOnly is true');
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2018-06-14 18:52:00 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it renders a copy button when allowCopy is true', async function (assert) {
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput @allowCopy={{true}} />`);
|
2018-10-31 17:13:57 +00:00
|
|
|
assert.ok(component.copyButtonIsPresent);
|
|
|
|
});
|
|
|
|
|
2023-05-01 16:43:05 +00:00
|
|
|
test('it renders a download button when allowDownload is true', async function (assert) {
|
|
|
|
await render(hbs`<MaskedInput @allowDownload={{true}} />`);
|
|
|
|
assert.ok(component.downloadButtonIsPresent);
|
2018-09-25 16:28:26 +00:00
|
|
|
});
|
2020-10-07 16:27:41 +00:00
|
|
|
|
2022-04-13 21:56:39 +00:00
|
|
|
test('it shortens all outputs when displayOnly and masked', async function (assert) {
|
2021-04-22 14:58:37 +00:00
|
|
|
this.set('value', '123456789-123456789-123456789');
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput @value={{this.value}} @displayOnly={{true}} />`);
|
2022-11-09 23:15:31 +00:00
|
|
|
const maskedValue = document.querySelector('.masked-value').innerText;
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(maskedValue.length, 11);
|
2021-04-22 14:58:37 +00:00
|
|
|
|
2020-10-07 16:27:41 +00:00
|
|
|
await component.toggleMasked();
|
2022-11-09 23:15:31 +00:00
|
|
|
const unMaskedValue = document.querySelector('.masked-value').innerText;
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(unMaskedValue.length, this.value.length);
|
2021-04-22 14:58:37 +00:00
|
|
|
});
|
2020-10-07 16:27:41 +00:00
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it does not unmask text on focus', async function (assert) {
|
2021-04-22 14:58:37 +00:00
|
|
|
this.set('value', '123456789-123456789-123456789');
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput @value={{this.value}} />`);
|
2021-04-22 14:58:37 +00:00
|
|
|
assert.dom('.masked-value').hasClass('masked-font');
|
|
|
|
await focus('.masked-value');
|
|
|
|
assert.dom('.masked-value').hasClass('masked-font');
|
2020-10-07 16:27:41 +00:00
|
|
|
});
|
2021-08-24 14:59:37 +00:00
|
|
|
|
2023-05-01 22:15:41 +00:00
|
|
|
test('it calls onChange events with the correct values', async function (assert) {
|
|
|
|
const changeSpy = sinon.spy();
|
|
|
|
this.set('value', 'before');
|
|
|
|
this.set('onChange', changeSpy);
|
|
|
|
await render(hbs`<MaskedInput @value={{this.value}} @onChange={{this.onChange}} />`);
|
|
|
|
await fillIn('[data-test-textarea]', 'after');
|
|
|
|
assert.true(changeSpy.calledWith('after'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('it calls onKeyUp events with the correct values', async function (assert) {
|
|
|
|
const keyupSpy = sinon.spy();
|
|
|
|
this.set('value', '');
|
|
|
|
this.set('onKeyUp', keyupSpy);
|
|
|
|
await render(hbs`<MaskedInput @name="foo" @value={{this.value}} @onKeyUp={{this.onKeyUp}} />`);
|
|
|
|
await typeIn('[data-test-textarea]', 'baz');
|
|
|
|
assert.true(keyupSpy.calledThrice, 'calls for each letter of typing');
|
|
|
|
assert.true(keyupSpy.firstCall.calledWithExactly('foo', 'b'));
|
|
|
|
assert.true(keyupSpy.secondCall.calledWithExactly('foo', 'ba'));
|
|
|
|
assert.true(keyupSpy.thirdCall.calledWithExactly('foo', 'baz'));
|
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it does not remove value on tab', async function (assert) {
|
2021-08-24 14:59:37 +00:00
|
|
|
this.set('value', 'hello');
|
2023-05-01 16:43:05 +00:00
|
|
|
await render(hbs`<MaskedInput @value={{this.value}} />`);
|
2021-08-24 14:59:37 +00:00
|
|
|
await triggerKeyEvent('[data-test-textarea]', 'keydown', 9);
|
|
|
|
await component.toggleMasked();
|
2022-11-09 23:15:31 +00:00
|
|
|
const unMaskedValue = document.querySelector('.masked-value').value;
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(unMaskedValue, this.value);
|
2021-08-24 14:59:37 +00:00
|
|
|
});
|
2018-06-14 18:52:00 +00:00
|
|
|
});
|