oxjs/test/basic.test.js

80 lines
3.2 KiB
JavaScript
Raw Normal View History

import { describe, it, expect, beforeAll } from 'vitest';
import * as Type from '../src/ox/core/Type.js';
import * as Collection from '../src/ox/core/Collection.js';
describe('Basic ES Module Tests', () => {
describe('Type utilities', () => {
it('should correctly identify types', () => {
expect(Type.typeOf([])).toBe('array');
expect(Type.typeOf({})).toBe('object');
expect(Type.typeOf('test')).toBe('string');
expect(Type.typeOf(123)).toBe('number');
expect(Type.typeOf(true)).toBe('boolean');
expect(Type.typeOf(null)).toBe('null');
expect(Type.typeOf(undefined)).toBe('undefined');
expect(Type.typeOf(new Date())).toBe('date');
expect(Type.typeOf(/test/)).toBe('regexp');
expect(Type.typeOf(NaN)).toBe('nan');
});
it('should check for empty values', () => {
expect(Type.isEmpty([])).toBe(true);
expect(Type.isEmpty({})).toBe(true);
expect(Type.isEmpty('')).toBe(true);
expect(Type.isEmpty(null)).toBe(true);
expect(Type.isEmpty(undefined)).toBe(true);
expect(Type.isEmpty([1])).toBe(false);
expect(Type.isEmpty({a: 1})).toBe(false);
expect(Type.isEmpty('test')).toBe(false);
});
it('should check equality', () => {
expect(Type.isEqual([1, 2, 3], [1, 2, 3])).toBe(true);
expect(Type.isEqual({a: 1, b: 2}, {b: 2, a: 1})).toBe(true);
expect(Type.isEqual(NaN, NaN)).toBe(true);
expect(Type.isEqual([1, 2], [1, 2, 3])).toBe(false);
expect(Type.isEqual({a: 1}, {a: 2})).toBe(false);
});
});
describe('Collection utilities', () => {
it('should iterate over arrays', () => {
const result = [];
Collection.forEach([1, 2, 3], (val, idx) => {
result.push(val * 2);
});
expect(result).toEqual([2, 4, 6]);
});
it('should iterate over objects', () => {
const result = {};
Collection.forEach({a: 1, b: 2, c: 3}, (val, key) => {
result[key] = val * 2;
});
expect(result).toEqual({a: 2, b: 4, c: 6});
});
it('should map over collections', () => {
expect(Collection.map([1, 2, 3], x => x * 2)).toEqual([2, 4, 6]);
expect(Collection.map('abc', x => x.toUpperCase())).toBe('ABC');
});
it('should filter collections', () => {
expect(Collection.filter([1, 2, 3, 4], x => x % 2 === 0)).toEqual([2, 4]);
expect(Collection.filter({a: 1, b: 2, c: 3}, x => x > 1)).toEqual({b: 2, c: 3});
});
it('should extend objects', () => {
const target = {a: 1};
const result = Collection.extend(target, {b: 2}, {c: 3});
expect(result).toEqual({a: 1, b: 2, c: 3});
expect(result).toBe(target); // Should modify in place
});
it('should deep extend objects', () => {
const target = {a: {x: 1}};
const result = Collection.extend(true, target, {a: {y: 2}});
expect(result).toEqual({a: {x: 1, y: 2}});
});
});
});