Skip to content

Instantly share code, notes, and snippets.

@Xotabu4
Created January 10, 2020 10:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Xotabu4/846ffc7159ac5782154a88f1d854b55b to your computer and use it in GitHub Desktop.
Save Xotabu4/846ffc7159ac5782154a88f1d854b55b to your computer and use it in GitHub Desktop.
Test files management module. TestResources
import * as path from 'path'
import * as fs from 'fs'
const resFolderImages = './resources/images/'
const resFolderAudio = './resources/audio/'
const resFolderOffice = './resources/office/'
const resFolderVideos = './resources/videos/'
const resFolderOther = './resources/other/'
export interface FileStats {
extension: string
filename: string
absolutePath: string
}
export interface IResources {
all(): FileStats[]
audio(): FileStats[]
images(): FileStats[]
office(): FileStats[]
other(): FileStats[]
videos(): FileStats[]
}
/**
* Metod images() works the same as audio(), office(), videos() and other().
*
* The methods will not work correctly on OS Windows, only on MacOs and Linux.
*
* The method first collects the contents of the Images directory using the fs.readdirSync() method and its result is written to the grabImageFolder array.
* The forEach() method takes each element of the grabImageFolder array and writes it into the Result variable, which is complemented by the push() method.
* The path.parse() method takes information about the file (filename, extension and other).
* The Resources interface filters the file information, remains: extension, filename and absolutePath.
*
* @example
* import { Resources } from '../../resources'
* const dataSource = Resources.audio()
* dataProvider(dataSource, function (data: FileStats) {
* it(`${data.filename} uploaded successfuly`, async function () {
* await new Conversations().createNewFileConversation()
* await new ConversationCreator()
* .withFile(data.absolutePath)
* .withTitle(`Title ${faker.random.uuid()}`)
* .createAndWaitForUploadToFinish()
* }, 180000) // 3 mins, upload might be slow.
* })
*/
class TestResources implements IResources {
/**
* Returns information about all files in resources/images folder
* @returns FileStats[]
*
* @example
* [ { extension: '.mp3',
* filename: 'Sample_Audio_MP3_700KB.mp3',
* absolutePath: '/Users/admin/projectFolder/resources/audio/Sample_Audio_MP3_700KB.mp3' },
* { extension: '.ogg',
* filename: 'Sample_Audio_OOG_1MB.ogg',
* absolutePath: '/Users/ofedoryc/GreenL/frontend-e2e/resources/audio/Sample_Audio_OOG_1MB.ogg' } ]
*/
images(): FileStats[] {
let grabImageFolder = fs.readdirSync(resFolderImages)
let result = []
grabImageFolder.forEach(function (grabImageItem) {
let parsed = path.parse(path.resolve(resFolderImages, grabImageItem))
let stats = {
extension: parsed.ext,
filename: parsed.base,
absolutePath: path.resolve(__dirname, `${parsed.dir}/${parsed.base}`)
}
result.push(stats)
})
return result
}
/**
* Returns information about all files in resources/audio folder
* @returns FileStats[]
*/
audio(): FileStats[] {
let grabAudioFolder = fs.readdirSync(resFolderAudio)
let result = []
grabAudioFolder.forEach(function (grabAudioItem) {
let parsed = path.parse(path.resolve(resFolderAudio, grabAudioItem))
let stats = {
extension: parsed.ext,
filename: parsed.base,
absolutePath: path.resolve(__dirname, `${parsed.dir}/${parsed.base}`)
}
result.push(stats)
})
return result
}
/**
* Returns information about all files in resources/office folder
* @returns FileStats[]
*/
office(): FileStats[] {
let grabOfficeFolder = fs.readdirSync(resFolderOffice)
let result = []
grabOfficeFolder.forEach(function (grabOfficeItem) {
let parsed = path.parse(path.resolve(resFolderOffice, grabOfficeItem))
let stats = {
extension: parsed.ext,
filename: parsed.base,
absolutePath: path.resolve(__dirname, `${parsed.dir}/${parsed.base}`)
}
result.push(stats)
})
return result
}
/**
* Returns information about all files in resources/videos folder
* @returns FileStats[]
*/
videos(): FileStats[] {
let grabVideosFolder = fs.readdirSync(resFolderVideos)
let result = []
grabVideosFolder.forEach(function (grabVideosItem) {
let parsed = path.parse(path.resolve(resFolderVideos, grabVideosItem))
let stats = {
extension: parsed.ext,
filename: parsed.base,
absolutePath: path.resolve(__dirname, `${parsed.dir}/${parsed.base}`)
}
result.push(stats)
})
return result
}
/**
* Returns information about all files in resources/other folder
* @returns FileStats[]
*/
other(): FileStats[] {
let grabOtherFolder = fs.readdirSync(resFolderOther)
let result = []
grabOtherFolder.forEach(function (grabOtherItem) {
let parsed = path.parse(path.resolve(resFolderOther, grabOtherItem))
let stats = {
extension: parsed.ext,
filename: parsed.base,
absolutePath: path.resolve(__dirname, `${parsed.dir}/${parsed.base}`)
}
result.push(stats)
})
return result
}
/**
* Returns information about all files in resources folder
* @returns FileStats[]
*/
all(): FileStats[] {
let allArray = []
allArray = allArray.concat(this.images())
allArray = allArray.concat(this.audio())
allArray = allArray.concat(this.office())
allArray = allArray.concat(this.videos())
allArray = allArray.concat(this.other())
return allArray
}
}
export const Resources = new TestResources()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment