Skip to content

Commit

Permalink
Fix unit tests / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
majakomel committed Oct 20, 2023
1 parent cecfe66 commit 07eed3c
Show file tree
Hide file tree
Showing 30 changed files with 1,972 additions and 2,393 deletions.
1 change: 1 addition & 0 deletions main/windows/aboutWindow.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const aboutWindow = () => {
webPreferences: {
enableRemoteModule: true,
preload: join(__dirname, 'preload.js'),
sandbox: false,
}
})

Expand Down
108 changes: 108 additions & 0 deletions main/windows/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const { ipcRenderer } = require('electron')

module.exports = {
ooni: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooni', subscription)
return () => {
ipcRenderer.removeListener('ooni', subscription)
}
},
config: {
get: (data) => ipcRenderer.invoke('config.get', data),
getFreshConfig: () => ipcRenderer.invoke('get-fresh-config'),
set: (key, currentValue, value) => ipcRenderer.invoke('config.set', key, currentValue, value),
onboard: (crashReportsOptIn) => ipcRenderer.invoke('config.onboard', { crashReportsOptIn }),
categories: () => ipcRenderer.sendSync('config.categories')
},
ooniprobe: {
run: (testGroupToRun, inputFile) => ipcRenderer.send('ooniprobe.run', { testGroupToRun, inputFile }),
stop: () => ipcRenderer.send('ooniprobe.stop'),
runningTest: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.running-test', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.running-test', subscription)
}
},
done: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.done', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.done', subscription)
}
},
completed: (fn) => ipcRenderer.on('ooniprobe.completed', () => fn()),
error: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.error', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.error', subscription)
}
},
},
autorun: {
schedule: () => ipcRenderer.invoke('autorun.schedule'),
remindLater: () => ipcRenderer.send('autorun.remind-later'),
maybeRemind: () => ipcRenderer.send('autorun.maybe-remind'),
cancel: () => ipcRenderer.send('autorun.cancel'),
showPrompt: (fn) => {
const subscription = () => fn()
ipcRenderer.on('autorun.showPrompt', subscription)
return () => {
ipcRenderer.removeAllListeners('autorun.showPrompt', subscription)
}
},
removeShowPromptListeners: () => ipcRenderer.removeAllListeners('autorun.showPrompt'),
status: () => ipcRenderer.invoke('autorun.status'),
disable: () => ipcRenderer.invoke('autorun.disable')
},
about: {
reset: () => ipcRenderer.invoke('reset'),
debugGetAllPaths: () => ipcRenderer.sendSync('debugGetAllPaths'),
updateMessage: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('update-message', subscription)
return () => {
ipcRenderer.removeAllListeners('update-message', subscription)
}
},
updateProgress: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('update-progress', subscription)
return () => {
ipcRenderer.removeAllListeners('update-progress', subscription)
}
}
},
results: {
list: (resultID) => ipcRenderer.invoke('list-results', resultID),
showMeasurement: (msmtID) => ipcRenderer.invoke('show-measurement', msmtID),
last: {
request: (testGroupName) => ipcRenderer.send('results.last.request', { testGroupName }),
response: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('results.last.response', subscription)
return () => {
ipcRenderer.removeAllListeners('results.last.response', subscription)
}
},
},
},
prefs: {
get: (key) => ipcRenderer.sendSync('prefs.get', key),
save: (key, value) => ipcRenderer.sendSync('prefs.save', { key, value })
},
fs: {
write: {
request: (testListString) => ipcRenderer.send('fs.write.request', testListString),
response: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('fs.write.response', subscription)
return () => {
ipcRenderer.removeAllListeners('fs.write.response', subscription)
}
},
}
}
}
1 change: 1 addition & 0 deletions main/windows/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const mainWindow = (url = 'dashboard') => {
webPreferences: {
preload: join(__dirname, 'preload.js'),
enableRemoteModule: true,
sandbox: false,
}
})
win.loadURL(windowURL(url))
Expand Down
111 changes: 4 additions & 107 deletions main/windows/preload.js
Original file line number Diff line number Diff line change
@@ -1,108 +1,5 @@
const { ipcRenderer, contextBridge } = require('electron')
const { contextBridge } = require('electron')
const api = require('./api')

