Files
RSSHub/lib/utils/readable-social.test.ts
dependabot[bot] 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 68.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-20 23:11:27 +08:00

82 lines
2.5 KiB
TypeScript

import { describe, expect, test } from 'vitest';
import { fallback, queryToBoolean, queryToFloat, queryToInteger } from './readable-social';
describe('fallback', () => {
test('应该返回第一个存在的参数', () => {
expect(fallback('primary', 'secondary', 'default')).toBe('primary');
expect(fallback(undefined, 42, 0)).toBe(42);
expect(fallback(null, '', 'default')).toBe('');
});
test('应该返回默认值', () => {
expect(fallback(undefined, null, 'default')).toBe('default');
expect(fallback(null, undefined, Math.PI)).toBe(Math.PI);
});
});
describe('queryToBoolean', () => {
test('should handle truthy values', () => {
expect(queryToBoolean('1')).toBe(true);
expect(queryToBoolean('true')).toBe(true);
});
test('should handle falsy values', () => {
expect(queryToBoolean('0')).toBe(false);
expect(queryToBoolean('false')).toBe(false);
});
test('should handle undefined and array inputs', () => {
expect(queryToBoolean(undefined)).toBeUndefined();
expect(queryToBoolean([])).toBeUndefined();
expect(queryToBoolean(['false', 'true'])).toBe(false);
});
});
describe('queryToInteger', () => {
test('should parse valid integers', () => {
expect(queryToInteger('42')).toBe(42);
expect(queryToInteger('-3')).toBe(-3);
});
test('should handle invalid inputs', () => {
expect(queryToInteger(null)).toBeNull();
expect(queryToInteger('abc')).toBeNaN();
});
test('should handle array inputs', () => {
expect(queryToInteger([])).toBeUndefined();
expect(queryToInteger(['7'])).toBe(7);
});
});
describe('queryToFloat', () => {
test('should handle undefined', () => {
expect(queryToFloat(undefined)).toBeUndefined();
});
test('should handle null', () => {
expect(queryToFloat(null)).toBeNull();
});
test('should return undefined for invalid input', () => {
expect(queryToFloat('invalid')).toBeNaN();
});
test('should process array input', () => {
expect(queryToFloat([String(Math.PI)])).toBe(Math.PI);
});
test('should handle empty array', () => {
expect(queryToFloat([])).toBeUndefined();
});
test('should convert numeric string', () => {
expect(queryToFloat('9.8')).toBe(9.8);
});
test('should handle edge cases', () => {
expect(queryToFloat(String(Math.PI))).toBeCloseTo(Math.PI);
});
});