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.

89 lines
2.0 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 2 arguments', () => {
  10. expect(formatFileSize).toHaveLength(2)
  11. })
  12. describe('on numeric arguments', () => {
  13. it('should format values of |x| < 1000', () => {
  14. fc.assert(
  15. fc.property(
  16. fc.integer().filter(v => Math.abs(v) < 1000),
  17. v => {
  18. expect(formatFileSize(v)).toMatch(/^-?\d+ B$/)
  19. }
  20. )
  21. )
  22. })
  23. it('should format values of |x| >= 1000', () => {
  24. fc.assert(
  25. fc.property(
  26. fc.integer().filter(v => Math.abs(v) >= 1000),
  27. v => {
  28. expect(formatFileSize(v)).toMatch(/^-?\d+\.\d\d .B$/)
  29. }
  30. )
  31. )
  32. })
  33. it('should throw an error on NaN', () => {
  34. expect(() => formatFileSize(NaN)).toThrow(RangeError)
  35. })
  36. })
  37. describe('on non-numeric arguments', () => {
  38. it('should throw an error', () => {
  39. fc.assert(
  40. fc.property(
  41. fc.anything().filter(v => typeof v !== 'number'),
  42. v => {
  43. expect(() => formatFileSize(v as number)).toThrow(TypeError)
  44. }
  45. )
  46. )
  47. })
  48. })
  49. describe('on raw', () => {
  50. it('should throw an error on non-numeric arguments', () => {
  51. fc.assert(
  52. fc.property(
  53. fc.anything().filter(v => typeof v !== 'number'),
  54. v => {
  55. expect(() => formatFileSize(v as number, true)).toThrow(TypeError)
  56. }
  57. )
  58. )
  59. })
  60. it('should throw an error on NaN', () => {
  61. expect(() => formatFileSize(NaN, true)).toThrow(RangeError)
  62. })
  63. it('should return string on numeric values', () => {
  64. fc.assert(
  65. fc.property(fc.integer(), v => {
  66. expect(typeof formatFileSize(v, true)).toBe('string')
  67. })
  68. )
  69. })
  70. it('should format numeric values', () => {
  71. fc.assert(
  72. fc.property(fc.integer(), v => {
  73. expect(formatFileSize(v, true)).toMatch(/^-?\d[,\d]* .?B$/)
  74. })
  75. )
  76. })
  77. })