|
- import * as fc from 'fast-check'
- import File from './File'
-
- jest.mock('./isBrowser', function IsBrowser() {
- return () => false
- })
-
- it('should exist', () => {
- expect(File).toBeDefined()
- })
-
- it('should be a callable', () => {
- expect(typeof File).toBe('function')
- })
-
- describe('instances', () => {
- it('should contain a name', () => {
- fc.assert(
- fc.property(
- fc.string().filter(s => /^[a-zA-Z0-9._-]+$/.test(s)),
- fileName => {
- const file = new File([], fileName, { type: 'application/octet-stream' })
- expect(file).toHaveProperty('name', fileName)
- }
- )
- )
- })
-
- it('should contain a last modified timestamp', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.string().filter(s => /^[a-zA-Z0-9._-]+$/.test(s)),
- fc.nat()
- ),
- ([fileName, lastModified]) => {
- const file = new File([], fileName, { type: 'application/octet-stream', lastModified })
- expect(file).toHaveProperty('lastModified', lastModified)
- }
- )
- )
- })
-
- it('should contain a type', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.oneof(
- fc.constant('application'),
- fc.constant('font'),
- fc.constant('audio'),
- fc.constant('model'),
- fc.constant('video'),
- fc.constant('text')
- ),
- fc.string().filter(s => /^[a-zA-Z0-9._-]+$/.test(s))
- ),
- mimeTypeFragments => {
- const file = new File([], 'foo', { type: mimeTypeFragments.join('/') })
- expect(file).toHaveProperty('type', mimeTypeFragments.join('/').toLowerCase())
- }
- )
- )
- })
- })
-
- describe('on cases for last modified date', () => {
- // We put lastModified as `any` because native DOM types classify
- // `lastModified` as number only under FilePropertyBag (but browser
- // accepts both Date and number objects equally.
- it('should be 0 given invalid date objects', () => {
- fc.assert(
- fc.property(
- fc.anything().filter(
- s =>
- // `new Date(Number.MAX_SAFE_INTEGER)` returns Invalid Date, but
- // is ok with File instances on browsers. Weird. This is why we
- // exclude numbers that will generate invalid dates (we already
- // covered numbers previously anyway).
- typeof s !== 'number' && isNaN(new Date(s as Date).getTime())
- ),
- v => {
- const lastModified = new Date(v as Date)
- const file = new File([], 'foo', { type: 'application/octet-stream', lastModified: lastModified as any })
- expect(file.lastModified).toBe(0)
- }
- )
- )
- })
-
- it('should be 0 given NaN', () => {
- const file = new File([], 'foo', { type: 'application/octet-stream', lastModified: NaN })
- expect(file.lastModified).toBe(0)
- })
-
- it('should be 0 given generic object values', () => {
- fc.assert(
- fc.property(fc.object(), o => {
- const file = new File([], 'foo', { type: 'application/octet-stream', lastModified: o as any })
- expect(file.lastModified).toBe(0)
- })
- )
- })
-
- it('should be a corresponding integer timestamp given valid date objects', () => {
- fc.assert(
- fc.property(fc.date(), lastModified => {
- const file = new File([], 'foo', { type: 'application/octet-stream', lastModified: lastModified as any })
- expect(file.lastModified).toBe(lastModified.getTime())
- })
- )
- })
- })
|