aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/util/sample.spec.ts
blob: 7d919e6ae0fbf30ddb274c04958a1d85d08604d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { sampleSize } from './sample';

describe('util/sample', () => {
  describe('sampleSize', () => {
    const array = ['a', 'b', 'c', 'd'];

    it('returns correct sized array', () => {
      expect(sampleSize(array, 2)).toHaveLength(2);
      expect(sampleSize(array, 10)).toHaveLength(4);
    });

    it('returns full array for undefined number', () => {
      expect(sampleSize(array, undefined as never)).toEqual(array);
    });

    it('returns full array for null number', () => {
      expect(sampleSize(array, null as never)).toBeEmptyArray();
    });

    it('returns full array for 0 number', () => {
      expect(sampleSize(array, 0)).toBeEmptyArray();
    });

    it('returns empty array for null array', () => {
      expect(sampleSize(null as never, 1)).toBeEmptyArray();
    });

    it('returns empty array for undefined array', () => {
      expect(sampleSize(undefined as never, 1)).toBeEmptyArray();
    });

    it('returns empty array for empty array', () => {
      expect(sampleSize([], 1)).toBeEmptyArray();
    });
  });
});