mirror of
https://github.com/serverless/serverless.git
synced 2025-12-08 19:46:03 +00:00
84 lines
1.7 KiB
JavaScript
84 lines
1.7 KiB
JavaScript
'use strict';
|
|
|
|
const expect = require('chai').expect;
|
|
const deepSortObjectByKey = require('../../../../lib/utils/deepSortObjectByKey');
|
|
|
|
describe('deepSortObjectByKey', () => {
|
|
it('handles plain object', () => {
|
|
const input = {
|
|
b: 'shouldBeLast',
|
|
a: 'shouldBeFirst',
|
|
};
|
|
|
|
const result = deepSortObjectByKey(input);
|
|
|
|
const expectedResult = JSON.stringify({
|
|
a: 'shouldBeFirst',
|
|
b: 'shouldBeLast',
|
|
});
|
|
expect(JSON.stringify(result)).to.equal(expectedResult);
|
|
});
|
|
|
|
it('handles non-object values', () => {
|
|
const input = 'shouldbereturnedasis';
|
|
|
|
const result = deepSortObjectByKey(input);
|
|
|
|
expect(result).to.equal(input);
|
|
});
|
|
|
|
it('handles array with objects', () => {
|
|
const input = [
|
|
{
|
|
b: 'shouldBeLast',
|
|
a: 'shouldBeFirst',
|
|
},
|
|
{
|
|
d: 'shouldBeLast',
|
|
c: 'shouldBeFirst',
|
|
},
|
|
];
|
|
|
|
const result = deepSortObjectByKey(input);
|
|
|
|
const expectedResult = JSON.stringify([
|
|
{
|
|
a: 'shouldBeFirst',
|
|
b: 'shouldBeLast',
|
|
},
|
|
{
|
|
c: 'shouldBeFirst',
|
|
d: 'shouldBeLast',
|
|
},
|
|
]);
|
|
expect(JSON.stringify(result)).to.equal(expectedResult);
|
|
});
|
|
|
|
it('handles nested, complex objects', () => {
|
|
const input = {
|
|
b: 'shouldBeLast',
|
|
a: {
|
|
d: 'nestedPlainValue',
|
|
c: {
|
|
f: 'shouldBeLast',
|
|
e: 'shouldBeFirst',
|
|
},
|
|
},
|
|
};
|
|
|
|
const result = deepSortObjectByKey(input);
|
|
const expectedResult = JSON.stringify({
|
|
a: {
|
|
c: {
|
|
e: 'shouldBeFirst',
|
|
f: 'shouldBeLast',
|
|
},
|
|
d: 'nestedPlainValue',
|
|
},
|
|
b: 'shouldBeLast',
|
|
});
|
|
|
|
expect(JSON.stringify(result)).to.equal(expectedResult);
|
|
});
|
|
});
|