Skip to content

Commit

Permalink
✅ Add unit tests; close brave/brave-browser#1530
Browse files Browse the repository at this point in the history
  • Loading branch information
imptrx committed Jul 9, 2019
1 parent 6aba9b5 commit 6cb1a92
Show file tree
Hide file tree
Showing 11 changed files with 245 additions and 39 deletions.
2 changes: 1 addition & 1 deletion components/brave_welcome_ui/actions/welcome_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { types } from '../constants/welcome_types'
// APIs
import * as welcomeUtils from '../welcomeUtils'

export const importBrowserProfileRequested = (sourceBrowserProfileIndex: number) => action(types.IMPORT_BROWSER_PROFILE_REQUESTED, sourceBrowserProfileIndex)
export const importBrowserProfileRequested = (sourceBrowserProfileIndex: number) => action(types.IMPORT_BROWSER_DATA_REQUESTED, sourceBrowserProfileIndex)

export const goToTabRequested = (url: string, target: string) => action(types.GO_TO_TAB_REQUESTED, {
url,
Expand Down
20 changes: 1 addition & 19 deletions components/brave_welcome_ui/components/screens/importBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { WelcomeImportImage } from 'brave-ui/features/welcome/images'
// Utils
import { getLocale } from '../../../common/locale'

interface Props {
export interface Props {
index: number
currentScreen: number
browserProfiles: Array<Welcome.BrowserProfile>
Expand Down Expand Up @@ -47,26 +47,8 @@ export default class ImportBox extends React.PureComponent<Props, State> {
}
}

// class ImportDataBrowserProxyImpl {
// /** @override */
// initializeImportDialog() {
// return cr.sendWithPromise('initializeImportDialog');
// }

// /** @override */
// importData(sourceBrowserProfileIndex) {
// chrome.send('importData', [sourceBrowserProfileIndex]);
// }

// /** @override */
// importFromBookmarksFile() {
// chrome.send('importFromBookmarksFile');
// }
// }

onHandleImport = () => {
const { onClick } = this.props

let sourceBrowserProfileIndex: number
sourceBrowserProfileIndex = this.state && this.state.selectedBrowserProfile && this.state.selectedBrowserProfile.index || 0
onClick(sourceBrowserProfileIndex)
Expand Down
2 changes: 1 addition & 1 deletion components/brave_welcome_ui/constants/welcome_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

export const enum types {
IMPORT_BROWSER_PROFILE_REQUESTED = '@@welcome/IMPORT_BROWSER_PROFILE_REQUESTED',
IMPORT_BROWSER_DATA_REQUESTED = '@@welcome/IMPORT_BROWSER_DATA_REQUESTED',
GO_TO_TAB_REQUESTED = '@@welcome/GO_TO_TAB_REQUESTED',
CLOSE_TAB_REQUESTED = '@@welcome/CLOSE_TAB_REQUESTED',
CHANGE_DEFAULT_SEARCH_PROVIDER = '@@welcome/CHANGE_DEFAULT_SEARCH',
Expand Down
2 changes: 1 addition & 1 deletion components/brave_welcome_ui/reducers/welcome_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const welcomeReducer: Reducer<Welcome.State | undefined> = (state: Welcome.State
const payload = action.payload
const startingState = state
switch (action.type) {
case types.IMPORT_BROWSER_PROFILE_REQUESTED:
case types.IMPORT_BROWSER_DATA_REQUESTED:
chrome.send('importData', [payload])
break
case types.GO_TO_TAB_REQUESTED:
Expand Down
12 changes: 11 additions & 1 deletion components/brave_welcome_ui/welcomeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@ export const getSearchEngineProviders = () => {
}
}

export const getValidBrowserProfiles = (browserProfiles: Array<Welcome.BrowserProfile>): Array<Welcome.BrowserProfile> => {
const result = browserProfiles.reduce((filteredProfiles, profile) =>
(profile.name === 'Safari' || profile.name === 'Bookmarks HTML File')
? filteredProfiles
: [...filteredProfiles, profile]
, [])
return result
}

export const getBrowserProfiles = () => {
return (dispatch: Dispatch) => {
window.cr.sendWithPromise('initializeImportDialog')
.then((response: Array<Welcome.BrowserProfile>) => {
dispatch(getBrowserProfilesSuccess(response))
const filteredProfiles = getValidBrowserProfiles(response)
dispatch(getBrowserProfilesSuccess(filteredProfiles))
})
}
}
17 changes: 13 additions & 4 deletions components/test/brave_welcome_ui/actions/welcome_actions_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@

import { types } from '../../../brave_welcome_ui/constants/welcome_types'
import * as actions from '../../../brave_welcome_ui/actions/welcome_actions'
import { mockImportSources } from '../../testData'

describe('welcome_actions', () => {
it('importNowRequested', () => {
expect(actions.importNowRequested()).toEqual({
type: types.IMPORT_NOW_REQUESTED,
it('getBrowserProfilesSuccess', () => {
expect(actions.getBrowserProfilesSuccess(mockImportSources)).toEqual({
type: types.IMPORT_BROWSER_PROFILES_SUCCESS,
meta: undefined,
payload: undefined
payload: mockImportSources
})
})

it('importBrowserProfileRequested', () => {
expect(actions.importBrowserProfileRequested(0)).toEqual({
type: types.IMPORT_BROWSER_DATA_REQUESTED,
meta: undefined,
payload: 0
})
})

Expand Down
3 changes: 2 additions & 1 deletion components/test/brave_welcome_ui/components/app_test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('welcomePage component', () => {
it('should map the default state', () => {
expect(mapStateToProps(welcomeInitialState)).toEqual({
welcomeData: {
searchProviders: []
searchProviders: [],
browserProfiles: []
}
})
})
Expand Down
93 changes: 93 additions & 0 deletions components/test/brave_welcome_ui/components/importBox_test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import * as React from 'react'
import { shallow } from 'enzyme'
import { Content, PrimaryButton } from 'brave-ui/features/welcome'
import { WelcomeImportImage } from 'brave-ui/features/welcome/images'
import ImportBox, { Props } from '../../../../components/brave_welcome_ui/components/screens/importBox'
import { mockImportSources } from '../../testData'

describe('ImportBox component tests', () => {

const mockProps: Props = {
index: 2,
currentScreen: 2,
onClick: () => null,
browserProfiles: mockImportSources
}

describe('ImportBox render tests', () => {
it('renders the component DOM without crashing', () => {
const wrapper = shallow(
<ImportBox
index={mockProps.index}
currentScreen={mockProps.currentScreen}
onClick={mockProps.onClick}
browserProfiles={mockProps.browserProfiles}
/>)

expect(wrapper.find(Content)).toHaveLength(1)
expect(wrapper.contains(<WelcomeImportImage />))
})
})

describe('ImportBox interaction tests', () => {
it('should call the import API on button click', () => {
const mockAction = jest.fn()
const wrapper = shallow(
<ImportBox
index={mockProps.index}
currentScreen={mockProps.currentScreen}
onClick={mockAction}
browserProfiles={mockProps.browserProfiles}
/>)
const button = wrapper.find(PrimaryButton)
expect(button).toHaveLength(1)
button.simulate('click')
expect(mockAction.mock.calls.length).toBe(1)
})
})

describe('onChangeImportSource', () => {
it('should set selectedBrowserProfile to null if non-valid option is selected', () => {
const mockEvent = {
target: {
value: ''
}
}
const wrapper = shallow(
<ImportBox
index={mockProps.index}
currentScreen={mockProps.currentScreen}
onClick={mockProps.onClick}
browserProfiles={mockProps.browserProfiles}
/>)

const expected = null
wrapper.instance().onChangeImportSource(mockEvent)
expect(wrapper.state().selectedBrowserProfile).toEqual(expected)
})

it('should set selectedBrowserProfile to profile if valid option selected', () => {
const mockEvent = {
target: {
value: '1'
}
}
const wrapper = shallow(
<ImportBox
index={mockProps.index}
currentScreen={mockProps.currentScreen}
onClick={mockProps.onClick}
browserProfiles={mockImportSources}
/>)

// Chrome profile which has browser index of 1
const expected = mockImportSources[0]
wrapper.instance().onChangeImportSource(mockEvent)
expect(wrapper.state().selectedBrowserProfile).toEqual(expected)
})
})
})
73 changes: 62 additions & 11 deletions components/test/brave_welcome_ui/reducers/welcome_reducer_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,52 @@

import welcomeReducer from '../../../brave_welcome_ui/reducers/welcome_reducer'
import * as actions from '../../../brave_welcome_ui/actions/welcome_actions'
import * as storage from '../../../brave_welcome_ui/storage'
import { types } from '../../../brave_welcome_ui/constants/welcome_types'
import { mockSearchProviders, mockImportSources } from '../../testData'

window.open = jest.fn()
window.close = jest.fn()

describe('welcomeReducer', () => {
it('should handle initial state', () => {
const assertion = welcomeReducer(undefined, actions.closeTabRequested())
expect(assertion).toEqual({
searchProviders: []
describe('Handlle initial state', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(storage, 'load')
})
afterEach(() => {
spy.mockRestore()
})
it('calls storage.load() when initial state is undefined', () => {
const assertion = welcomeReducer(undefined, actions.closeTabRequested())
expect(assertion).toEqual({
searchProviders: [],
browserProfiles: []
})
expect(spy).toBeCalled()
expect(spy.mock.calls[0][1]).toBe(undefined)
})
})

describe('IMPORT_NOW_REQUESTED', () => {
let importNowRequestStub: jest.SpyInstance
describe('IMPORT_BROWSER_DATA_REQUESTED', () => {
let importBrowserProfileRequestStub: jest.SpyInstance

beforeEach(() => {
importNowRequestStub = jest.spyOn(chrome, 'send')
importBrowserProfileRequestStub = jest.spyOn(chrome, 'send')
})

afterEach(() => {
importNowRequestStub.mockRestore()
importBrowserProfileRequestStub.mockRestore()
})

it('should call chrome.send with the correct arguments', () => {
welcomeReducer(undefined, {
type: types.IMPORT_NOW_REQUESTED
type: types.IMPORT_BROWSER_DATA_REQUESTED,
payload: 0
})

expect(importNowRequestStub).toBeCalledTimes(1)
expect(importNowRequestStub).toBeCalledWith('importNowRequested', [])
expect(importBrowserProfileRequestStub).toBeCalledTimes(1)
expect(importBrowserProfileRequestStub).toBeCalledWith('importData', [0])
})
})

Expand Down Expand Up @@ -88,4 +103,40 @@ describe('welcomeReducer', () => {
expect(changeSearchProviderStub).toBeCalledWith('setDefaultSearchEngine', [12345])
})
})

describe('IMPORT_DEFAULT_SEARCH_PROVIDERS_SUCCESS', () => {
it('should set default search provider data', () => {
const mockState = {
searchProviders: [],
browserProfiles: []
}
const result = welcomeReducer(mockState, {
type: types.IMPORT_DEFAULT_SEARCH_PROVIDERS_SUCCESS,
payload: mockSearchProviders
})
const expected = {
...mockState,
searchProviders: mockSearchProviders
}
expect(result).toEqual(expected)
})
})

describe('IMPORT_BROWSER_PROFILES_SUCCESS', () => {
it('should set import browser profile data', () => {
const mockState = {
searchProviders: [],
browserProfiles: []
}
const result = welcomeReducer(mockState, {
type: types.IMPORT_BROWSER_PROFILES_SUCCESS,
payload: mockImportSources
})
const expected = {
...mockState,
browserProfiles: mockImportSources
}
expect(result).toEqual(expected)
})
})
})
18 changes: 18 additions & 0 deletions components/test/brave_welcome_ui/utils/welcomeUtils_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import { getValidBrowserProfiles } from '../../../brave_welcome_ui/welcomeUtils'
import { mockImportSources } from '../../testData'

describe('welcome utils tests', () => {
describe('getfilterBrowserProfiles tests', () => {
it('should filter out profiles with "safari" and "Bookmarks HTML File" as names', () => {
const result = getValidBrowserProfiles(mockImportSources)
// Chrome browser profile object
const expected = [mockImportSources[0]]
expect(result).toEqual(expected)
})
})
})
42 changes: 42 additions & 0 deletions components/test/testData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,45 @@ export const mockSearchProviders = [
urlLocked: true
}
]

export const mockImportSources = [
{
autofillFormData : false,
cookies : true,
favorites : true,
history : true,
index : 1,
ledger : false,
name : `Chrome Person 1`,
passwords : true,
search : false,
stats : false,
windows : false
},
{
autofillFormData : false,
cookies : true,
favorites : true,
history : true,
index : 0,
ledger : false,
name : `Safari`,
passwords : true,
search : false,
stats : false,
windows : false
},
{
autofillFormData : false,
cookies : true,
favorites : true,
history : true,
index : 2,
ledger : false,
name : `Bookmarks HTML File`,
passwords : true,
search : false,
stats : false,
windows : false
}
]

0 comments on commit 6cb1a92

Please sign in to comment.