Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 10, 2024
1 parent 38f5ee5 commit 1d08233
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ export const findProgram = (app: App, program: string) => {
return null;
}

export const writeFileContents = (app: App, payload: anyDict) => {
export const writeFileContents = (app: App, payload: anyDict): string => {

// parse properties
const properties = payload.properties ? { ...payload.properties } : {};
Expand Down
69 changes: 69 additions & 0 deletions tests/unit/file.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@

import { vi, expect, test } from 'vitest'
import * as file from '../../src/main/file'
import { app } from 'electron'
import path from 'path'
import fs from 'fs'
import os from 'os'

vi.mock('electron', async() => {
return {
app: {
getPath: () => os.tmpdir()
},
}
})

test('Find program', async () => {
expect(file.findProgram(null, 'sh')).toBe('/bin/sh')
expect(file.findProgram(null, 'sh2')).toBeNull()
})

test('Get file contents', async () => {
const contents = file.getFileContents(app, './tests/fixtures/sample.txt')
const text = Buffer.from(contents.contents, 'base64').toString()
expect(contents).toStrictEqual({
url: 'file://./tests/fixtures/sample.txt',
contents: 'SGVsbG8gZnJvbSBURVhU',
})
expect(text).toContain('Hello from TEXT')
})

test('Delete file', async () => {
const tempFile = path.join(os.tmpdir(), 'vitest')
fs.writeFileSync(tempFile, 'Hello')
expect(fs.existsSync(tempFile)).toBeTruthy()
file.deleteFile(null, tempFile)
expect(fs.existsSync(tempFile)).toBeFalsy()
})

test('Write file contents', async () => {
const tempFile = path.join(os.tmpdir(), 'vitest')
const fileURL = file.writeFileContents(app, {
contents: 'SGVsbG8gZnJvbSBURVhU',
properties: {
filename: 'vitest'
}
})
expect(fileURL).toBe(tempFile)
expect(fs.existsSync(tempFile)).toBeTruthy()
expect(fs.readFileSync(tempFile, 'utf8')).toBe('Hello from TEXT')
file.deleteFile(null, `file://${tempFile}`)
expect(fs.existsSync(tempFile)).toBeFalsy()
})

test('Download local file', async () => {
const tempFile = path.join(os.tmpdir(), 'vitest')
const fileURL = await file.downloadFile(app, {
url: `file://./tests/fixtures/sample.txt`,
properties: {
filename: 'vitest',
prompt: false,
}
})
expect(fileURL).toBe(tempFile)
expect(fs.existsSync(tempFile)).toBeTruthy()
expect(fs.readFileSync(tempFile, 'utf8')).toBe('Hello from TEXT')
file.deleteFile(null, `file://${tempFile}`)
expect(fs.existsSync(tempFile)).toBeFalsy()
})
11 changes: 11 additions & 0 deletions tests/unit/mimetype.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

import { expect, test } from 'vitest'
import { mimeTypeToExtension } from '../../src/main/mimetype'

test('MIME type to extension', async () => {
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.wordprocessingml.document')).toBe('docx')
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.presentationml.presentation')).toBe('pptx')
expect(mimeTypeToExtension('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')).toBe('xlsx')
expect(mimeTypeToExtension('text/plain')).toBe('txt')
expect(mimeTypeToExtension('application/pdf')).toBe('pdf')
})
2 changes: 1 addition & 1 deletion tests/unit/text.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { getTextContent } from '../../src/main/text'
import { vi, expect, test } from 'vitest'
import { getTextContent } from '../../src/main/text'
import fs from 'fs'

vi.mock('electron', async() => {
Expand Down

0 comments on commit 1d08233

Please sign in to comment.