vault/ui/tests/unit/utils/common-prefix-test.js
Vault Automation 0c6c13dd38
license: update headers to IBM Corp. (#10229) (#10233)
* license: update headers to IBM Corp.
* `make proto`
* update offset because source file changed

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-10-21 15:20:20 -06:00

38 lines
1.6 KiB
JavaScript

/**
* Copyright IBM Corp. 2016, 2025
* SPDX-License-Identifier: BUSL-1.1
*/
import commonPrefix from 'core/utils/common-prefix';
import { module, test } from 'qunit';
module('Unit | Util | common prefix', function () {
test('it returns empty string if called with no args or an empty array', function (assert) {
let returned = commonPrefix();
assert.strictEqual(returned, '', 'returns an empty string');
returned = commonPrefix([]);
assert.strictEqual(returned, '', 'returns an empty string for an empty array');
});
test('it returns empty string if there are no common prefixes', function (assert) {
const secrets = ['asecret', 'secret2', 'secret3'].map((s) => ({ id: s }));
const returned = commonPrefix(secrets);
assert.strictEqual(returned, '', 'returns an empty string');
});
test('it returns the longest prefix', function (assert) {
const secrets = ['secret1', 'secret2', 'secret3'].map((s) => ({ id: s }));
let returned = commonPrefix(secrets);
assert.strictEqual(returned, 'secret', 'finds secret prefix');
const greetings = ['hello-there', 'hello-hi', 'hello-howdy'].map((s) => ({ id: s }));
returned = commonPrefix(greetings);
assert.strictEqual(returned, 'hello-', 'finds hello- prefix');
});
test('it can compare an attribute that is not "id" to calculate the longest prefix', function (assert) {
const secrets = ['secret1', 'secret2', 'secret3'].map((s) => ({ name: s }));
const returned = commonPrefix(secrets, 'name');
assert.strictEqual(returned, 'secret', 'finds secret prefix from name attribute');
});
});