Skip to content

Commit

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

Auditors:

Test Plan:
  • Loading branch information
NejcZdovc committed Nov 7, 2017
1 parent 3bb444e commit f1a68a8
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 10 deletions.
19 changes: 19 additions & 0 deletions app/common/state/bookmarkFoldersState.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,39 @@ 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)

if (folderDetails.get('key') == null) {
return state
}

state = state.setIn([STATE_SITES.BOOKMARK_FOLDERS, folderDetails.get('key')], folderDetails)
state = bookmarkOrderCache.addFolderToCache(state, folderDetails.get('parentFolderId'), folderDetails.get('key'), destinationKey)
return state
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())
})
})
})
223 changes: 223 additions & 0 deletions test/unit/app/common/state/bookmarkFoldersStateTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/* 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',
key: 10
})
const expectedState = state
.setIn(['bookmarkFolders', '10'], Immutable.fromJS({
title: 'Brave',
key: '10'
}))
.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'
})
assert.deepEqual(newState.toJS(), stateWithData.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 () {

})
})
})

0 comments on commit f1a68a8

Please sign in to comment.