NTP: Update existing tests after topSites refactoring

This commit is contained in:
Cezar Augusto
2020-03-19 13:01:21 -03:00
parent c8ed82c3f2
commit 2e60795cc9
6 changed files with 54 additions and 505 deletions
@@ -1,164 +0,0 @@
/* 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 { types } from '../../../brave_new_tab_ui/constants/new_tab_types'
import { Stats } from '../../../brave_new_tab_ui/api/stats'
import { Preferences } from '../../../brave_new_tab_ui/api/preferences'
import * as actions from '../../../brave_new_tab_ui/actions/new_tab_actions'
describe('newTabActions', () => {
// TODO(petemill): We possibly don't need a test for every action to
// just to check that the actions are passing their payloads correctly.
// These aren't valid tests to make sure the reducer expects what it gets
// since we can change the payload signature here and in the actions,
// and still get an error in the reducer. It's perhaps more useful to get
// a build time error by using Typescript types in the reducer for each
// action payload.
// https://redux.js.org/recipes/usage-with-typescript
it('bookmarkAdded', () => {
const url: string = 'https://brave.com'
expect(actions.bookmarkAdded(url)).toEqual({
meta: undefined,
type: types.BOOKMARK_ADDED,
payload: { url }
})
})
it('bookmarkRemoved', () => {
const url: string = 'https://brave.com'
expect(actions.bookmarkRemoved(url)).toEqual({
meta: undefined,
type: types.BOOKMARK_REMOVED,
payload: { url }
})
})
it('sitePinned', () => {
const url: string = 'https://brave.com'
expect(actions.sitePinned(url)).toEqual({
meta: undefined,
type: types.NEW_TAB_SITE_PINNED,
payload: { url }
})
})
it('siteUnpinned', () => {
const url: string = 'https://brave.com'
expect(actions.siteUnpinned(url)).toEqual({
meta: undefined,
type: types.NEW_TAB_SITE_UNPINNED,
payload: { url }
})
})
it('siteIgnored', () => {
const url: string = 'https://brave.com'
expect(actions.siteIgnored(url)).toEqual({
meta: undefined,
type: types.NEW_TAB_SITE_IGNORED,
payload: { url }
})
})
it('undoSiteIgnored', () => {
const url: string = 'https://brave.com'
expect(actions.undoSiteIgnored(url)).toEqual({
meta: undefined,
type: types.NEW_TAB_UNDO_SITE_IGNORED,
payload: { url }
})
})
it('undoAllSiteIgnored', () => {
const url: string = 'https://brave.com'
expect(actions.undoAllSiteIgnored(url)).toEqual({
meta: undefined,
type: types.NEW_TAB_UNDO_ALL_SITE_IGNORED,
payload: { url }
})
})
it('siteDragged', () => {
const fromUrl: string = 'https://brave.com'
const toUrl: string = 'https://wikipedia.org'
const dragRight: boolean = true
expect(actions.siteDragged(fromUrl, toUrl, dragRight)).toEqual({
meta: undefined,
type: types.NEW_TAB_SITE_DRAGGED,
payload: { fromUrl, toUrl, dragRight }
})
})
it('siteDragEnd', () => {
const url: string = 'https://brave.com'
const didDrop: boolean = false
expect(actions.siteDragEnd(url, didDrop)).toEqual({
meta: undefined,
type: types.NEW_TAB_SITE_DRAG_END,
payload: { url, didDrop }
})
})
it('onHideSiteRemovalNotification', () => {
expect(actions.onHideSiteRemovalNotification()).toEqual({
meta: undefined,
type: types.NEW_TAB_HIDE_SITE_REMOVAL_NOTIFICATION
})
})
it('bookmarkInfoAvailable', () => {
const queryUrl: string = 'https://brave.com'
const bookmarkTreeNode = {
dateAdded: 1557899510259,
id: '7',
index: 0,
parentId: '2',
title: 'Secure, Fast & Private Web Browser with Adblocker | Brave Browser',
url: 'http://brave.com/'
}
expect(actions.bookmarkInfoAvailable(queryUrl, bookmarkTreeNode)).toEqual({
meta: undefined,
type: types.NEW_TAB_BOOKMARK_INFO_AVAILABLE,
payload: { queryUrl, bookmarkTreeNode }
})
})
it('gridSitesUpdated', () => {
const gridSites: Array<NewTab.Sites> = [
{
bookmarked: undefined,
favicon: 'chrome://favicon/size/64@1x/http://brave.com/',
index: 0,
letter: 'B',
pinned: true,
thumb: 'chrome://thumb/http://brave.com/',
title: 'Secure, Fast & Private Web Browser with Adblocker | Brave Browser',
url: 'http://brave.com/'
}
]
expect(actions.gridSitesUpdated(gridSites)).toEqual({
meta: undefined,
type: types.NEW_TAB_GRID_SITES_UPDATED,
payload: { gridSites }
})
})
it('statsUpdated', () => {
const stats: Stats = {
adsBlockedStat: 1,
fingerprintingBlockedStat: 2,
httpsUpgradesStat: 3,
javascriptBlockedStat: 4,
trackersBlockedStat: 5
}
expect(actions.statsUpdated(stats)).toEqual({
meta: undefined,
payload: {
stats
},
type: types.NEW_TAB_STATS_UPDATED
})
})
it('preferencesUpdated', () => {
const preferences: Preferences = {
showBackgroundImage: false,
showStats: false,
showClock: false,
showTopSites: false
}
expect(actions.preferencesUpdated(preferences)).toEqual({
meta: undefined,
type: types.NEW_TAB_PREFERENCES_UPDATED,
payload: preferences
})
})
})
@@ -4,19 +4,21 @@
import getActions from '../../../brave_new_tab_ui/api/getActions'
import { getTopSites } from '../../../brave_new_tab_ui/api/topSites'
import * as actions from '../../../brave_new_tab_ui/actions/new_tab_actions'
import { types } from '../../../brave_new_tab_ui/constants/new_tab_types'
import * as newTabActions from '../../../brave_new_tab_ui/actions/new_tab_actions'
import * as topSitesActions from '../../../brave_new_tab_ui/actions/grid_sites_actions'
import { types as topSitesTypes } from '../../../brave_new_tab_ui/constants/grid_sites_types'
describe('new tab data api tests', () => {
describe('getActions', () => {
it('returns an object with the same keys mimicking the original new tab actions', () => {
const assertion = getActions()
const actions = Object.assign({}, newTabActions, topSitesActions)
expect(Object.keys(assertion)).toEqual(Object.keys(actions))
})
it('can call an action from getActions', () => {
expect(getActions().onHideSiteRemovalNotification()).toEqual({
payload: undefined,
type: types.NEW_TAB_HIDE_SITE_REMOVAL_NOTIFICATION
expect(getActions().showGridSiteRemovedNotification(true)).toEqual({
payload: { shouldShow: true },
type: topSitesTypes.GRID_SITES_SHOW_SITE_REMOVED_NOTIFICATION
})
})
})
@@ -1,52 +0,0 @@
/* 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 bookmarksAPI from '../../../../brave_new_tab_ui/api/topSites/bookmarks'
const state = {
backgroundImage: {},
bookmarks: {},
gridSites: [],
ignoredTopSites: [],
isIncognito: false,
isQwant: false,
isTor: false,
pinnedTopSites: [],
showEmptyPage: false,
showSiteRemovalNotification: false,
stats: {},
topSites: [],
useAlternativePrivateSearchEngine: false
}
describe('new tab bookmarks api tests', () => {
describe('fetchBookmarkInfo', () => {
let spy: jest.SpyInstance
const url = 'https://brave.com'
beforeEach(() => {
spy = jest.spyOn(chrome.bookmarks, 'search')
})
afterEach(() => {
spy.mockRestore()
})
it('calls chrome.bookmarks.search', () => {
bookmarksAPI.fetchBookmarkInfo(url)
expect(spy).toBeCalled()
})
})
describe('updateBookmarkInfo', () => {
const url = 'https://brave.com'
it('bookmarks the url if bookmark has a tree node', () => {
const updateBookmarkInfo = bookmarksAPI.updateBookmarkInfo(state, url, true)
const assertion = updateBookmarkInfo.bookmarks
expect(assertion).toEqual({ 'https://brave.com': true })
})
it('sets bookmark to undefined if tree node is not defined', () => {
const updateBookmarkInfo = bookmarksAPI.updateBookmarkInfo(state, url, false)
const assertion = updateBookmarkInfo.bookmarks
expect(assertion).toEqual({ 'https://brave.com': undefined })
})
})
})
@@ -1,51 +0,0 @@
/* 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 { getGridSites } from '../../../../brave_new_tab_ui/api/topSites/grid'
describe('new tab grid api tests', () => {
const defaultState = {
topSites: [],
ignoredTopSites: [],
pinnedTopSites: [],
bookmarks: {}
}
describe('getGridSites', () => {
it('allows http sites', () => {
const url = 'http://cezaraugusto.net'
const newState = {
...defaultState,
topSites: [
{ url }
]
}
const assertion = getGridSites(newState, true)
expect(assertion[0]).toBe(newState.topSites[0])
expect(assertion[0].url).toBe(url)
})
it('allows https sites', () => {
const url = 'https://cezaraugusto.net'
const newState = {
...defaultState,
topSites: [
{ url }
]
}
const assertion = getGridSites(newState, true)
expect(assertion[0]).toBe(newState.topSites[0])
expect(assertion[0].url).toBe(url)
})
it('do not allow the default chrome topSites url', () => {
const url = 'https://chrome.google.com/webstore?hl=en'
const newState = {
...defaultState,
topSites: [
{ url }
]
}
const assertion = getGridSites(newState, true)
expect(assertion[0]).toBe(undefined)
})
})
})
@@ -2,272 +2,88 @@
* 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/. */
// Constants
import { types } from '../../../brave_new_tab_ui/constants/new_tab_types'
// Reducer
import newTabReducer from '../../../brave_new_tab_ui/reducers/new_tab_reducer'
// State
import { newTabInitialState } from '../../testData'
// API
import * as gridAPI from '../../../brave_new_tab_ui/api/topSites/grid'
import * as bookmarksAPI from '../../../brave_new_tab_ui/api/topSites/bookmarks'
import * as dndAPI from '../../../brave_new_tab_ui/api/topSites/dnd'
import * as storage from '../../../brave_new_tab_ui/storage'
const initialState = newTabInitialState.newTabData
describe('newTabReducer', () => {
const url: string = 'http://brave.com/'
const topSites: Partial<NewTab.Sites> = [{ url }]
const pinnedTopSites: Partial<NewTab.Sites> = topSites
const ignoredTopSites: Partial<NewTab.Sites> = [{ url: 'https://github.com' }]
const bookmarks: Partial<NewTab.Bookmark> = { [url]: { id: 'bookmark_id' } }
const fakeState = {
...initialState,
topSites,
bookmarks,
pinnedTopSites,
ignoredTopSites
}
describe('initial state', () => {
it('loads initial data', () => {
const expectedState = storage.load()
const returnedState = newTabReducer(undefined, {})
const returnedState = newTabReducer(undefined, { type: {} })
expect(returnedState).toEqual(expectedState)
})
})
describe('BOOKMARK_ADDED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(chrome.bookmarks, 'create')
})
afterEach(() => {
spy.mockRestore()
})
it('calls chrome.bookmarks.create if topSites url match payload url', () => {
newTabReducer(fakeState, {
type: types.BOOKMARK_ADDED,
payload: { url }
})
expect(spy).toBeCalled()
})
it('does not call chrome.bookmarks.create if url does not match', () => {
newTabReducer(fakeState, {
type: types.BOOKMARK_ADDED,
payload: { url: 'https://very-different-website-domain.com' }
})
expect(spy).not.toBeCalled()
})
describe('NEW_TAB_SET_INITIAL_DATA', () => {
// TODO
})
describe('BOOKMARK_REMOVED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(chrome.bookmarks, 'remove')
})
afterEach(() => {
spy.mockRestore()
})
it('calls chrome.bookmarks.remove if bookmarkInfo exists', () => {
newTabReducer(fakeState, {
type: types.BOOKMARK_REMOVED,
payload: { url }
})
expect(spy).toBeCalled()
})
it('does not call chrome.bookmarks.remove if bookmarkInfo is undefined', () => {
const newTabInitialStateWithoutBookmarks = { ...initialState, bookmarks: {} }
newTabReducer(newTabInitialStateWithoutBookmarks, {
type: types.BOOKMARK_REMOVED,
payload: { url }
})
expect(spy).not.toBeCalled()
})
describe('NEW_TAB_STATS_UPDATED', () => {
// TODO
})
describe('NEW_TAB_SITE_PINNED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(gridAPI, 'calculateGridSites')
})
afterEach(() => {
spy.mockRestore()
})
it('calls gridAPI.calculateGridSites', () => {
jest.useFakeTimers()
newTabReducer(fakeState, {
type: types.NEW_TAB_SITE_PINNED,
payload: { url }
})
jest.runAllTimers()
expect(spy).toBeCalled()
jest.useRealTimers()
})
describe('NEW_TAB_PRIVATE_TAB_DATA_UPDATED', () => {
// TODO
})
describe('NEW_TAB_SITE_UNPINNED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(gridAPI, 'calculateGridSites')
})
afterEach(() => {
spy.mockRestore()
})
it('calls gridAPI.calculateGridSites', () => {
jest.useFakeTimers()
newTabReducer(fakeState, {
type: types.NEW_TAB_SITE_UNPINNED,
payload: { url }
})
jest.runAllTimers()
expect(spy).toBeCalled()
jest.useRealTimers()
})
describe('NEW_TAB_DISMISS_BRANDED_WALLPAPER_NOTIFICATION', () => {
// TODO
})
describe('NEW_TAB_SITE_IGNORED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(gridAPI, 'calculateGridSites')
})
afterEach(() => {
spy.mockRestore()
})
it('calls gridAPI.calculateGridSites', () => {
jest.useFakeTimers()
newTabReducer(fakeState, {
type: types.NEW_TAB_SITE_IGNORED,
payload: { url }
})
jest.runAllTimers()
expect(spy).toBeCalled()
jest.useRealTimers()
})
describe('NEW_TAB_PREFERENCES_UPDATED', () => {
// TODO
})
describe('NEW_TAB_UNDO_SITE_IGNORED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(gridAPI, 'calculateGridSites')
describe('rewards features inside new tab page', () => {
describe('CREATE_WALLET', () => {
// TODO
})
afterEach(() => {
spy.mockRestore()
describe('ON_ENABLED_MAIN', () => {
// TODO
})
it('calls gridAPI.calculateGridSites', () => {
jest.useFakeTimers()
newTabReducer(fakeState, {
type: types.NEW_TAB_UNDO_SITE_IGNORED,
payload: { url }
})
jest.runAllTimers()
expect(spy).toBeCalled()
jest.useRealTimers()
describe('CREATE_WALLET', () => {
// TODO
})
})
describe('NEW_TAB_UNDO_ALL_SITE_IGNORED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(gridAPI, 'calculateGridSites')
describe('ON_ENABLED_MAIN', () => {
// TODO
})
afterEach(() => {
spy.mockRestore()
describe('ON_WALLET_INITIALIZED', () => {
// TODO
})
it('calls gridAPI.calculateGridSites', () => {
jest.useFakeTimers()
newTabReducer(fakeState, {
type: types.NEW_TAB_UNDO_ALL_SITE_IGNORED
})
jest.runAllTimers()
expect(spy).toBeCalled()
describe('WALLET_CORRUPT', () => {
// TODO
})
jest.useRealTimers()
})
describe('NEW_TAB_HIDE_SITE_REMOVAL_NOTIFICATION', () => {
it('set showSiteRemovalNotification to false', () => {
const assertion = newTabReducer(fakeState, {
type: types.NEW_TAB_HIDE_SITE_REMOVAL_NOTIFICATION
})
expect(assertion).toEqual({
...fakeState,
showSiteRemovalNotification: false
})
describe('WALLET_CREATED', () => {
// TODO
})
})
describe('NEW_TAB_SITE_DRAGGED', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(dndAPI, 'onDraggedSite')
describe('LEDGER_OK', () => {
// TODO
})
afterEach(() => {
spy.mockRestore()
describe('ON_ADS_ENABLED', () => {
// TODO
})
it('calls dndAPI.onDraggedSite', () => {
newTabReducer(fakeState, {
type: types.NEW_TAB_SITE_DRAGGED,
payload: {
fromUrl: 'https://brave.com',
toUrl: 'https://github.com'
}
})
expect(spy).toBeCalled()
describe('ON_ADS_ESTIMATED_EARNINGS', () => {
// TODO
})
})
describe('NEW_TAB_SITE_DRAG_END', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(dndAPI, 'onDragEnd')
describe('ON_BALANCE_REPORT', () => {
// TODO
})
afterEach(() => {
spy.mockRestore()
describe('DISMISS_NOTIFICATION', () => {
// TODO
})
it('calls dndAPI.onDragEnd', () => {
newTabReducer(fakeState, {
type: types.NEW_TAB_SITE_DRAG_END
})
expect(spy).toBeCalled()
describe('ON_PROMOTIONS', () => {
// TODO
})
})
describe('NEW_TAB_BOOKMARK_INFO_AVAILABLE', () => {
let spy: jest.SpyInstance
beforeEach(() => {
spy = jest.spyOn(bookmarksAPI, 'updateBookmarkInfo')
describe('ON_BALANCE', () => {
// TODO
})
afterEach(() => {
spy.mockRestore()
describe('ON_WALLET_EXISTS', () => {
// TODO
})
it('calls bookmarksAPI.updateBookmarkInfo', () => {
const queryUrl: string = 'https://brave.com'
const bookmarkTreeNode = {
dateAdded: 1557899510259,
id: '7',
index: 0,
parentId: '2',
title: 'Secure, Fast & Private Web Browser with Adblocker | Brave Browser',
url: 'http://brave.com/'
}
newTabReducer(fakeState, {
type: types.NEW_TAB_BOOKMARK_INFO_AVAILABLE,
payload: {
queryUrl,
bookmarkTreeNode
}
})
expect(spy).toBeCalled()
describe('SET_PRE_INITIAL_REWARDS_DATA', () => {
// TODO
})
})
describe('NEW_TAB_GRID_SITES_UPDATED', () => {
it('sets gridSites into gridSites state', () => {
const url: string = 'http://brave.com/'
const gridSites: Partial<NewTab.Sites> = [{ url }]
const assertion = newTabReducer(fakeState, {
type: types.NEW_TAB_GRID_SITES_UPDATED,
payload: { gridSites }
})
expect(assertion).toEqual({
...fakeState,
gridSites: [ { url: 'http://brave.com/' } ]
})
describe('SET_INITIAL_REWARDS_DATA', () => {
// TODO
})
})
})
+1 -3
View File
@@ -44,15 +44,13 @@ export const newTabInitialState: NewTab.ApplicationState = {
showBackgroundImage: false,
showSettingsMenu: false,
topSites: [],
ignoredTopSites: [],
pinnedTopSites: [],
excludedSites: [],
gridSites: [],
showEmptyPage: false,
isIncognito: new ChromeEvent(),
useAlternativePrivateSearchEngine: false,
isTor: false,
isQwant: false,
bookmarks: {},
stats: {
adsBlockedStat: 0,
javascriptBlockedStat: 0,