Useful methods for file-related functions.
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.

77 lines
1.9 KiB

  1. import * as fc from 'fast-check'
  2. import formatFileSize from './formatFileSize'
  3. it('should exist', () => {
  4. expect(formatFileSize).toBeDefined()
  5. })
  6. it('should be a function', () => {
  7. expect(typeof formatFileSize).toBe('function')
  8. })
  9. it('should accept minimum of 1 argument', () => {
  10. expect(formatFileSize).toHaveLength(1)
  11. })
  12. describe.each`
  13. raw | largeValuePattern
  14. ${false} | ${/^-?\d+\.\d\d .B$/}
  15. ${true} | ${/^-?\d[,\d]* .?B$/}
  16. `('on raw mode = $raw', ({ raw, largeValuePattern }) => {
  17. describe('on numeric arguments', () => {
  18. it('should format values of |x| < 1000', () => {
  19. fc.assert(
  20. fc.property(
  21. fc.integer().filter(v => v >= 0 && Math.abs(v) < 1000),
  22. v => {
  23. expect(formatFileSize(v, raw)).toMatch(/^-?\d+ B$/)
  24. }
  25. )
  26. )
  27. })
  28. it('should format values of |x| >= 1000', () => {
  29. fc.assert(
  30. fc.property(
  31. fc.integer().filter(v => v >= 0 && Math.abs(v) >= 1000),
  32. v => {
  33. expect(formatFileSize(v, raw)).toMatch(largeValuePattern)
  34. }
  35. )
  36. )
  37. })
  38. it('should throw an error on NaN', () => {
  39. expect(() => formatFileSize(NaN)).toThrow(RangeError)
  40. })
  41. it('should throw an error on negative values', () => {
  42. fc.assert(
  43. fc.property(
  44. fc.nat().filter(v => v !== 0),
  45. v => {
  46. expect(() => formatFileSize(-v, raw)).toThrow(RangeError)
  47. }
  48. )
  49. )
  50. })
  51. it('should throw an error on positive infinity', () => {
  52. expect(() => formatFileSize(Number.POSITIVE_INFINITY, raw)).toThrow(RangeError)
  53. })
  54. })
  55. describe('on non-numeric arguments', () => {
  56. it('should throw an error', () => {
  57. fc.assert(
  58. fc.property(
  59. fc.anything().filter(v => typeof v !== 'number'),
  60. v => {
  61. expect(() => formatFileSize(v as number, raw)).toThrow(TypeError)
  62. }
  63. )
  64. )
  65. })
  66. })
  67. })