Skip to content

Commit

Permalink
Added spec for native app menu
Browse files Browse the repository at this point in the history
  • Loading branch information
mappum committed Jul 2, 2018
1 parent aecb48d commit b9aeb17
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 0 deletions.
56 changes: 56 additions & 0 deletions test/unit/specs/__snapshots__/menu.spec.js.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`App Menu should create the app menu 1`] = `
Array [
Array [
Array [
Object {
"label": "Cosmos Voyager",
"submenu": Array [
Object {
"click": [Function],
"label": "About Cosmos Voyager",
"selector": "orderFrontStandardAboutPanel:",
},
Object {
"type": "separator",
},
Object {
"accelerator": "Command+Q",
"click": [Function],
"label": "Quit",
},
],
},
Object {
"label": "Edit",
"submenu": Array [
Object {
"accelerator": "CmdOrCtrl+X",
"label": "Cut",
"selector": "cut:",
},
Object {
"accelerator": "CmdOrCtrl+C",
"label": "Copy",
"selector": "copy:",
},
Object {
"accelerator": "CmdOrCtrl+V",
"label": "Paste",
"selector": "paste:",
},
],
},
],
],
]
`;

exports[`App Menu should create the app menu 2`] = `
Array [
Array [
undefined,
],
]
`;
74 changes: 74 additions & 0 deletions test/unit/specs/menu.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
const { join } = require("path")

let mainWindow
describe("App Menu", () => {
beforeEach(() => {
jest.resetModules()
jest.unmock("electron")

mainWindow = {
webContents: { send: jest.fn() }
}

jest.mock("electron", () => ({
Menu: {
buildFromTemplate: jest.fn(menu => {
menu
}),
setApplicationMenu: jest.fn()
},
app: {
quit: jest.fn()
}
}))

let createMenu = require("../../../app/src/main/menu.js")

createMenu(mainWindow)
})

it("should create the app menu", function() {
expect(
require("electron").Menu.buildFromTemplate.mock.calls
).toMatchSnapshot()
expect(
require("electron").Menu.setApplicationMenu.mock.calls
).toMatchSnapshot()
})

it("should quit app when clicking Quit menu item", function() {
let menu = require("electron").Menu.buildFromTemplate.mock.calls[0][0]
let quitItem = menu
.find(({ label }) => label === "Cosmos Voyager")
.submenu.find(({ label }) => label === "Quit")

expect(require("electron").app.quit.mock.calls.length).toBe(0)
quitItem.click()
expect(require("electron").app.quit.mock.calls.length).toBe(1)
})

it("should send 'open-about-menu' when clicking 'About'", function() {
process.platform = "linux"
let menu = require("electron").Menu.buildFromTemplate.mock.calls[0][0]
let aboutItem = menu
.find(({ label }) => label === "Cosmos Voyager")
.submenu.find(({ label }) => label === "About Cosmos Voyager")

expect(mainWindow.webContents.send.mock.calls.length).toBe(0)
aboutItem.click()
expect(mainWindow.webContents.send.mock.calls.length).toBe(1)
expect(mainWindow.webContents.send.mock.calls[0][0]).toBe("open-about-menu")
})

it("should not send 'open-about-menu' when clicking 'About' on mac", function() {
process.platform = "darwin"
let menu = require("electron").Menu.buildFromTemplate.mock.calls[0][0]
let aboutItem = menu
.find(({ label }) => label === "Cosmos Voyager")
.submenu.find(({ label }) => label === "About Cosmos Voyager")

expect(mainWindow.webContents.send.mock.calls.length).toBe(0)
aboutItem.click()
expect(mainWindow.webContents.send.mock.calls.length).toBe(0)
})
})

0 comments on commit b9aeb17

Please sign in to comment.