From 4ced9dddc8c9e3ce420330f42994de3a5869ed58 Mon Sep 17 00:00:00 2001 From: Brian Clifton Date: Tue, 29 Aug 2017 00:45:53 -0700 Subject: [PATCH] Added unit tests, fixed lint error Auditors: @petemill Test Plan: `npm run unittest -- --grep="window API unit tests"` --- app/browser/windows.js | 14 ++- test/unit/app/browser/windowsTest.js | 124 +++++++++++++++++++++++++++ 2 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 test/unit/app/browser/windowsTest.js diff --git a/app/browser/windows.js b/app/browser/windows.js index a742ba9d526..5feeb046460 100644 --- a/app/browser/windows.js +++ b/app/browser/windows.js @@ -16,7 +16,6 @@ const messages = require('../../js/constants/messages') const settings = require('../../js/constants/settings') const windowState = require('../common/state/windowState') const pinnedSitesState = require('../common/state/pinnedSitesState') -const pinnedSitesUtil = require('../common/lib/pinnedSitesUtil') const windowActions = require('../../js/actions/windowActions') // TODO(bridiver) - set window uuid @@ -332,6 +331,19 @@ const api = { } return windowState.WINDOW_ID_NONE + }, + + privateMethods: () => { + return process.env.NODE_ENV === 'test' + ? { + cleanupWindow, + getWindowState, + getWindowValue, + updateWindow, + siteMatchesTab, + updatePinnedTabs + } + : {} } } diff --git a/test/unit/app/browser/windowsTest.js b/test/unit/app/browser/windowsTest.js new file mode 100644 index 00000000000..1be566d8764 --- /dev/null +++ b/test/unit/app/browser/windowsTest.js @@ -0,0 +1,124 @@ +/* global describe, it, before, beforeEach, after, afterEach */ +const mockery = require('mockery') +const sinon = require('sinon') +const Immutable = require('immutable') +const assert = require('assert') +const fakeElectron = require('../../lib/fakeElectron') +const fakeAdBlock = require('../../lib/fakeAdBlock') + +require('../../braveUnit') + +describe('window API unit tests', function () { + let windows, appActions + let appStore + let defaultState, createTabState, tabCloseState + + before(function () { + mockery.enable({ + warnOnReplace: false, + warnOnUnregistered: false, + useCleanCache: true + }) + + const tab1 = { + id: 1, + url: 'https://pinned-tab.com', + partitionNumber: 0, + windowId: 1, + pinned: true + } + const pinned1 = { + 'https://pinned-tab.com|0|0': { + location: 'https://pinned-tab.com', + partitionNumber: 0, + title: 'Brave Software - Example Pinned Tab', + order: 1 + } + } + const pinned2 = { + 'https://another-pinned-tab.com|0|0': { + location: 'https://another-pinned-tab.com', + partitionNumber: pinned1.partitionNumber, + title: pinned1.title, + order: pinned1.order + } + } + + defaultState = Immutable.fromJS({ + pinnedSites: pinned1, + tabs: [tab1] + }) + + createTabState = Immutable.fromJS({ + pinnedSites: pinned2, + tabs: [tab1] + }) + + tabCloseState = Immutable.fromJS({ + pinnedSites: {}, + tabs: [tab1] + }) + + appStore = { + getState: () => Immutable.fromJS(defaultState) + } + + mockery.registerMock('electron', fakeElectron) + mockery.registerMock('ad-block', fakeAdBlock) + mockery.registerMock('../../js/stores/appStore', appStore) + + windows = require('../../../../app/browser/windows') + appActions = require('../../../../js/actions/appActions') + }) + + after(function () { + mockery.disable() + }) + + describe('privateMethods', function () { + let updatePinnedTabs + let createTabRequestedSpy, tabCloseRequestedSpy + const win = { + id: 1, + webContents: { + browserWindowOptions: { + disposition: '' + } + } + } + + before(function () { + const methods = windows.privateMethods() + updatePinnedTabs = methods.updatePinnedTabs + }) + beforeEach(function () { + createTabRequestedSpy = sinon.spy(appActions, 'createTabRequested') + tabCloseRequestedSpy = sinon.spy(appActions, 'tabCloseRequested') + }) + afterEach(function () { + createTabRequestedSpy.restore() + tabCloseRequestedSpy.restore() + appStore.getState = () => Immutable.fromJS(defaultState) + }) + + describe('updatePinnedTabs', function () { + it('takes no action if pinnedSites list matches tab state', function () { + updatePinnedTabs(win) + assert.equal(createTabRequestedSpy.calledOnce, false) + assert.equal(tabCloseRequestedSpy.calledOnce, false) + }) + + it('calls `appActions.createTabRequested` for pinnedSites not in tab state', function () { + appStore.getState = () => Immutable.fromJS(createTabState) + updatePinnedTabs(win) + assert.equal(createTabRequestedSpy.calledOnce, true) + }) + + it('calls `appActions.tabCloseRequested` for items in tab state but not in pinnedSites', function () { + appStore.getState = () => Immutable.fromJS(tabCloseState) + updatePinnedTabs(win) + assert.equal(tabCloseRequestedSpy.calledOnce, true) + }) + }) + }) +})