Skip to content

Commit

Permalink
Adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc authored and syuan100 committed Nov 9, 2017
1 parent 87ba927 commit 4d745b6
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 14 deletions.
33 changes: 19 additions & 14 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ const verifiedP = (state, publisherKey, callback) => {
}

if (callback) {
let data = {}
if (result && result.properties && result.properties) {
data = result.properties
let data = false
if (result && result.properties && result.properties.verified) {
data = result.properties.verified
}

callback(null, data)
Expand Down Expand Up @@ -952,19 +952,20 @@ const addVisit = (state, startTimestamp, location, tabId) => {
}

const checkVerifiedStatus = (state, publisherKey) => {
if (publisherKey == null) {
return state
}

const lastUpdate = parseInt(ledgerState.getLedgerValue(state, 'publisherTimestamp'))
const lastPublisherUpdate = parseInt(ledgerState.getPublisherOption(state, publisherKey, 'verifiedTimestamp') || 0)

if (lastUpdate > lastPublisherUpdate) {
state = verifiedP(state, publisherKey, (error, result) => {
state = module.exports.verifiedP(state, publisherKey, (error, result) => {
if (!error) {
const verified = result.verified || false
const timestamp = result.timestamp || 0

appActions.onPublisherOptionUpdate(publisherKey, 'verified', verified)
appActions.onPublisherOptionUpdate(publisherKey, 'verifiedTimestamp', timestamp)
savePublisherOption(publisherKey, 'verified', verified)
savePublisherOption(publisherKey, 'verifiedTimestamp', timestamp)
appActions.onPublisherOptionUpdate(publisherKey, 'verified', result)
appActions.onPublisherOptionUpdate(publisherKey, 'verifiedTimestamp', lastUpdate)
savePublisherOption(publisherKey, 'verified', result)
savePublisherOption(publisherKey, 'verifiedTimestamp', lastUpdate)
}
})
}
Expand Down Expand Up @@ -2316,13 +2317,13 @@ const saveOptionSynopsis = (prop, value) => {
}

const savePublisherOption = (publisherKey, prop, value) => {
if (synopsis.publishers && synopsis.publishers[publisherKey]) {
if (synopsis && synopsis.publishers && synopsis.publishers[publisherKey] && synopsis.publishers[publisherKey].options) {
synopsis.publishers[publisherKey].options[prop] = value
}
}

const savePublisherData = (publisherKey, prop, value) => {
if (synopsis.publishers && synopsis.publishers[publisherKey]) {
if (synopsis && synopsis.publishers && synopsis.publishers[publisherKey]) {
synopsis.publishers[publisherKey][prop] = value
}
}
Expand Down Expand Up @@ -2505,8 +2506,12 @@ const getMethods = () => {
setSynopsis: (data) => {
synopsis = data
},
setClient: (data) => {
client = data
},
synopsisNormalizer,
pruneSynopsis
pruneSynopsis,
checkVerifiedStatus
}
}

Expand Down
49 changes: 49 additions & 0 deletions test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ describe('ledger api unit tests', function () {
},
busyP: function () {
return isBusy
},
publisherTimestamp: function () {
return 0
}
}
ledgerClient.prototype.boolion = function (value) { return false }
Expand Down Expand Up @@ -779,4 +782,50 @@ describe('ledger api unit tests', function () {
assert.deepEqual(result.toJS(), expectedResult)
})
})

describe('checkVerifiedStatus', function () {
let verifiedPSpy

before(function () {
verifiedPSpy = sinon.spy(ledgerApi, 'verifiedP')
ledgerApi.setClient({
publisherInfo: function () {
return false
}
})
})

after(function () {
verifiedPSpy.restore()
})

it('null case', function () {
const result = ledgerApi.checkVerifiedStatus(defaultAppState)
assert.deepEqual(result.toJS(), defaultAppState.toJS())
assert(verifiedPSpy.notCalled)
})

it('only update if timestamp is older then current', function () {
const newState = defaultAppState
.setIn(['ledger', 'publisherTimestamp'], 20)
.setIn(['ledger', 'synopsis', 'publishers', 'clifton.io', 'options', 'verifiedTimestamp'], 20)
const result = ledgerApi.checkVerifiedStatus(newState, 'clifton.io')
assert.deepEqual(result.toJS(), newState.toJS())
assert(verifiedPSpy.notCalled)
})

it('update when timestamp is older', function () {
const newState = defaultAppState
.setIn(['ledger', 'publisherTimestamp'], 20)
.setIn(['ledger', 'synopsis', 'publishers', 'clifton.io', 'options', 'verifiedTimestamp'], 10)

const expectedState = newState
.setIn(['ledger', 'about', 'synopsis'], [])
.setIn(['ledger', 'about', 'synopsisOptions'], {})
.setIn(['ledger', 'synopsis', 'publishers', 'clifton.io', 'options', 'verified'], true)
const result = ledgerApi.checkVerifiedStatus(newState, 'clifton.io')
assert.deepEqual(result.toJS(), expectedState.toJS())
assert(verifiedPSpy.calledOnce)
})
})
})
46 changes: 46 additions & 0 deletions test/unit/app/common/state/ledgerStateTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* 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 assert = require('assert')
const Immutable = require('immutable')

require('../../../braveUnit')
const ledgerState = require('../../../../../app/common/state/ledgerState')

const blankState = Immutable.fromJS({
ledger: {}
})

const stateWithData = Immutable.fromJS({
ledger: {
publisherTime: 1
}
})

describe('ledgerState unit test', function () {
describe('setLedgerValue', function () {
it('null case', function () {
const result = ledgerState.setLedgerValue(blankState)
assert.deepEqual(result.toJS(), blankState.toJS())
})

it('key is provided', function () {
const result = ledgerState.setLedgerValue(blankState, 'publisherTime', 1)
assert.deepEqual(result.toJS(), stateWithData.toJS())
})
})

describe('getLedgerValue', function () {
it('null case', function () {
const result = ledgerState.getLedgerValue(blankState)
assert.deepEqual(result, null)
})

it('key is provided', function () {
const result = ledgerState.getLedgerValue(stateWithData, 'publisherTime')
assert.deepEqual(result, 1)
})
})
})

0 comments on commit 4d745b6

Please sign in to comment.