This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 973
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pulled code from about:history into a new historyUtil module
Fixes #5353 This has regressed at least once before; we need a way besides web driver to test the code. (usually, problem is immutable vs mutable; ex: ".get method not found", etc) Auditors: @bbondy Test Plan: 1. Launch Brave and visit about:history 2. Double click any entry 3. Enjoy :)
- Loading branch information
Showing
3 changed files
with
161 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* 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/. */ | ||
|
||
'use strict' | ||
const Immutable = require('immutable') | ||
|
||
const getDayString = (entry, locale) => { | ||
const lastAccessedTime = entry.get('lastAccessedTime') | ||
return lastAccessedTime | ||
? new Date(lastAccessedTime).toLocaleDateString(locale, { weekday: 'long', month: 'long', day: 'numeric', year: 'numeric' }) | ||
: '' | ||
} | ||
|
||
module.exports.groupEntriesByDay = (history, locale) => { | ||
const reduced = history.reduce((previousValue, currentValue, currentIndex, array) => { | ||
const result = currentIndex === 1 ? [] : previousValue | ||
if (currentIndex === 1) { | ||
const firstDate = getDayString(currentValue, locale) | ||
result.push({date: firstDate, entries: [previousValue]}) | ||
} | ||
const date = getDayString(currentValue, locale) | ||
const dateIndex = result.findIndex((entryByDate) => entryByDate.date === date) | ||
if (dateIndex !== -1) { | ||
result[dateIndex].entries.push(currentValue) | ||
} else { | ||
result.push({date: date, entries: [currentValue]}) | ||
} | ||
return result | ||
}) | ||
if (reduced) { | ||
return Immutable.fromJS( | ||
Array.isArray(reduced) | ||
? reduced | ||
: [{date: getDayString(reduced, locale), entries: [reduced]}] | ||
) | ||
} | ||
return Immutable.fromJS([]) | ||
} | ||
|
||
/** | ||
* Return an array with ALL entries. | ||
* Format is expected to be array containing one array per day. | ||
*/ | ||
module.exports.totalEntries = (entriesByDay) => { | ||
let result = new Immutable.List() | ||
entriesByDay.forEach((entry) => { | ||
result = result.push(entry.get('entries')) | ||
}) | ||
return result | ||
} | ||
|
||
module.exports.sortTimeDescending = (left, right) => { | ||
if (left.get('lastAccessedTime') < right.get('lastAccessedTime')) return 1 | ||
if (left.get('lastAccessedTime') > right.get('lastAccessedTime')) return -1 | ||
return 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* global describe, it */ | ||
const assert = require('assert') | ||
const Immutable = require('immutable') | ||
const historyUtil = require('../../../app/common/lib/historyUtil') | ||
require('../braveUnit') | ||
|
||
describe('historyUtil', function () { | ||
const historyDayOne = Immutable.fromJS({ | ||
lastAccessedTime: 1477944718876, | ||
location: 'https://brave.com/page1', | ||
title: 'sample 1', | ||
tags: [] | ||
}) | ||
const historyDayTwo = Immutable.fromJS({ | ||
lastAccessedTime: 1478079042097, | ||
location: 'https://brave.com/page2', | ||
title: 'sample 2', | ||
tags: [] | ||
}) | ||
const historyDayThree = Immutable.fromJS([{ | ||
lastAccessedTime: 1478157051910, | ||
location: 'https://brave.com/page3', | ||
title: 'sample 3', | ||
tags: [] | ||
}, { | ||
lastAccessedTime: 1478157051921, | ||
location: 'https://brave.com/page4', | ||
title: 'sample 4', | ||
tags: [] | ||
}, { | ||
lastAccessedTime: 1478157051932, | ||
location: 'https://brave.com/page5', | ||
title: 'sample 5', | ||
tags: [] | ||
}]) | ||
const historyMultipleDays = historyDayThree.push(historyDayTwo, historyDayOne) | ||
|
||
describe('groupEntriesByDay', function () { | ||
it('returns the result as an Immutable.List', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree) | ||
assert.equal(Immutable.List.isList(result), true) | ||
}) | ||
it('has one object for each day', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree) | ||
assert.equal(result.size, 1) | ||
}) | ||
it('can handle multiple days', function () { | ||
const result = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
assert.equal(result.size, 3) | ||
}) | ||
describe('with the object representing a day', function () { | ||
it('formats a readable `date` field', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree, 'en-US') | ||
assert.equal(result.getIn([0, 'date']), 'Thursday, November 3, 2016') | ||
}) | ||
it('has an entry for each history item', function () { | ||
const result = historyUtil.groupEntriesByDay(historyDayThree, 'en-US') | ||
const entries = result.getIn([0, 'entries']) | ||
assert.equal(entries && entries.size, historyDayThree.size) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('totalEntries', function () { | ||
it('returns the result as an Immutable.List', function () { | ||
const result1 = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
const result2 = historyUtil.totalEntries(result1) | ||
assert.equal(Immutable.List.isList(result2), true) | ||
}) | ||
it('combines entries for multiple days into one response', function () { | ||
const result1 = historyUtil.groupEntriesByDay(historyMultipleDays) | ||
const result2 = historyUtil.totalEntries(result1) | ||
const expectedResult = [ | ||
historyDayThree.toJS(), | ||
[historyDayTwo.toJS()], | ||
[historyDayOne.toJS()] | ||
] | ||
assert.deepEqual(result2.toJS(), expectedResult) | ||
}) | ||
}) | ||
|
||
describe('sortTimeDescending', function () { | ||
it('sorts the items by date/time DESC', function () { | ||
const result = historyMultipleDays.sort(historyUtil.sortTimeDescending) | ||
const expectedResult = historyDayThree.toJS().reverse() | ||
expectedResult.push(historyDayTwo.toJS()) | ||
expectedResult.push(historyDayOne.toJS()) | ||
|
||
assert.deepEqual(result.toJS(), expectedResult) | ||
}) | ||
}) | ||
}) |