diff --git a/README.md b/README.md index 163e77c..cf2155f 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Tiny cypress command to upload fast large files to an HTML input type="file". The file that will be uploaded is empty. Multiple attribute is not supported {yet}. - # Installation ```bash npm i dummyattachfile @@ -19,7 +18,10 @@ import 'dummyattachfile'; ```javascript cy.get('input').dummyAttachFile(, , ) -// Number | Optional | Default value: 1024 +// Number or String | Optional | Default value: 1024 +// Number expressed in bytes +// Valid String Format: d+K, d+M eg. 666K, 500M etc. +// Current file limit: 1GB // String | Optional | Default value: 'whatever.txt' // String | Optional | Default value: 'text/plain' diff --git a/package.json b/package.json index 21c788b..bb2ef61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dummyattachfile", - "version": "0.0.12", + "version": "0.0.13", "description": "dummy uploading a file for cypress tests", "main": "dist/bundle.js", "source": "src/index.js", diff --git a/src/main.js b/src/main.js index 4f033ab..15cd30a 100644 --- a/src/main.js +++ b/src/main.js @@ -1,13 +1,49 @@ export default function dummyAttachFile(subject, sizeBytes = 1024, fileName = 'whatever.txt', fileType = 'text/plain') { + // if bytes do not respect the FILE_LIMIT + // force it to FILE_LIMIT + const FILE_LIMIT = 1047527424 + // if arguments do not respect their type // convert them to defaults + if (typeof sizeBytes !== 'number') { + if (typeof sizeBytes === 'string') { + const re = /^\d+[MK]{1}$/ + if (re.test(sizeBytes)) { + const MB = sizeBytes.charAt(sizeBytes.length-1) + const parsed = parseInt(sizeBytes.substring(0, sizeBytes.length-1), 10) + // is it possible isNaN to be true?? + // since regexp has handled the format? + if (isNaN(parsed)){ + sizeBytes = 1024 + } + else { + // 'K' Kilobytes case + sizeBytes = parsed*1024 + // 'M' Megabytes case + if ( MB === 'M') { + sizeBytes *= 1024 + } + + } + } else { + sizeBytes = 1024 + } + } + else { sizeBytes = 1024 + } } + + if (sizeBytes > FILE_LIMIT) { + sizeBytes = FILE_LIMIT + } + if (typeof fileName !== 'string') { fileName = 'whatever.txt' } + if (typeof fileType !== 'string') { fileType = 'text/plain' }