vault/ui/tests/integration/components/b64-toggle-test.js
Vault Automation b057aac746
[VAULT-43339] 1/2 Chore update TS (#13050) (#13105)
* Initial ts updgrade

* Migrate linked-block to ts to squash ts errors

* [VAULT-43339] 2/2 Update vault-reporting and add ember-intl (#13062)

* Update vault-reporting and add ember-intl

* Add setupIntl for rendering tests

Co-authored-by: Jim Wright <jim.wright@hashicorp.com>
2026-03-17 15:52:40 -07:00

62 lines
2.3 KiB
JavaScript

/**
* Copyright IBM Corp. 2016, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
import { module, test } from 'qunit';
import { setupRenderingTest } from 'vault/tests/helpers';
import { render, rerender, click, find } from '@ember/test-helpers';
import hbs from 'htmlbars-inline-precompile';
module('Integration | Component | b64 toggle', function (hooks) {
setupRenderingTest(hooks);
test('it renders', async function (assert) {
await render(hbs`<B64Toggle />`);
assert.dom('button').exists({ count: 1 });
});
test('it toggles encoding on the passed string', async function (assert) {
this.set('value', 'value');
await render(hbs`<B64Toggle @value={{this.value}} @onUpdate={{fn this.set 'value'}}/>`);
await click('button');
assert.strictEqual(this.value, btoa('value'), 'encodes to base64');
await click('button');
assert.strictEqual(this.value, 'value', 'decodes from base64');
});
test('it toggles encoding starting with base64', async function (assert) {
this.set('value', btoa('value'));
await render(
hbs`<B64Toggle @value={{this.value}} @initialEncoding="base64" @onUpdate={{fn this.set 'value'}} />`
);
assert.true(find('button').textContent.includes('Decode'), 'renders as on when in b64 mode');
await click('button');
assert.strictEqual(this.value, 'value', 'decodes from base64');
});
test('it detects changes to value after encoding', async function (assert) {
this.set('value', btoa('value'));
await render(
hbs`<B64Toggle @value={{this.value}} @initialEncoding="base64" @onUpdate={{fn this.set 'value'}} />`
);
assert.true(find('button').textContent.includes('Decode'), 'renders as on when in b64 mode');
this.set('value', btoa('value') + '=');
await rerender();
assert.true(find('button').textContent.includes('Encode'), 'toggles off since value has changed');
this.set('value', btoa('value'));
await rerender();
assert.true(
find('button').textContent.includes('Decode'),
'toggles on since value is equal to the original'
);
});
test('it does not toggle when the value is empty', async function (assert) {
this.set('value', '');
await render(hbs`<B64Toggle @value={{this.value}} @onUpdate={{fn this.set 'value'}} />`);
await click('button');
assert.true(find('button').textContent.includes('Encode'));
});
});