var assert = require('chai').assert;
var arrayToObject = require(`${__dirname}/../../../src/steps/arrayToObject`);
describe('#arrayToObject', function () {
it('should convert array to object', function () {
var config = {
from: 'a',
to: 'b',
byKey: 'id'
};
var data = {
a: [{id: 1, login: 1}, {id: 2, login: 2}]
};
var result = {};
arrayToObject(config, data, result);
var valid = {
b: {
1: {id: 1, login: 1},
2: {id: 2, login: 2}
}
};
assert.deepEqual(valid, result);
});
it('should error on invalid config', function () {
var data = {
a: [{id: 1, login: 1}, {id: 2, login: 2}]
};
var result = {};
assert.throws(x => arrayToObject({}, data, result), Error, 'invalid config fo step "arrayToObject"');
assert.throws(x => arrayToObject({from: 'a'}, data, result), Error, 'invalid config fo step "arrayToObject"');
assert.throws(x => arrayToObject({to: 'a'}, data, result), Error, 'invalid config fo step "arrayToObject"');
assert.throws(x => arrayToObject({byKey: 'a'}, data, result), Error, 'invalid config fo step "arrayToObject"');
});
});
const { arrayToObject } = require("../lib");
describe("arrayToObject", () => {
test("Should return an empty object", () => {
expect(arrayToObject()).toEqual({});
expect(arrayToObject([])).toEqual({});
expect(arrayToObject(null)).toEqual({});
expect(arrayToObject("fff")).toEqual({});
expect(arrayToObject(55)).toEqual({});
});
test("Should return an object with correct keys/values", () => {
let obj = [33, "foo", "bing"];
let withVal = ["33", "foo", ["bing", "free"]];
let withValObj = { "33": undefined, "foo": undefined, "bing": "free" };
expect(arrayToObject(obj)).toEqual({
"33": undefined,
"foo": undefined,
"bing": undefined,
});
expect(arrayToObject(withVal)).toEqual(withValObj);
});
test("Should return a nested object with correct keys/values", () => {
let arr = ["33", "foo", ["bing", "free", ["bop", 72]]];
let obj = { "33": undefined, "foo": undefined, "bing": { "free": undefined, "bop": 72 }};
let nestedArr = ["33", "foo", ["bing", "free", ["bop", 72, "bar"]]];
let nestedObj = { "33": undefined, "foo": undefined, "bing": { "free": undefined, "bop": { "72": undefined, "bar": undefined }}};
expect(arrayToObject(arr)).toEqual(obj);
expect(arrayToObject(nestedArr)).toEqual(nestedObj);
});
});
'use strict';
const arrayToObject = require('../../src/util/array-to-object');
describe('arrayToObject', () => {
it('fills in an object by joining an array of properties with the names of the properties', () => {
expect(arrayToObject(['a', 'b', 'c'], ['na', 'nb', 'nc'])).toEqual({na: 'a', nb: 'b', nc: 'c'});
});
it('survives unequal length arrays', () => {
expect(arrayToObject(['a'], ['na', 'nb'])).toEqual({na: 'a'});
expect(arrayToObject(['a', 'b'], ['na'])).toEqual({na: 'a'});
});
it('survives empty arrays', () => {
expect(arrayToObject([], ['na', 'nb'])).toEqual({});
expect(arrayToObject(['a', 'b'], [])).toEqual({});
});
it('throws if arguments are not arrays', () => {
expect(() => arrayToObject({}, ['na'])).toThrowError(/values must be an array/);
expect(() => arrayToObject(['a'], {})).toThrowError(/names must be an array/);
});
});
Accelerate Your Automation Test Cycles With LambdaTest
Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.