You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

functions.test.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { limitLastN, validateLastNLimits } from './functions';
  2. describe('limitLastN', () => {
  3. it('handles undefined mapping', () => {
  4. expect(limitLastN(0, undefined)).toBe(undefined);
  5. });
  6. describe('when a correct limit mapping is given', () => {
  7. const limits = new Map();
  8. limits.set(5, -1);
  9. limits.set(10, 8);
  10. limits.set(20, 5);
  11. it('returns undefined when less participants that the first limit', () => {
  12. expect(limitLastN(2, limits)).toBe(undefined);
  13. });
  14. it('picks the first limit correctly', () => {
  15. expect(limitLastN(5, limits)).toBe(-1);
  16. expect(limitLastN(9, limits)).toBe(-1);
  17. });
  18. it('picks the middle limit correctly', () => {
  19. expect(limitLastN(10, limits)).toBe(8);
  20. expect(limitLastN(13, limits)).toBe(8);
  21. expect(limitLastN(19, limits)).toBe(8);
  22. });
  23. it('picks the top limit correctly', () => {
  24. expect(limitLastN(20, limits)).toBe(5);
  25. expect(limitLastN(23, limits)).toBe(5);
  26. expect(limitLastN(100, limits)).toBe(5);
  27. });
  28. });
  29. });
  30. describe('validateLastNLimits', () => {
  31. describe('validates the input by returning undefined', () => {
  32. it('if lastNLimits param is not an Object', () => {
  33. expect(validateLastNLimits(5)).toBe(undefined);
  34. });
  35. it('if any key is not a number', () => {
  36. const limits = {
  37. 'abc': 8,
  38. 5: -1,
  39. 20: 5
  40. };
  41. expect(validateLastNLimits(limits)).toBe(undefined);
  42. });
  43. it('if any value is not a number', () => {
  44. const limits = {
  45. 8: 'something',
  46. 5: -1,
  47. 20: 5
  48. };
  49. expect(validateLastNLimits(limits)).toBe(undefined);
  50. });
  51. it('if any value is null', () => {
  52. const limits = {
  53. 1: 1,
  54. 5: null,
  55. 20: 5
  56. };
  57. expect(validateLastNLimits(limits)).toBe(undefined);
  58. });
  59. it('if any value is undefined', () => {
  60. const limits = {
  61. 1: 1,
  62. 5: undefined,
  63. 20: 5
  64. };
  65. expect(validateLastNLimits(limits)).toBe(undefined);
  66. });
  67. it('if the map is empty', () => {
  68. expect(validateLastNLimits({})).toBe(undefined);
  69. });
  70. });
  71. it('sorts by the keys', () => {
  72. const mappingKeys = validateLastNLimits({
  73. 10: 5,
  74. 3: 3,
  75. 5: 4
  76. }).keys();
  77. expect(mappingKeys.next().value).toBe(3);
  78. expect(mappingKeys.next().value).toBe(5);
  79. expect(mappingKeys.next().value).toBe(10);
  80. expect(mappingKeys.next().done).toBe(true);
  81. });
  82. it('converts keys and values to numbers', () => {
  83. const mapping = validateLastNLimits({
  84. 3: 3,
  85. 5: 4,
  86. 10: 5
  87. });
  88. for (const key of mapping.keys()) {
  89. expect(typeof key).toBe('number');
  90. expect(typeof mapping.get(key)).toBe('number');
  91. }
  92. });
  93. });