|
- import * as fc from 'fast-check'
- import isValidFilename, { IsValidFilenameConfig } from './isValidFileName'
-
- it('should exist', () => {
- expect(isValidFilename).toBeDefined()
- })
-
- it('should be a callable', () => {
- expect(typeof isValidFilename).toBe('function')
- })
-
- it('should accept a minimum of 1 argument', () => {
- expect(isValidFilename).toHaveLength(1)
- })
-
- it('should throw an error given invalid param shapes', () => {
- fc.assert(
- fc.property(
- fc.object().filter(o => !('validExtensions' in o)),
- params => {
- expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
- }
- )
- )
- })
-
- describe('on valid extensions', () => {
- it('should throw an error given non-string and non-array values', () => {
- fc.assert(
- fc.property(
- fc.record({
- validExtensions: fc.anything().filter(e => !(typeof e === 'string' || Array.isArray(e))),
- }),
- params => {
- expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
- }
- )
- )
- })
- it('should throw an error given empty arrays', () => {
- expect(() => isValidFilename({ validExtensions: [] })).toThrow(RangeError)
- })
- it('should throw an error given non-string arrays', () => {
- fc.assert(
- fc.property(
- fc.record({
- validExtensions: fc.array(
- fc.anything().filter(e => typeof e !== 'string'),
- 1,
- 20
- ),
- }),
- params => {
- expect(() => isValidFilename((params as unknown) as IsValidFilenameConfig)).toThrow(TypeError)
- }
- )
- )
- })
- it('should return a main callable given strings', () => {
- fc.assert(
- fc.property(
- fc.record<IsValidFilenameConfig>({
- validExtensions: fc.string(),
- }),
- params => {
- expect(typeof isValidFilename(params)).toBe('function')
- }
- )
- )
- })
- it('should return a main callable given string arrays', () => {
- fc.assert(
- fc.property(
- fc.record<IsValidFilenameConfig>({
- validExtensions: fc.array(fc.string(), 1, 20),
- }),
- params => {
- expect(typeof isValidFilename(params)).toBe('function')
- }
- )
- )
- })
- })
-
- describe('on main callable', () => {
- it('should throw an error for non-string params', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.record<IsValidFilenameConfig>({
- validExtensions: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
- }),
- fc.anything().filter(v => typeof v !== 'string')
- ),
- ([params, maybeFileName]) => {
- expect(() => isValidFilename(params)(maybeFileName as string)).toThrow(TypeError)
- }
- )
- )
- })
-
- it('should return a boolean for string params', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.record<IsValidFilenameConfig>({
- validExtensions: fc.oneof(fc.string(), fc.array(fc.string(), 1, 20)),
- }),
- fc.string()
- ),
- ([params, maybeFileName]) => {
- expect(typeof isValidFilename(params)(maybeFileName)).toBe('boolean')
- }
- )
- )
- })
-
- it('should return a boolean for string params with multiple extensions enabled', () => {
- fc.assert(
- fc.property(
- fc.tuple(
- fc.record<IsValidFilenameConfig>({
- validExtensions: fc.array(
- fc.string().filter(s => /^[a-zA-Z0-9]{1,8}$/.test(s)),
- 1,
- 20
- ),
- allowMultipleExtensions: fc.constant(true),
- }),
- fc.string().filter(s => /^[a-zA-Z0-9]+$/.test(s)),
- fc.string().filter(s => /^[a-zA-Z0-9]{1,8}$/.test(s))
- ),
- ([params, maybeFileName, excludedFileName]) => {
- const { validExtensions } = params
- const chosenExtension = validExtensions[Math.floor(Math.random() * validExtensions.length)]
- const theFileName = maybeFileName + '.' + chosenExtension + '.' + excludedFileName
-
- expect(isValidFilename(params)(theFileName)).toBe(true)
- }
- )
- )
- })
- })
|