File size: 1,624 Bytes
9438a57
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
const { pageCount, clampPage, pageItems, pageButtons } = require('./solution');

describe('pageCount', () => {
  test('rounds up and has at least one page', () => {
    expect(pageCount(10, 3)).toBe(4);
    expect(pageCount(9, 3)).toBe(3);
    expect(pageCount(0, 5)).toBe(1);
  });
  test('validates arguments', () => {
    expect(() => pageCount(5, 0)).toThrow(RangeError);
    expect(() => pageCount(5, 2.5)).toThrow(RangeError);
    expect(() => pageCount(-1, 5)).toThrow(RangeError);
  });
});

test('clampPage', () => {
  expect(clampPage(0, 4)).toBe(1);
  expect(clampPage(9, 4)).toBe(4);
  expect(clampPage(3, 4)).toBe(3);
});

describe('pageItems', () => {
  const items = [1, 2, 3, 4, 5, 6, 7];
  test('returns the requested page', () => {
    expect(pageItems(items, 1, 3)).toEqual([1, 2, 3]);
    expect(pageItems(items, 3, 3)).toEqual([7]);
  });
  test('clamps out-of-range pages', () => {
    expect(pageItems(items, 99, 3)).toEqual([7]);
    expect(pageItems(items, -2, 3)).toEqual([1, 2, 3]);
  });
});

describe('pageButtons', () => {
  test('all pages when few', () => {
    expect(pageButtons(2, 3)).toEqual([1, 2, 3]);
    expect(pageButtons(1, 1)).toEqual([1]);
  });
  test('ellipses on both sides', () => {
    expect(pageButtons(5, 10)).toEqual([1, '…', 4, 5, 6, '…', 10]);
  });
  test('single-page gap is filled with the page itself', () => {
    expect(pageButtons(4, 10)).toEqual([1, 2, 3, 4, 5, '…', 10]);
  });
  test('window size', () => {
    expect(pageButtons(5, 10, 2)).toEqual([1, 2, 3, 4, 5, 6, 7, '…', 10]);
    expect(pageButtons(1, 10, 0)).toEqual([1, '…', 10]);
  });
});