Skip to content

Commit

Permalink
monitor test
Browse files Browse the repository at this point in the history
  • Loading branch information
nbonamy committed May 17, 2024
1 parent 0f4aebb commit f8d1317
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/main/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import fs from 'fs'

export default class {

delay: number
filepath: string
filesize: number
signal: string
timer: NodeJS.Timeout

constructor(signal: string) {
constructor(signal: string, delay = 1000) {
this.signal = signal
this.delay = delay
}

start(filepath: string): void {
Expand All @@ -29,7 +31,7 @@ export default class {
this.filesize = size
this.notify()
}
}, 1000)
}, this.delay)
}

stop(): void {
Expand All @@ -43,6 +45,7 @@ export default class {
try {
return fs.statSync(this.filepath).size
} catch (error) {
//console.error('Error while getting file size', error)
return 0
}
}
Expand Down
75 changes: 75 additions & 0 deletions tests/unit/monitor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@

import { vi, expect, test, beforeEach, afterEach, beforeAll, afterAll } from 'vitest'
import { BrowserWindow } from 'electron'
import Monitor from '../../src/main/monitor'
import { wait } from '../../src/main/utils'
import path from 'path'
import fs from 'fs'
import os from 'os'

vi.mock('electron', async () => {
const BrowserWindow = vi.fn()
BrowserWindow.prototype.isDestroyed = vi.fn(function() { return false })
BrowserWindow.getAllWindows = vi.fn(() => [ new BrowserWindow(), new BrowserWindow(), new Monitor('test') ])
BrowserWindow.prototype.webContents = {
send: vi.fn(),
}
return {
BrowserWindow,
}
})

let monitor: Monitor = null
const tempFile = path.join(os.tmpdir(), 'vitest')

beforeAll(async () => {
fs.writeFileSync(tempFile, 'Hello')
})

afterAll(async () => {
try {
fs.unlinkSync(tempFile)
} catch (error) {
console.error(error)
}
})

beforeEach(async () => {
vi.clearAllMocks()
})

afterEach(async () => {
monitor.stop()
})

test('Start monitor', async () => {
monitor = new Monitor('test')
monitor.start(tempFile)
expect(monitor.size()).toBe(5)
})

test('Stop monitor', async () => {
monitor = new Monitor('test')
monitor.start(tempFile)
monitor.stop()
expect(monitor.size()).toBe(0)
})

test('Notify monitor', async () => {
monitor = new Monitor('test', 50)
monitor.start(tempFile)
fs.appendFileSync(tempFile, ' World')
await wait(150)
expect(BrowserWindow.getAllWindows).toHaveBeenCalledTimes(1)
expect(BrowserWindow.prototype.webContents.send).toHaveBeenCalledTimes(2)
})

test('Notify after stop', async () => {
monitor = new Monitor('test', 500)
monitor.start(tempFile)
monitor.stop()
fs.appendFileSync(tempFile, ' World')
await wait(150)
expect(BrowserWindow.getAllWindows).toHaveBeenCalledTimes(0)
expect(BrowserWindow.prototype.webContents.send).toHaveBeenCalledTimes(0)
})

0 comments on commit f8d1317

Please sign in to comment.