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

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Nov 3, 2017
1 parent 298c7b5 commit 6fc982e
Show file tree
Hide file tree
Showing 6 changed files with 238 additions and 21 deletions.
12 changes: 12 additions & 0 deletions app/common/cache/ledgerVideoCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,19 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

const Immutable = require('immutable')
const assert = require('assert')

const { makeImmutable, isMap } = require('../state/immutableUtil')

const validateState = function (state) {
state = makeImmutable(state)
assert.ok(isMap(state), 'state must be an Immutable.Map')
assert.ok(isMap(state.getIn(['cache', 'ledgerVideos'])), 'state must contain ledgerVideos as Immutable.Map')
return state
}

const getDataByVideoId = (state, key) => {
state = validateState(state)
if (key == null) {
return Immutable.Map()
}
Expand All @@ -12,6 +23,7 @@ const getDataByVideoId = (state, key) => {
}

const setCacheByVideoId = (state, key, data) => {
state = validateState(state)
if (key == null) {
return state
}
Expand Down
68 changes: 48 additions & 20 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,11 @@ const stickyP = (state, publisherKey) => {

const getMediaId = (data, type) => {
let id = null

if (type == null || data == null) {
return id
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
Expand All @@ -204,11 +209,16 @@ const getMediaKey = (id, type) => {
return null
}

return `${type.toLowerCase()}_${id.toLowerCase()}`
return `${type.toLowerCase()}_${id}`
}

const getMediaData = (xhr, type) => {
let result = null

if (xhr == null || type == null) {
return result
}

switch (type) {
case ledgerMediaProviders.YOUTUBE:
{
Expand Down Expand Up @@ -253,18 +263,22 @@ const getYoutubeDuration = (data) => {
}

for (let i = 0; i < startTime.length; i++) {
time += parseInt(endTime[i]) - parseInt(startTime[i])
time += parseFloat(endTime[i]) - parseFloat(startTime[i])
}

// we get seconds back, so we need to convert it into ms
time = time * 1000

return time
return parseInt(time)
}

const isMediaProvider = (url) => {
let provider = null

if (url == null) {
return provider
}

// Youtube
if (url.startsWith('https://www.youtube.com/api/stats/watchtime?')) {
provider = ledgerMediaProviders.YOUTUBE
Expand All @@ -273,21 +287,35 @@ const isMediaProvider = (url) => {
return provider
}

module.exports = {
shouldTrackView,
batToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus,
blockedP,
contributeP,
visibleP,
eligibleP,
stickyP,
formatCurrentBalance,
getMediaId,
getMediaDuration,
isMediaProvider,
getMediaData,
getMediaKey
const getMethods = () => {
const publicMethods = {
shouldTrackView,
batToCurrencyString,
formattedTimeFromNow,
formattedDateFromTimestamp,
walletStatus,
blockedP,
contributeP,
visibleP,
eligibleP,
stickyP,
formatCurrentBalance,
getMediaId,
getMediaDuration,
isMediaProvider,
getMediaData,
getMediaKey
}

let privateMethods = {}

if (process.env.NODE_ENV === 'test') {
privateMethods = {
getYoutubeDuration
}
}

return Object.assign({}, publicMethods, privateMethods)
}

module.exports = getMethods()
3 changes: 2 additions & 1 deletion app/sessionStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -925,7 +925,8 @@ module.exports.defaultAppState = () => {
},
cache: {
bookmarkLocation: undefined,
bookmarkOrder: {}
bookmarkOrder: {},
ledgerVideos: {}
},
pinnedSites: {},
bookmarks: {},
Expand Down
3 changes: 3 additions & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ AppStore
order: number,
type: string // siteTags.BOOKMARK or siteTags.BOOKMARK_FOLDER
}]
},
ledgerVideos: {
[mediaKey]: string // publisher key
}
}
clearBrowsingDataDefaults: {
Expand Down
53 changes: 53 additions & 0 deletions test/unit/app/common/cache/ledgerVideoCacheTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* 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 */

const Immutable = require('immutable')
const assert = require('assert')
const ledgerVideoCache = require('../../../../../app/common/cache/ledgerVideoCache')

const baseState = Immutable.fromJS({
cache: {
ledgerVideos: {}
}
})
const stateWithData = Immutable.fromJS({
cache: {
ledgerVideos: {
'youtube_kLiLOkzLetE': 'youtube#channel:kLiLOkzLetE'
}
}
})

describe('ledgerVideoCache unit test', function () {
describe('getDataByVideoId', function () {
it('key is not provided', function () {
const result = ledgerVideoCache.getDataByVideoId(baseState)
assert.deepEqual(result.toJS(), {})
})

it('key does not exist in the cache', function () {
const result = ledgerVideoCache.getDataByVideoId(baseState, 'key')
assert.deepEqual(result.toJS(), {})
})

it('data is ok', function () {
const result = ledgerVideoCache.getDataByVideoId(stateWithData, 'youtube_kLiLOkzLetE')
assert.deepEqual(result, 'youtube#channel:kLiLOkzLetE')
})
})

describe('setCacheByVideoId', function () {
it('key is not provided', function () {
const state = ledgerVideoCache.setCacheByVideoId(baseState)
assert.deepEqual(state.toJS(), baseState.toJS())
})

it('data is ok', function () {
const state = ledgerVideoCache.setCacheByVideoId(baseState, 'youtube_kLiLOkzLetE', 'youtube#channel:kLiLOkzLetE')
const expectedState = state.setIn(['cache', 'ledgerVideos', 'youtube_kLiLOkzLetE'], 'youtube#channel:kLiLOkzLetE')
assert.deepEqual(state.toJS(), expectedState.toJS())
})
})
})
120 changes: 120 additions & 0 deletions test/unit/app/common/lib/ledgerUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const mockery = require('mockery')
const assert = require('assert')
const Immutable = require('immutable')
require('../../../braveUnit')
const ledgerMediaProviders = require('../../../../../app/common/constants/ledgerMediaProviders')

describe('ledgerUtil test', function () {
let ledgerUtil
Expand Down Expand Up @@ -171,4 +172,123 @@ describe('ledgerUtil test', function () {

describe('walletStatus', function () {
})

describe('getMediaId', function () {
it('null case', function () {
const result = ledgerUtil.getMediaId()
assert.equal(result, null)
})

it('unknown type', function () {
const result = ledgerUtil.getMediaData({}, 'test')
assert.equal(result, null)
})

describe('Youtube', function () {
it('null case', function () {
const result = ledgerUtil.getMediaId(null, ledgerMediaProviders.YOUTUBE)
assert.equal(result, null)
})

it('id is provided', function () {
const result = ledgerUtil.getMediaId({docid: 'kLiLOkzLetE'}, ledgerMediaProviders.YOUTUBE)
assert.equal(result, 'kLiLOkzLetE')
})
})
})

describe('getMediaKey', function () {
it('null case', function () {
const result = ledgerUtil.getMediaKey()
assert.equal(result, null)
})

it('type is missing', function () {
const result = ledgerUtil.getMediaKey('kLiLOkzLetE')
assert.equal(result, null)
})

it('id is null', function () {
const result = ledgerUtil.getMediaKey(null, ledgerMediaProviders.YOUTUBE)
assert.equal(result, null)
})

it('data is ok', function () {
const result = ledgerUtil.getMediaKey('kLiLOkzLetE', ledgerMediaProviders.YOUTUBE)
assert.equal(result, 'youtube_kLiLOkzLetE')
})
})

describe('getMediaData', function () {
it('null case', function () {
const result = ledgerUtil.getMediaData()
assert.equal(result, null)
})

it('unknown type', function () {
const result = ledgerUtil.getMediaData('https://youtube.com', 'test')
assert.equal(result, null)
})

describe('Youtube', function () {
it('null case', function () {
const result = ledgerUtil.getMediaData(null, ledgerMediaProviders.YOUTUBE)
assert.equal(result, null)
})

it('query is not present', function () {
const result = ledgerUtil.getMediaData('https://youtube.com', ledgerMediaProviders.YOUTUBE)
assert.equal(result, null)
})

it('query is present', function () {
const result = ledgerUtil.getMediaData('https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=11.338&et=21.339', ledgerMediaProviders.YOUTUBE)
assert.deepEqual(result, {
docid: 'kLiLOkzLetE',
st: '11.338',
et: '21.339'
})
})
})
})

describe('getYoutubeDuration', function () {
it('null case', function () {
const result = ledgerUtil.getYoutubeDuration()
assert.equal(result, 0)
})

it('multiple times', function () {
const result = ledgerUtil.getYoutubeDuration({
st: '11.338,21.339,25.000',
et: '21.339,25.000,26.100'
})
assert.equal(result, 14762)
})

it('single time', function () {
const result = ledgerUtil.getYoutubeDuration({
st: '11.338',
et: '21.339'
})
assert.equal(result, 10001)
})
})

describe('isMediaProvider', function () {
it('null case', function () {
const result = ledgerUtil.isMediaProvider()
assert.equal(result, null)
})

it('unknown provider', function () {
const result = ledgerUtil.isMediaProvider('https://www.brave.com')
assert.equal(result, null)
})

it('youtube', function () {
const result = ledgerUtil.isMediaProvider('https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=11.338&et=21.339')
assert.equal(result, ledgerMediaProviders.YOUTUBE)
})
})
})

0 comments on commit 6fc982e

Please sign in to comment.