diff --git a/app/common/state/bookmarkFoldersState.js b/app/common/state/bookmarkFoldersState.js index 49046a08a03..a34dec1ca2c 100644 --- a/app/common/state/bookmarkFoldersState.js +++ b/app/common/state/bookmarkFoldersState.js @@ -36,6 +36,11 @@ const bookmarkFoldersState = { getFolder: (state, folderKey) => { state = validateState(state) + + if (folderKey == null) { + return Immutable.Map() + } + folderKey = folderKey.toString() return state.getIn([STATE_SITES.BOOKMARK_FOLDERS, folderKey], Immutable.Map()) }, @@ -43,12 +48,21 @@ const bookmarkFoldersState = { getFoldersByParentId: (state, parentFolderId) => { state = validateState(state) + if (parentFolderId == null) { + return Immutable.List() + } + const folders = bookmarkOrderCache.getFoldersByParentId(state, parentFolderId) return folders.map(folder => bookmarkFoldersState.getFolder(state, folder.get('key'))) }, addFolder: (state, folderDetails, destinationKey) => { state = validateState(state) + + if (folderDetails == null) { + return state + } + folderDetails = makeImmutable(folderDetails) let folders = bookmarkFoldersState.getFolders(state) let key = folderDetails.get('folderId') diff --git a/app/common/state/bookmarksState.js b/app/common/state/bookmarksState.js index 6d0b38c2a27..fdba29e447f 100644 --- a/app/common/state/bookmarksState.js +++ b/app/common/state/bookmarksState.js @@ -270,10 +270,12 @@ const bookmarksState = { : destinationItem.get('folderId') state = bookmarkOrderCache.removeCacheKey(state, bookmark.get('parentFolderId'), bookmarkKey) + state = bookmarkLocationCache.removeCacheKey(state, bookmark.get('location'), bookmarkKey) bookmark = bookmark.set('parentFolderId', ~~parentFolderId) const newKey = bookmarkUtil.getKey(bookmark) state = state.deleteIn([STATE_SITES.BOOKMARKS, bookmarkKey]) state = bookmarkOrderCache.addBookmarkToCache(state, bookmark.get('parentFolderId'), newKey) + state = bookmarkLocationCache.addCacheKey(state, bookmark.get('location'), newKey) bookmark = bookmark.set('key', newKey) return state.setIn([STATE_SITES.BOOKMARKS, newKey], bookmark) } diff --git a/test/unit/app/common/state/aboutHistoryStateTest.js b/test/unit/app/common/state/aboutHistoryStateTest.js index ac40ae6c82f..f3cea453609 100644 --- a/test/unit/app/common/state/aboutHistoryStateTest.js +++ b/test/unit/app/common/state/aboutHistoryStateTest.js @@ -1,29 +1,39 @@ -/* global describe, it */ +/* 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/. */ + +/* global describe, it, before, after */ const aboutHistoryState = require('../../../../../app/common/state/aboutHistoryState') const Immutable = require('immutable') const assert = require('assert') +const sinon = require('sinon') const defaultAppState = Immutable.fromJS({ about: { history: { - entries: [], - updatedStamp: undefined + entries: {}, + updatedStamp: 0 } } }) const arbitraryTimeInThePast = 1450000000000 +const historyItems = Immutable.fromJS({ + 'https://brave.com/|0': { + location: 'https://brave.com' + } +}) + const assertTimeUpdated = (state) => { const updatedStamp = state.getIn(['about', 'history', 'updatedStamp']) assert.equal(typeof updatedStamp === 'number' && updatedStamp > arbitraryTimeInThePast, true) } -describe('aboutHistoryState', function () { +describe('aboutHistoryState unit test', function () { describe('getHistory', function () { it('reads the history from the state', function () { - const fakeHistoryEntries = [1, 2, 3] - const state = defaultAppState.setIn(['about', 'history', 'entries'], fakeHistoryEntries) + const state = defaultAppState.setIn(['about', 'history', 'entries'], historyItems) const history = aboutHistoryState.getHistory(state) assert.deepEqual(state.getIn(['about', 'history']).toJS(), history.toJS()) }) @@ -35,4 +45,21 @@ describe('aboutHistoryState', function () { assertTimeUpdated(state) }) }) + + describe('clearHistory', function () { + before(function () { + this.clock = sinon.useFakeTimers() + this.clock.tick(0) + }) + + after(function () { + this.clock.restore() + }) + + it('history is cleared', function () { + const state = defaultAppState.setIn(['about', 'history', 'entries'], historyItems) + const history = aboutHistoryState.clearHistory(state) + assert.deepEqual(history.toJS(), defaultAppState.toJS()) + }) + }) }) diff --git a/test/unit/app/common/state/aboutNewTabStateTest.js b/test/unit/app/common/state/aboutNewTabStateTest.js index 83a23f46ede..b41f854d6bc 100644 --- a/test/unit/app/common/state/aboutNewTabStateTest.js +++ b/test/unit/app/common/state/aboutNewTabStateTest.js @@ -1,7 +1,12 @@ -/* global describe, it */ +/* 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/. */ + +/* global describe, it, before, after */ const aboutNewTabState = require('../../../../../app/common/state/aboutNewTabState') const Immutable = require('immutable') const assert = require('assert') +const sinon = require('sinon') const defaultAppState = Immutable.fromJS({ about: { @@ -10,7 +15,7 @@ const defaultAppState = Immutable.fromJS({ sites: [], ignoredTopSites: [], pinnedTopSites: [], - updatedStamp: undefined + updatedStamp: 0 } } }) @@ -26,10 +31,10 @@ const assertTimeUpdated = (state) => { const assertNoChange = (state) => { const updatedStamp = state.getIn(['about', 'newtab', 'updatedStamp']) assert.deepEqual(state, defaultAppState) - assert.equal(updatedStamp, undefined) + assert.equal(updatedStamp, 0) } -describe('aboutNewTabState', function () { +describe('aboutNewTabState unit test', function () { describe('getSites', function () { it('returns the contents of about.newtab.sites', function () { const expectedSites = Immutable.List().push(1).push(2).push(3) @@ -87,4 +92,25 @@ describe('aboutNewTabState', function () { assert.equal(updatedValue, 'TEST STRING') }) }) + + describe('clearTopSites', function () { + before(function () { + this.clock = sinon.useFakeTimers() + this.clock.tick(0) + }) + + after(function () { + this.clock.restore() + }) + + it('is cleared', function () { + const state = defaultAppState.setIn(['about', 'newtab', 'sites'], Immutable.fromJS([ + { + location: 'https://brave.com' + } + ])) + const topSItes = aboutNewTabState.clearTopSites(state) + assert.deepEqual(topSItes.toJS(), defaultAppState.toJS()) + }) + }) }) diff --git a/test/unit/app/common/state/bookmarkFoldersStateTest.js b/test/unit/app/common/state/bookmarkFoldersStateTest.js new file mode 100644 index 00000000000..5c03c0bdcf2 --- /dev/null +++ b/test/unit/app/common/state/bookmarkFoldersStateTest.js @@ -0,0 +1,284 @@ +/* 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/. */ + +/* global describe, it, before, after */ +const mockery = require('mockery') +const Immutable = require('immutable') +const assert = require('assert') + +const siteTags = require('../../../../../js/constants/siteTags') +const {STATE_SITES} = require('../../../../../js/constants/stateConstants') +require('../../../braveUnit') + +describe('bookmarkFoldersState unit test', function () { + let bookmarkFoldersState + + const state = Immutable.fromJS({ + bookmarks: {}, + bookmarkFolders: {}, + cache: { + bookmarkOrder: {} + } + }) + + const stateWithData = Immutable.fromJS({ + bookmarks: {}, + bookmarkFolders: { + '1': { + title: 'folder1', + folderId: 1, + key: '1', + parentFolderId: 0, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER + }, + '2': { + title: 'folder2', + folderId: 2, + key: '2', + parentFolderId: 1, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER + }, + '69': { + title: 'folder69', + folderId: 69, + key: '69', + parentFolderId: 0, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER + } + }, + cache: { + bookmarkOrder: { + '0': [ + { + key: '1', + order: 0, + type: siteTags.BOOKMARK_FOLDER + }, + { + key: '69', + order: 1, + type: siteTags.BOOKMARK_FOLDER + } + ], + '1': [ + { + key: '2', + order: 0, + type: siteTags.BOOKMARK_FOLDER + } + ] + } + } + }) + + before(function () { + mockery.enable({ + warnOnReplace: false, + warnOnUnregistered: false, + useCleanCache: true + }) + bookmarkFoldersState = require('../../../../../app/common/state/bookmarkFoldersState') + }) + + after(function () { + mockery.disable() + }) + + describe('getFolders', function () { + it('return folders', function () { + const newState = bookmarkFoldersState.getFolders(stateWithData) + assert.deepEqual(newState.toJS(), stateWithData.get(STATE_SITES.BOOKMARK_FOLDERS).toJS()) + }) + }) + + describe('getFolder', function () { + it('null case', function () { + const newState = bookmarkFoldersState.getFolder(stateWithData) + assert.deepEqual(newState, Immutable.Map()) + }) + + it('folder key is not found', function () { + const newState = bookmarkFoldersState.getFolder(stateWithData, '100') + assert.deepEqual(newState, Immutable.Map()) + }) + + it('folder key is found', function () { + const newState = bookmarkFoldersState.getFolder(stateWithData, '1') + assert.deepEqual(newState.toJS(), stateWithData.getIn([STATE_SITES.BOOKMARK_FOLDERS, '1']).toJS()) + }) + }) + + describe('getFoldersByParentId', function () { + it('null case', function () { + const newState = bookmarkFoldersState.getFoldersByParentId(stateWithData) + assert.deepEqual(newState, Immutable.List()) + }) + + it('parent folder is not found', function () { + const newState = bookmarkFoldersState.getFoldersByParentId(stateWithData, '1000') + assert.deepEqual(newState, Immutable.List()) + }) + + it('parent folder is found, but dont have any items', function () { + const newState = bookmarkFoldersState.getFoldersByParentId(stateWithData, '69') + assert.deepEqual(newState, Immutable.List()) + }) + + it('parent folder has child item', function () { + const newState = bookmarkFoldersState.getFoldersByParentId(stateWithData, '1') + assert.deepEqual(newState, Immutable.fromJS([ + { + title: 'folder2', + folderId: 2, + key: '2', + parentFolderId: 1, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER + } + ])) + }) + }) + + describe('addFolder', function () { + it('null case', function () { + const newState = bookmarkFoldersState.addFolder(state) + assert.deepEqual(newState.toJS(), state.toJS()) + }) + + it('folder key is provided', function () { + const newState = bookmarkFoldersState.addFolder(state, { + title: 'Brave', + folderId: 10 + + }) + const expectedState = state + .setIn(['bookmarkFolders', '10'], Immutable.fromJS({ + title: 'Brave', + folderId: 10, + key: '10', + parentFolderId: 0, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER, + skipSync: null + })) + .setIn(['cache', 'bookmarkOrder', '0'], Immutable.fromJS([ + { + key: '10', + order: 0, + type: siteTags.BOOKMARK_FOLDER + } + ])) + assert.deepEqual(newState.toJS(), expectedState.toJS()) + }) + + it('folder key is not provided', function () { + const newState = bookmarkFoldersState.addFolder(stateWithData, { + title: 'Brave', + parentFolderId: 3 + }) + const expectedState = stateWithData + .setIn(['bookmarkFolders', '70'], Immutable.fromJS({ + title: 'Brave', + folderId: 70, + key: '70', + parentFolderId: 3, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER, + skipSync: null + })) + .setIn(['cache', 'bookmarkOrder', '3'], Immutable.fromJS([ + { + key: '70', + order: 0, + type: siteTags.BOOKMARK_FOLDER + } + ])) + assert.deepEqual(newState.toJS(), expectedState.toJS()) + }) + + it('folder key is not provided, destination key is provided', function () { + const newState = bookmarkFoldersState.addFolder(stateWithData, { + title: 'Brave' + }, '1') + const expectedState = stateWithData + .setIn(['bookmarkFolders', '70'], Immutable.fromJS({ + title: 'Brave', + folderId: 70, + key: '70', + parentFolderId: 0, + partitionNumber: 0, + objectId: null, + type: siteTags.BOOKMARK_FOLDER, + skipSync: null + })) + .setIn(['cache', 'bookmarkOrder', '0'], Immutable.fromJS([ + { + key: '1', + order: 0, + type: siteTags.BOOKMARK_FOLDER + }, + { + key: '70', + order: 1, + type: siteTags.BOOKMARK_FOLDER + }, + { + key: '69', + order: 2, + type: siteTags.BOOKMARK_FOLDER + } + ])) + assert.deepEqual(newState.toJS(), expectedState.toJS()) + }) + }) + + describe('editFolder', function () { + it('', function () { + + }) + + it('', function () { + + }) + }) + + describe('removeFolder', function () { + it('', function () { + + }) + + it('', function () { + + }) + }) + + describe('getFoldersWithoutKey', function () { + it('', function () { + + }) + + it('', function () { + + }) + }) + + describe('moveFolder', function () { + it('', function () { + + }) + + it('', function () { + + }) + }) +})