bruno/packages/bruno-js/tests/get.spec.js

48 lines
1.3 KiB
JavaScript
Raw Normal View History

2023-02-13 00:27:54 +01:00
const { filter } = require("lodash");
const { get } = require("../src/get");
const data = {
customer: {
address: {
city: "bangalore"
},
orders: [
{
id: "order-1",
items: [
{ id: 1, amount: 10 },
{ id: 2, amount: 20 },
]
},
{
id: "order-2",
items: [
{ id: 3, amount: 30, },
{ id: 4, amount: 40 }
]
}
]
},
};
describe("get", () => {
it.each([
["customer.address.city", "bangalore"],
["customer.orders.items.amount", [10, 20, 30, 40]],
["customer.orders.items.amount[0]", 10],
["..items.amount", [10, 20, 30, 40]],
["..amount", [10, 20, 30, 40]],
["..items.amount[0]", 10],
["..items[0].amount", 10],
["..items[?].amount", [40], (i) => i.amount > 30], // [?] filter
["..id", ["order-1", 1, 2, "order-2", 3, 4]], // all ids
["customer.orders.foo", undefined],
["..customer.foo", undefined],
["..address", [{ city: "bangalore" }]], // .. will return array
2023-02-13 02:47:14 +01:00
["..address[0]", { city: "bangalore" }],
["..items..amount[?][0]", 40, amt => amt > 30]
2023-02-13 00:27:54 +01:00
])("%s should be %j %s", (expr, result, filter = undefined) => {
expect(get(data, expr, filter)).toEqual(result);
});
});