|
- import * as fc from 'fast-check'
- import isValidMimeType, { IsValidMimeTypeConfig } from './isValidMimeType'
-
- it('should exist', () => {
- expect(isValidMimeType).toBeDefined()
- })
-
- it('should be a callable', () => {
- expect(typeof isValidMimeType).toBe('function')
- })
-
- it('should accept a minimum of 1 argument', () => {
- expect(isValidMimeType).toHaveLength(1)
- })
-
- it('should throw an error given invalid param shapes', () => {
- fc.assert(
- fc.property(
- fc.object().filter(o => !('validMimeTypes' in o)),
- params => {
- expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
- }
- )
- )
- })
-
- describe('on valid MIME types', () => {
- it('should throw an error given non-string and non-array values', () => {
- fc.assert(
- fc.property(
- fc.record({
- validMimeTypes: fc.anything().filter(e => !(typeof e === 'string' || Array.isArray(e))),
- }),
- params => {
- expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
- }
- )
- )
- })
- it('should throw an error given empty arrays', () => {
- expect(() => isValidMimeType({ validMimeTypes: [] })).toThrow(RangeError)
- })
- it('should throw an error given non-string arrays', () => {
- fc.assert(
- fc.property(
- fc.record({
- validMimeTypes: fc.array(
- fc.anything().filter(e => typeof e !== 'string'),
- 1,
- 20
- ),
- }),
- params => {
- expect(() => isValidMimeType((params as unknown) as IsValidMimeTypeConfig)).toThrow(TypeError)
- }
- )
- )
- })
- it('should return a main callable given strings', () => {
- fc.assert(
- fc.property(
- fc.record<IsValidMimeTypeConfig>({
- validMimeTypes: fc.string(),
- }),
- params => {
- expect(typeof isValidMimeType(params)).toBe('function')
- }
- )
- )
- })
- it('should return a main callable given string arrays', () => {
- fc.assert(
- fc.property(
- fc.record<IsValidMimeTypeConfig>({
- validMimeTypes: fc.array(fc.string(), 1, 20),
- }),
- params => {
- expect(typeof isValidMimeType(params)).toBe('function')
- }
- )
- )
- })
- })
-
- describe('on main callable', () => {
- it('should throw an error for non-string params', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.record<IsValidMimeTypeConfig>({
- validMimeTypes: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
- }),
- fc.anything().filter(v => typeof v !== 'string')
- ),
- ([params, maybeFileName]) => {
- expect(() => isValidMimeType(params)(maybeFileName as string)).toThrow(TypeError)
- }
- )
- )
- })
-
- it('should return a boolean for string params', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.record<IsValidMimeTypeConfig>({
- validMimeTypes: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
- }),
- fc.string()
- ),
- ([params, maybeFileName]) => {
- expect(typeof isValidMimeType(params)(maybeFileName)).toBe('boolean')
- }
- )
- )
- })
-
- it('should return true when catch-all MIME type is specified', () => {
- fc.assert(
- fc.property(fc.tuple(fc.array(fc.string(), 1, 20), fc.string()), ([validMimeTypes, maybeFileName]) => {
- expect(isValidMimeType({ validMimeTypes: [...validMimeTypes, '*/*'] })(maybeFileName)).toBe(true)
- })
- )
- })
-
- it('should return true when catch-all for specified MIME type class is specified', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.string().filter(s => !s.includes('/')),
- fc.string().filter(s => !s.includes('/'))
- ),
- ([mimeTypeClass, test]) => {
- expect(isValidMimeType({ validMimeTypes: [`${mimeTypeClass}/*`] })(`${mimeTypeClass}/${test}`)).toBe(true)
- }
- )
- )
- })
- })
|