@cainiaofe/cn-ui-m
Version:
42 lines (41 loc) • 1.48 kB
JavaScript
import { findInArray } from '../func';
describe('findInArray function', function () {
it('returns the correct item when the item is found', function () {
var arr = [
{ value: '1', label: 'Item 1' },
{
value: '2',
label: 'Item 2',
children: [{ value: '3', label: 'Item 3' }],
},
];
var fn = function (item) { return item.value === '3'; };
var result = findInArray(arr, fn);
expect(result).toEqual({ value: '3', label: 'Item 3' });
});
it('returns null when the item is not found', function () {
var arr = [
{ value: '1', label: 'Item 1' },
{
value: '2',
label: 'Item 2',
children: [{ value: '3', label: 'Item 3' }],
},
];
var fn = function (item) { return item.value === '4'; };
var result = findInArray(arr, fn);
expect(result).toBeNull();
});
it('returns null when array is empty', function () {
var arr = [];
var fn = function (item) { return item.value === '1'; };
var result = findInArray(arr, fn);
expect(result).toBeNull();
});
it('returns null when array is undefined', function () {
var arr = undefined;
var fn = function (item) { return item.value === '1'; };
var result = findInArray(arr, fn);
expect(result).toBeNull();
});
});