contextBridge.exposeInMainWorld('electron', api)

contextBridge.exposeInMainWorld('electron', {
ooni: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooni', subscription)
return () => {
ipcRenderer.removeListener('ooni', subscription)
}
},
config: {
get: (data) => ipcRenderer.invoke('config.get', data),
getFreshConfig: () => ipcRenderer.invoke('get-fresh-config'),
set: (key, currentValue, value) => ipcRenderer.invoke('config.set', key, currentValue, value),
onboard: (crashReportsOptIn) => ipcRenderer.invoke('config.onboard', { crashReportsOptIn }),
categories: () => ipcRenderer.sendSync('config.categories')
},
ooniprobe: {
run: (testGroupToRun, inputFile) => ipcRenderer.send('ooniprobe.run', { testGroupToRun, inputFile }),
stop: () => ipcRenderer.send('ooniprobe.stop'),
runningTest: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.running-test', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.running-test', subscription)
}
},
done: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.done', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.done', subscription)
}
},
completed: (fn) => ipcRenderer.on('ooniprobe.completed', () => fn()),
error: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('ooniprobe.error', subscription)
return () => {
ipcRenderer.removeAllListeners('ooniprobe.error', subscription)
}
},
},
autorun: {
schedule: () => ipcRenderer.invoke('autorun.schedule'),
remindLater: () => ipcRenderer.send('autorun.remind-later'),
maybeRemind: () => ipcRenderer.send('autorun.maybe-remind'),
cancel: () => ipcRenderer.send('autorun.cancel'),
showPrompt: (fn) => {
const subscription = () => fn()
ipcRenderer.on('autorun.showPrompt', subscription)
return () => {
ipcRenderer.removeAllListeners('autorun.showPrompt', subscription)
}
},
removeShowPromptListeners: () => ipcRenderer.removeAllListeners('autorun.showPrompt'),
status: () => ipcRenderer.invoke('autorun.status'),
disable: () => ipcRenderer.invoke('autorun.disable')
},
about: {
reset: () => ipcRenderer.invoke('reset'),
debugGetAllPaths: () => ipcRenderer.sendSync('debugGetAllPaths'),
updateMessage: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('update-message', subscription)
return () => {
ipcRenderer.removeAllListeners('update-message', subscription)
}
},
updateProgress: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('update-progress', subscription)
return () => {
ipcRenderer.removeAllListeners('update-progress', subscription)
}
}
},
results: {
list: (resultID) => ipcRenderer.invoke('list-results', resultID),
showMeasurement: (msmtID) => ipcRenderer.invoke('show-measurement', msmtID),
last: {
request: (testGroupName) => ipcRenderer.send('results.last.request', { testGroupName }),
response: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('results.last.response', subscription)
return () => {
ipcRenderer.removeAllListeners('results.last.response', subscription)
}
},
},
},
prefs: {
get: (key) => ipcRenderer.sendSync('prefs.get', key),
save: (key, value) => ipcRenderer.sendSync('prefs.save', { key, value })
},
fs: {
write: {
request: (testListString) => ipcRenderer.send('fs.write.request', testListString),
response: (fn) => {
const subscription = (_, args) => fn(args)
ipcRenderer.on('fs.write.response', subscription)
return () => {
ipcRenderer.removeAllListeners('fs.write.response', subscription)
}
},
}
}
})
2 changes: 0 additions & 2 deletions main/windows/windowURL.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
/* global require, module */

const { resolve } = require('app-root-path')
const { format } = require('url')

