Skip to content

Commit

Permalink
unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flotwig committed Aug 29, 2022
1 parent d25b9e0 commit 9b8cfbe
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
4 changes: 3 additions & 1 deletion packages/server/lib/gui/windows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export type WindowOptions = Electron.BrowserWindowConstructorOptions & {
onCrashed?: () => void
}

export type WindowOpenOptions = WindowOptions & { url: string }

type TrackStateMap = Record<'width' | 'height' | 'x' | 'y' | 'devTools', string>

let windows = {}
Expand Down Expand Up @@ -213,7 +215,7 @@ export function create (projectRoot, _options: WindowOptions, newBrowserWindow =
}

// open launchpad BrowserWindow
export async function open (projectRoot: string, options: WindowOptions & { url: string }, newBrowserWindow = _newBrowserWindow): Promise<BrowserWindow> {
export async function open (projectRoot: string, options: WindowOpenOptions, newBrowserWindow = _newBrowserWindow): Promise<BrowserWindow> {
// if we already have a window open based
// on that type then just show + focus it!
const knownWin = options.type && getByType(options.type)
Expand Down
11 changes: 4 additions & 7 deletions packages/server/test/unit/gui/windows_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { EventEmitter } from 'events'
import { BrowserWindow } from 'electron'
import * as Windows from '../../../lib/gui/windows'
import * as savedState from '../../../lib/saved_state'
import { getPathToDesktopIndex } from '@packages/resolve-dist'

const DEFAULT_USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Cypress/0.0.0 Chrome/59.0.3071.115 Electron/1.8.2 Safari/537.36'

Expand Down Expand Up @@ -42,23 +41,21 @@ describe('lib/gui/windows', () => {

context('.open', () => {
it('sets default options', function () {
const options: Windows.WindowOptions = {
const options: Windows.WindowOpenOptions = {
type: 'INDEX',
url: 'foo',
}

return Windows.open('/path/to/project', 1234, options, () => this.win)
return Windows.open('/path/to/project', options, () => this.win)
.then((win) => {
expect(options).to.include({
height: 500,
width: 600,
type: 'INDEX',
show: true,
url: getPathToDesktopIndex(1234),
})

expect(win.loadURL).to.be.calledWith(getPathToDesktopIndex(
1234,
))
expect(win.loadURL).to.be.calledWith('foo')
})
})
})
Expand Down
28 changes: 14 additions & 14 deletions packages/server/test/unit/modes/interactive_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ describe('gui/interactive', () => {
context('.getWindowArgs', () => {
it('quits app when onClose is called', () => {
electron.app.quit = sinon.stub()
interactiveMode.getWindowArgs({}).onClose()
interactiveMode.getWindowArgs(1234, {}).onClose()

expect(electron.app.quit).to.be.called
})

it('tracks state properties', () => {
const { trackState } = interactiveMode.getWindowArgs({})
const { trackState } = interactiveMode.getWindowArgs(1234, {})

const args = _.pick(trackState, 'width', 'height', 'x', 'y', 'devTools')

Expand All @@ -51,49 +51,49 @@ describe('gui/interactive', () => {
// Use the saved value if it's valid
describe('when no dimension', () => {
it('renders with preferred width if no width saved', () => {
expect(interactiveMode.getWindowArgs({}).width).to.equal(1200)
expect(interactiveMode.getWindowArgs(1234, {}).width).to.equal(1200)
})

it('renders with preferred height if no height saved', () => {
expect(interactiveMode.getWindowArgs({}).height).to.equal(800)
expect(interactiveMode.getWindowArgs(1234, {}).height).to.equal(800)
})
})

describe('when saved dimension is too small', () => {
it('uses the preferred width', () => {
expect(interactiveMode.getWindowArgs({ appWidth: 1 }).width).to.equal(1200)
expect(interactiveMode.getWindowArgs(1234, { appWidth: 1 }).width).to.equal(1200)
})

it('uses the preferred height', () => {
expect(interactiveMode.getWindowArgs({ appHeight: 1 }).height).to.equal(800)
expect(interactiveMode.getWindowArgs(1234, { appHeight: 1 }).height).to.equal(800)
})
})

describe('when saved dimension is within min/max dimension', () => {
it('uses the saved width', () => {
expect(interactiveMode.getWindowArgs({ appWidth: 1500 }).width).to.equal(1500)
expect(interactiveMode.getWindowArgs(1234, { appWidth: 1500 }).width).to.equal(1500)
})

it('uses the saved height', () => {
expect(interactiveMode.getWindowArgs({ appHeight: 1500 }).height).to.equal(1500)
expect(interactiveMode.getWindowArgs(1234, { appHeight: 1500 }).height).to.equal(1500)
})
})
})

it('renders with saved x if it exists', () => {
expect(interactiveMode.getWindowArgs({ appX: 3 }).x).to.equal(3)
expect(interactiveMode.getWindowArgs(1234, { appX: 3 }).x).to.equal(3)
})

it('renders with no x if no x saved', () => {
expect(interactiveMode.getWindowArgs({}).x).to.be.undefined
expect(interactiveMode.getWindowArgs(1234, {}).x).to.be.undefined
})

it('renders with saved y if it exists', () => {
expect(interactiveMode.getWindowArgs({ appY: 4 }).y).to.equal(4)
expect(interactiveMode.getWindowArgs(1234, { appY: 4 }).y).to.equal(4)
})

it('renders with no y if no y saved', () => {
expect(interactiveMode.getWindowArgs({}).y).to.be.undefined
expect(interactiveMode.getWindowArgs(1234, {}).y).to.be.undefined
})

describe('on window focus', () => {
Expand All @@ -105,7 +105,7 @@ describe('gui/interactive', () => {
const env = process.env['CYPRESS_INTERNAL_ENV']

process.env['CYPRESS_INTERNAL_ENV'] = 'development'
interactiveMode.getWindowArgs({}).onFocus()
interactiveMode.getWindowArgs(1234, {}).onFocus()
expect(menu.set.lastCall.args[0].withInternalDevTools).to.be.true
process.env['CYPRESS_INTERNAL_ENV'] = env
})
Expand All @@ -114,7 +114,7 @@ describe('gui/interactive', () => {
const env = process.env['CYPRESS_INTERNAL_ENV']

process.env['CYPRESS_INTERNAL_ENV'] = 'production'
interactiveMode.getWindowArgs({}).onFocus()
interactiveMode.getWindowArgs(1234, {}).onFocus()
expect(menu.set.lastCall.args[0].withInternalDevTools).to.be.false
process.env['CYPRESS_INTERNAL_ENV'] = env
})
Expand Down

0 comments on commit 9b8cfbe

Please sign in to comment.