Skip to content

Commit

Permalink
🧪 #8 Test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
c-o-l-i-n committed Sep 20, 2022
1 parent b98ca9b commit c722224
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
40 changes: 32 additions & 8 deletions src/__tests__/DropZone.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import React, { ReactElement } from 'react'
import '@testing-library/jest-dom'
import { fireEvent, render, RenderResult, screen } from '@testing-library/react'
import { DragDropEventOptions, FileType, MessageBoxType } from '../types/types'
import { DragDropEventOptions, FileType, MessageBoxType, TestFile } from '../types/types'
import DropZone from '../renderer/main-window/components/DropZone'
import { mockElectronApi, mockPdfFile, mockTxtFile, noop } from './mocks'
import { mockDirectory, mockElectronApi, mockFileWithNoType, mockPdfFile, mockTxtFile, noop } from './mocks'

const createDragDropEventOptions = (files: File[]): DragDropEventOptions => ({
dataTransfer: {
Expand Down Expand Up @@ -110,11 +110,35 @@ describe('DropZone', () => {
expect(messageBoxSpy.mock.calls[0][0]).toEqual(MessageBoxType.ERROR)
})

it('should show an error message if drop undesired file type', async () => {
const messageBoxSpy = jest.spyOn(window.electron, 'showMessageBox')
fireEvent.drop(window, createDragDropEventOptions([mockTxtFile]))
await new Promise(process.nextTick) // wait for all code to execute
expect(messageBoxSpy).toHaveBeenCalledTimes(1)
expect(messageBoxSpy.mock.calls[0][0]).toEqual(MessageBoxType.ERROR)
const undesiredFiles: Record<string, TestFile> = {
'txt file': {
file: mockTxtFile,
isFolder: false,
fileExtension: '.txt'
},
directory: {
file: mockDirectory,
isFolder: true,
fileExtension: ''
},
'file with no type': {
file: mockFileWithNoType,
isFolder: false,
fileExtension: ''
}
}

Object.entries(undesiredFiles).forEach(([testFileType, testFile]) => {
it(`should show an error message if drop undesired file type (${testFileType})`, async () => {
const messageBoxSpy = jest.spyOn(window.electron, 'showMessageBox')
jest.spyOn(window.electron, 'fileIsDirectory').mockReturnValue(Promise.resolve(testFile.isFolder))
jest.spyOn(window.electron, 'getFileExtension').mockReturnValue(Promise.resolve(testFile.fileExtension))

fireEvent.drop(window, createDragDropEventOptions([testFile.file]))
await new Promise(process.nextTick) // wait for all code to execute

expect(messageBoxSpy).toHaveBeenCalledTimes(1)
expect(messageBoxSpy.mock.calls[0][0]).toEqual(MessageBoxType.ERROR)
})
})
})
2 changes: 2 additions & 0 deletions src/__tests__/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ export const mockTab: Tab = { page: Page.GENERATE, icon: 'mock/icon/path' }

export const mockPdfFile = { ...new File(['Mock PDF File'], 'MockPdfFile.pdf', { type: 'application/pdf' }), path: '/path/to/MockPdfFile.pdf' }
export const mockTxtFile = { ...new File(['Mock TXT File'], 'MockTxtFile.txt', { type: 'text/plain' }), path: '/path/to/MockTxtFile.txt' }
export const mockDirectory = { ...new File(['Mock Directory'], 'MockDirectory', {}), path: '/path/to/MockDirectory' }
export const mockFileWithNoType = { ...new File(['Mock File With No Type'], 'MockFileWithNoTYpe', {}), path: '/path/to/MockFileWithNoTYpe' }
2 changes: 1 addition & 1 deletion src/renderer/main-window/components/DropZone.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ const DropZone = ({ text, desiredFileType, onDrop }: Props): ReactElement => {
? FileType.FOLDER
: fileExtension === ''
? FileType.NO_TYPE
: fileExtension.split('.').pop()?.toUpperCase() ?? FileType.UNEXPECTED
: fileExtension.split('.').pop()?.toUpperCase() ?? FileType.IMPOSSIBLE

if (fileType !== desiredFileType) {
const lettersPrecededByAn = 'AEFHILMNORSX'
Expand Down
8 changes: 7 additions & 1 deletion src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export enum FileType {
PDF = 'PDF',
FOLDER = 'folder',
NO_TYPE = 'file with no type',
UNEXPECTED = '<unexpected file type>'
IMPOSSIBLE = '<impossible file type>'
}

export enum IpcMainMessage {
Expand Down Expand Up @@ -99,3 +99,9 @@ export interface DragDropEventOptions {
clearData: () => void
}
}

export interface TestFile {
file: File
isFolder: boolean
fileExtension: string
}

0 comments on commit c722224

Please sign in to comment.