Skip to content

Commit

Permalink
Merge pull request #81 from penn201500:main
Browse files Browse the repository at this point in the history
增加单元测试:randomSelect.test.js
  • Loading branch information
chenbimo authored Aug 2, 2024
2 parents 4039340 + e6f77ea commit 42145e0
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions lib/data/randomSelect.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, it, expect } from 'vitest';
import yd_data_randomSelect from './randomSelect';

describe('yd_data_randomSelect', () => {
it('should return a random element from an array'),
() => {
const array = [1, 2, 3, 4, 5];
const result = array.includes(yd_data_randomSelect(array));
expect(result).toBe(true);
};

it('should return a random element from a set'),
() => {
const set = new Set([1, 2, 3, 4, 5]);
const result = Array.from(set).includes(yd_data_randomSelect(set));
expect(result).toBe(true);
};

it('should return a random char from a string'),
() => {
const string = 'helloworld';
const result = string.includes(yd_data_randomSelect(string));
expect(result).toBe(true);
};

it('should return a random value from a map'),
() => {
const map = new Map([
[1, 'first'],
[2, 'second'],
[3, 'third']
]);
const values = Array.from(map.values());
const result = values.includes(yd_data_randomSelect(map));
expect(result).toBe(true);
};

it('should return a random value from an object'),
() => {
const object = { first: 1, second: 2, third: 3 };
const values = Object.values(object);
const result = values.includes(yd_data_randomSelect(object));
expect(result).toBe(true);
};

it('should throw TypeError if input collection is empty'),
() => {
expect(() => yd_data_randomSelect()).toThrow(TypeError);
expect(() => yd_data_randomSelect([])).toThrow(TypeError);
expect(() => yd_data_randomSelect({})).toThrow(TypeError);
expect(() => yd_data_randomSelect('')).toThrow(TypeError);
expect(() => yd_data_randomSelect(new Set())).toThrow(TypeError);
expect(() => yd_data_randomSelect(new Map())).toThrow(TypeError);
};

it('should throw TypeError if input collection is not supported'),
() => {
expect(() => yd_data_randomSelect(123)).toThrow(TypeError);
expect(() => yd_data_randomSelect(null)).toThrow(TypeError);
expect(() => yd_data_randomSelect(undefined)).toThrow(TypeError);
expect(() => yd_data_randomSelect(true)).toThrow(TypeError);
expect(() => yd_data_randomSelect(false)).toThrow(TypeError);
};
});

0 comments on commit 42145e0

Please sign in to comment.