Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Follow up for sites splits PR #10296
Browse files Browse the repository at this point in the history
Resolves #10296
Resolves #10348

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Aug 9, 2017
1 parent c91b1c9 commit 4bb21ee
Show file tree
Hide file tree
Showing 5 changed files with 363 additions and 10 deletions.
14 changes: 14 additions & 0 deletions app/common/state/bookmarkFoldersState.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,33 @@ 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())
},

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')
Expand Down
2 changes: 2 additions & 0 deletions app/common/state/bookmarksState.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,10 +266,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)
}
Expand Down
39 changes: 33 additions & 6 deletions test/unit/app/common/state/aboutHistoryStateTest.js
Original file line number Diff line number Diff line change
@@ -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())
})
Expand All @@ -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())
})
})
})
34 changes: 30 additions & 4 deletions test/unit/app/common/state/aboutNewTabStateTest.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand All @@ -10,7 +15,7 @@ const defaultAppState = Immutable.fromJS({
sites: [],
ignoredTopSites: [],
pinnedTopSites: [],
updatedStamp: undefined
updatedStamp: 0
}
}
})
Expand All @@ -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)
Expand Down Expand Up @@ -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())
})
})
})
Loading

0 comments on commit 4bb21ee

Please sign in to comment.