open-nomad/ui/tests/unit/helpers/stringify-object-test.js
2023-04-10 15:36:59 +00:00

71 lines
1.8 KiB
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import { module, test } from 'qunit';
import { stringifyObject } from 'nomad-ui/helpers/stringify-object';
const objectToStringify = {
dog: 'thelonious',
dogAge: 12,
dogDegreesHeld: null,
};
module('Unit | Helper | stringify-object', function () {
test('Contains the correct number of whitespace', function (assert) {
// assertions with whitespace > 0 are whitespace + 3, to account for quote, newline and \ characters
assert.equal(
stringifyObject([objectToStringify], {
whitespace: 10,
}).indexOf('dog'),
13,
'Ten spaces'
);
assert.equal(
stringifyObject([objectToStringify], {
whitespace: 5,
}).indexOf('dog'),
8,
'Five spaces'
);
assert.equal(
stringifyObject([objectToStringify], {
whitespace: 0,
}).indexOf('dog'),
2,
'Zero spaces'
);
});
test('Observes replacer array', function (assert) {
assert.ok(
stringifyObject([objectToStringify], {
replacer: ['dog', 'dogDegreesHeld'],
}).indexOf('dogDegreesHeld'),
'Unreplaced value is present'
);
assert.equal(
stringifyObject([objectToStringify], {
replacer: ['dog', 'dogDegreesHeld'],
}).indexOf('dogAge'),
-1,
'Replaced value is missing'
);
});
test('Observes replacer function', function (assert) {
assert.ok(
stringifyObject([objectToStringify], {
replacer: (k, v) => (v ? v : undefined),
}).indexOf('dogAge'),
'Unreplaced value is present'
);
assert.equal(
stringifyObject([objectToStringify], {
replacer: (k, v) => (v ? v : undefined),
}).indexOf('dogDegreesHeld'),
-1,
'Replaced value is missing'
);
});
});