2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2019-06-21 21:05:45 +00:00
|
|
|
import commonPrefix from 'core/utils/common-prefix';
|
2019-05-22 14:28:34 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
module('Unit | Util | common prefix', function () {
|
|
|
|
test('it returns empty string if called with no args or an empty array', function (assert) {
|
2019-05-22 14:28:34 +00:00
|
|
|
let returned = commonPrefix();
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, '', 'returns an empty string');
|
2019-05-22 14:28:34 +00:00
|
|
|
returned = commonPrefix([]);
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, '', 'returns an empty string for an empty array');
|
2019-05-22 14:28:34 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns empty string if there are no common prefixes', function (assert) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const secrets = ['asecret', 'secret2', 'secret3'].map((s) => ({ id: s }));
|
|
|
|
const returned = commonPrefix(secrets);
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, '', 'returns an empty string');
|
2019-05-22 14:28:34 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it returns the longest prefix', function (assert) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const secrets = ['secret1', 'secret2', 'secret3'].map((s) => ({ id: s }));
|
2019-05-22 14:28:34 +00:00
|
|
|
let returned = commonPrefix(secrets);
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, 'secret', 'finds secret prefix');
|
2022-11-09 23:15:31 +00:00
|
|
|
const greetings = ['hello-there', 'hello-hi', 'hello-howdy'].map((s) => ({ id: s }));
|
2019-05-22 14:28:34 +00:00
|
|
|
returned = commonPrefix(greetings);
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, 'hello-', 'finds hello- prefix');
|
2019-05-22 14:28:34 +00:00
|
|
|
});
|
|
|
|
|
2021-12-17 03:44:29 +00:00
|
|
|
test('it can compare an attribute that is not "id" to calculate the longest prefix', function (assert) {
|
2022-11-09 23:15:31 +00:00
|
|
|
const secrets = ['secret1', 'secret2', 'secret3'].map((s) => ({ name: s }));
|
|
|
|
const returned = commonPrefix(secrets, 'name');
|
2022-10-18 15:46:02 +00:00
|
|
|
assert.strictEqual(returned, 'secret', 'finds secret prefix from name attribute');
|
2019-05-22 14:28:34 +00:00
|
|
|
});
|
|
|
|
});
|