Expand Down
24 changes: 16 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,21 @@
"afterSign": "scripts/notarize.js"
},
"devDependencies": {
"@babel/core": "^7.23.0",
"@babel/plugin-transform-modules-commonjs": "^7.23.0",
"@electron/notarize": "^2.1.0",
"@next/bundle-analyzer": "^13.4.19",
"@sentry/integrations": "^7.68.0",
"@svgr/webpack": "^8.1.0",
"@testing-library/dom": "^9.3.1",
"@testing-library/dom": "^9.3.3",
"@testing-library/jest-dom": "^6.1.3",
"@testing-library/react": "^14.0.0",
"@zeit/next-source-maps": "^0.0.3",
"babel-jest": "^29.7.0",
"babel-plugin-inline-react-svg": "^2.0.2",
"babel-plugin-styled-components": "^2.1.4",
"cross-env": "^7.0.3",
"csv-parse": "^5.5.0",
"csv-parse": "^5.5.2",
"debug": "^4.3.4",
"dotenv": "^16.3.1",
"electron": "^26.1.0",
Expand All @@ -171,14 +176,17 @@
"hasha": "^5.2.2",
"humanize": "^0.0.9",
"icon-gen": "^4.0.0",
"jest": "^27.5.1",
"jest-canvas-mock": "^2.3.1",
"jest-css-modules-transform": "^4.2.1",
"jest-styled-components": "^7.0.4",
"jest": "^29.7.0",
"jest-canvas-mock": "^2.5.2",
"jest-css-modules-transform": "^4.4.2",
"jest-environment-jsdom": "^29.7.0",
"jest-esm-transformer": "^1.0.0",
"jest-esm-transformer-2": "^1.0.0",
"jest-styled-components": "^7.2.0",
"markdown-to-jsx": "^7.3.2",
"moment-range": "^4.0.2",
"next": "^13.4.19",
"ooni-components": "1.0.0-alpha.17",
"ooni-components": "0.6.0-alpha.1",
"os-locale": "^6.0.2",
"prop-types": "^15.8.1",
"raven-js": "^3.27.2",
Expand All @@ -196,7 +204,7 @@
"react-table": "^7.0.0-rc.15",
"react-test-renderer": "^18.2.0",
"spectron": "^19.0.0",
"styled-components": "^6.0.8",
"styled-components": "^6.1.0",
"stylis": "^4.3.0",
"stylis-plugin-rtl": "^2.1.1",
"use-clipboard-copy": "^0.2.0"
Expand Down
1 change: 0 additions & 1 deletion renderer/components/FullHeightFlex.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React from 'react'
import { Flex } from 'ooni-components'
import styled from 'styled-components'

Expand Down
8 changes: 2 additions & 6 deletions renderer/components/dashboard/__tests__/RunTestCard.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
/**
* @jest-environment jsdom
*/

import React from 'react'
import { screen, render, cleanup, fireEvent } from '@testing-library/react'
import { screen, render, fireEvent } from '@testing-library/react'
import { theme } from 'ooni-components'

import { ThemeProvider } from 'styled-components'
import { IntlProvider } from 'react-intl'
import English from '../../../../lang/en.json'
Expand Down Expand Up @@ -37,7 +34,6 @@ describe('Tests for RunTestCard component', () => {
const router = useRouter()

afterEach(() => {
cleanup()
jest.clearAllMocks()
})

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
// TODO: Can parametrize these tests based on testGroups

/**
* @jest-environment jsdom
*/

import React, { useState, createContext } from 'react'
import PropTypes from 'prop-types'
import { theme } from 'ooni-components'
Expand All @@ -14,13 +10,15 @@ import {
render,
waitFor,
fireEvent,
cleanup,
} from '@testing-library/react'

import English from '../../../../lang/en.json'
import { testGroups } from '../../nettests'
import TestGroupInDetail from '../TestGroupInDetail'
import { initializeConfig } from '../../../../main/utils/config'
import api from '../../../../main/windows/api'

Object.defineProperty(window, 'electron', { value: api })

// For Using Intl formatting outside React Hooks
const cache = createIntlCache()
Expand Down Expand Up @@ -115,7 +113,6 @@ const getEstimatedSizeAndTime = (testGroup) => {
describe('Test if TestGroupInDetail component is correctly rendered', () => {
const runTest = jest.fn()
afterEach(() => {
cleanup()
runTest.mockClear()
})

Expand Down Expand Up @@ -247,7 +244,6 @@ describe('Test if TestGroupInDetail component is correctly rendered', () => {
describe('TestGroupInDetail is correctly rendered for Website test', () => {
const runTest = jest.fn()
afterEach(() => {
cleanup()
runTest.mockClear()
})

Expand Down
Loading

0 comments on commit 07eed3c

Please sign in to comment.