2023-03-14 13:18:55 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
2023-08-11 13:12:13 +00:00
|
|
|
* SPDX-License-Identifier: BUSL-1.1
|
2023-03-14 13:18:55 +00:00
|
|
|
*/
|
|
|
|
|
2020-12-09 09:22:01 +00:00
|
|
|
import { module, test } from 'qunit';
|
|
|
|
import Helper from 'consul-ui/helpers/document-attrs';
|
|
|
|
|
|
|
|
const root = {
|
|
|
|
classList: {
|
|
|
|
add: () => {},
|
|
|
|
remove: () => {},
|
|
|
|
},
|
|
|
|
};
|
2022-09-15 08:43:17 +00:00
|
|
|
module('Unit | Helper | document-attrs', function () {
|
|
|
|
test('synchronize adds and removes values correctly', function (assert) {
|
2020-12-09 09:22:01 +00:00
|
|
|
let attrs, actual;
|
|
|
|
// add first helper
|
|
|
|
const a = new Helper();
|
|
|
|
attrs = a.synchronize(root, {
|
|
|
|
class: 'a b a a a a',
|
|
|
|
});
|
|
|
|
actual = [...attrs.get('class').keys()];
|
|
|
|
assert.deepEqual(actual, ['a', 'b'], 'keys are adding correctly');
|
|
|
|
const b = new Helper();
|
|
|
|
// add second helper
|
|
|
|
attrs = b.synchronize(root, {
|
|
|
|
class: 'z a a a a',
|
|
|
|
});
|
|
|
|
actual = [...attrs.get('class').keys()];
|
|
|
|
assert.deepEqual(actual, ['a', 'b', 'z'], 'more keys are added correctly');
|
|
|
|
// remove second helper
|
|
|
|
b.synchronize(root);
|
|
|
|
actual = [...attrs.get('class').keys()];
|
|
|
|
assert.deepEqual(actual, ['a', 'b'], 'keys are removed, leaving keys that need to remain');
|
|
|
|
// remove first helper
|
|
|
|
a.synchronize(root);
|
2023-02-10 21:32:19 +00:00
|
|
|
assert.strictEqual(
|
|
|
|
typeof attrs.get('class'),
|
|
|
|
'undefined',
|
2020-12-09 09:22:01 +00:00
|
|
|
'property is completely removed once its empty'
|
|
|
|
);
|
|
|
|
assert.throws(() => {
|
|
|
|
a.synchronize(root, { data: 'a' });
|
|
|
|
}, `throws an error if the attrs isn't class`);
|
|
|
|
});
|
|
|
|
});
|