您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

functions.test.js 3.0KB

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