|
- import * as fc from 'fast-check'
- import formatFileSize from './formatFileSize'
-
- it('should exist', () => {
- expect(formatFileSize).toBeDefined()
- })
-
- it('should be a function', () => {
- expect(typeof formatFileSize).toBe('function')
- })
-
- it('should accept minimum of 1 argument', () => {
- expect(formatFileSize).toHaveLength(1)
- })
-
- describe.each`
- raw | largeValuePattern
- ${false} | ${/^-?\d+\.\d\d .B$/}
- ${true} | ${/^-?\d[,\d]* .?B$/}
- `('on raw mode = $raw', ({ raw, largeValuePattern }) => {
- describe('on numeric arguments', () => {
- it('should format values of |x| < 1000', () => {
- fc.assert(
- fc.property(
- fc.integer().filter(v => v >= 0 && Math.abs(v) < 1000),
- v => {
- expect(formatFileSize(v, raw)).toMatch(/^-?\d+ B$/)
- }
- )
- )
- })
-
- it('should format values of |x| >= 1000', () => {
- fc.assert(
- fc.property(
- fc.integer().filter(v => v >= 0 && Math.abs(v) >= 1000),
- v => {
- expect(formatFileSize(v, raw)).toMatch(largeValuePattern)
- }
- )
- )
- })
-
- it('should throw an error on NaN', () => {
- expect(() => formatFileSize(NaN)).toThrow(RangeError)
- })
-
- it('should throw an error on negative values', () => {
- fc.assert(
- fc.property(
- fc.nat().filter(v => v !== 0),
- v => {
- expect(() => formatFileSize(-v, raw)).toThrow(RangeError)
- }
- )
- )
- })
-
- it('should throw an error on positive infinity', () => {
- expect(() => formatFileSize(Number.POSITIVE_INFINITY, raw)).toThrow(RangeError)
- })
- })
-
- describe('on non-numeric arguments', () => {
- it('should throw an error', () => {
- fc.assert(
- fc.property(
- fc.anything().filter(v => typeof v !== 'number'),
- v => {
- expect(() => formatFileSize(v as number, raw)).toThrow(TypeError)
- }
- )
- )
- })
- })
- })
|