Skip to content

Commit be569f2

Browse files
author
hromero
committed
Unit Tests
1 parent 762a1b3 commit be569f2

File tree

2 files changed

+82
-13
lines changed

2 files changed

+82
-13
lines changed

Diff for: tests/unit/example.spec.js

-13
This file was deleted.

Diff for: tests/unit/table-utils.spec.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import { expect } from 'chai'
2+
import { calculateTotalPages, isNumeric, getPropertyValue, doPaginate } from '../../src/table-utils.js'
3+
4+
let scenario = [
5+
{ totalItems: 10, pageSize: 5, result: 2 },
6+
{ totalItems: 1, pageSize: 10, result: 1 },
7+
{ totalItems: 11, pageSize: 10, result: 2 },
8+
{ totalItems: 200, pageSize: 200, result: 1 },
9+
{ totalItems: 200, pageSize: 1, result: 200 }
10+
]
11+
12+
scenario.forEach(({ totalItems, pageSize, result }) => {
13+
describe('calculateTotalPages', () => {
14+
it(`Should be ${result} pages when totalItems is ${totalItems} and pageSize: is ${pageSize}`, () => {
15+
expect(calculateTotalPages(totalItems, pageSize))
16+
.to.equal(result)
17+
})
18+
})
19+
})
20+
21+
scenario = [
22+
{ toCheck: 5, result: true },
23+
{ toCheck: 1.0, result: true },
24+
{ toCheck: -1.0, result: true },
25+
// eslint-disable-next-line no-floating-decimal
26+
{ toCheck: .5, result: true },
27+
{ toCheck: 0.8, result: true },
28+
{ toCheck: '0.5', result: true },
29+
{ toCheck: '-0.5', result: true },
30+
{ toCheck: 'asd', result: false },
31+
{ toCheck: '5,2', result: false },
32+
{ toCheck: [1], result: false },
33+
{ toCheck: [], result: false },
34+
{ toCheck: { value: 1 }, result: false }
35+
]
36+
37+
scenario.forEach(({ toCheck, result }) => {
38+
describe('isNumeric', () => {
39+
it(`${toCheck} should ${result ? '' : 'not'} be numeric`, () => {
40+
expect(isNumeric(toCheck))
41+
.to.equal(result)
42+
})
43+
})
44+
})
45+
46+
scenario = [
47+
{ object: { value: 'asd', values: 123 }, path: 'value', result: 'asd' },
48+
{ object: { value: 'asd', values: 123 }, path: '[value]', result: 'asd' },
49+
{ object: { a: { b: { c: 13 } } }, path: 'a.b.c', result: 13 },
50+
{ object: { a: { b: { c: 13 } } }, path: 'a[b].c', result: 13 },
51+
{ object: { value: 'asd' }, path: 'none', result: undefined },
52+
{ object: {}, path: 'empty', result: undefined }
53+
]
54+
55+
scenario.forEach(({ object, path, result }) => {
56+
describe('getPropertyValue', () => {
57+
it(`path '${path}' should be ${result}`, () => {
58+
expect(getPropertyValue(object, path))
59+
.to.equal(result)
60+
})
61+
})
62+
})
63+
64+
let toPaginate = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
65+
scenario = [
66+
{ pageSize: 3, currentPage: 1, result: [1, 2, 3] },
67+
{ pageSize: 5, currentPage: 2, result: [6, 7, 8, 9, 10] },
68+
{ pageSize: 2, currentPage: 3, result: [5, 6] },
69+
{ pageSize: 5, currentPage: 3, result: [] },
70+
{ pageSize: 50, currentPage: 1, result: toPaginate },
71+
{ pageSize: 0, currentPage: 1, result: toPaginate },
72+
{ pageSize: 5, currentPage: 0, result: toPaginate }
73+
]
74+
75+
scenario.forEach(({ pageSize, currentPage, result }) => {
76+
describe('doPaginate', () => {
77+
it(`With size: ${pageSize} and currentPage: ${currentPage} it should return ${result}`, () => {
78+
expect(doPaginate(toPaginate, pageSize, currentPage))
79+
.to.eql(result)
80+
})
81+
})
82+
})

0 commit comments

Comments
 (0)