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 Feb 20, 2018
1 parent 58af36a commit dd42a4d
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 16 deletions.
22 changes: 18 additions & 4 deletions app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ const getMediaId = (data, type) => {
}
case ledgerMediaProviders.TWITCH:
{
if (data.event === 'minute-watched') {
if (data.event === 'minute-watched' && data.properties) {
id = data.properties.channel
let vod = data.properties.vod

Expand Down Expand Up @@ -271,15 +271,22 @@ const getMediaData = (xhr, type) => {
{
result = queryString.parse(parsedUrl.query)
if (!result.data) {
result = null
break
}

let obj = Buffer.from(result.data, 'base64').toString('utf8')
if (obj == null) {
result = null
break
}

result = JSON.parse(obj)
try {
result = JSON.parse(obj)
} catch (error) {
result = null
console.error(error.toString())
}
break
}
}
Expand Down Expand Up @@ -308,6 +315,12 @@ const getMediaDuration = (data, type) => {
const getMediaFavicon = (providerName) => {
let image = null

if (!providerName) {
return image
}

providerName = providerName.toLowerCase()

switch (providerName) {
case ledgerMediaProviders.YOUTUBE:
{
Expand All @@ -331,7 +344,7 @@ const getTwitchDuration = (data) => {
return time
}

if (data.properties.minutes_logged) {
if (data.properties && data.properties.minutes_logged) {
time = 60 * 1000 // 1 min
}

Expand Down Expand Up @@ -426,7 +439,8 @@ const getMethods = () => {

if (process.env.NODE_ENV === 'test') {
privateMethods = {
getYouTubeDuration
getYouTubeDuration,
getTwitchDuration
}
}

Expand Down
159 changes: 147 additions & 12 deletions test/unit/app/common/lib/ledgerUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('ledgerUtil unit test', function () {
mockery.registerMock('electron', fakeElectron)
mockery.registerMock('ad-block', fakeAdBlock)
mockery.registerMock('level', fakeLevel)
mockery.registerMock('../../../img/mediaProviders/youtube.png', 'youtube.png')
mockery.registerMock('../../../img/mediaProviders/twitch.svg', 'twitch.svg')

mockery.registerMock('../../../js/settings', {
getSetting: (settingKey) => {
Expand Down Expand Up @@ -395,6 +397,48 @@ describe('ledgerUtil unit test', function () {
assert.equal(result, 'kLiLOkzLetE')
})
})

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

it('event is not correct', function () {
const result = ledgerUtil.getMediaId({
event: 'wrong'
}, ledgerMediaProviders.TWITCH)
assert.equal(result, null)
})

it('properties are missing', function () {
const result = ledgerUtil.getMediaId({
event: 'minute-watched'
}, ledgerMediaProviders.TWITCH)
assert.equal(result, null)
})

it('content is a live stream', function () {
const result = ledgerUtil.getMediaId({
event: 'minute-watched',
properties: {
channel: 'tchannel'
}
}, ledgerMediaProviders.TWITCH)
assert.equal(result, 'tchannel')
})

it('content is a vod', function () {
const result = ledgerUtil.getMediaId({
event: 'minute-watched',
properties: {
channel: 'tchannel',
vod: 'v12343234'
}
}, ledgerMediaProviders.TWITCH)
assert.equal(result, 'tchannel_vod_12343234')
})
})
})

describe('getMediaKey', function () {
Expand All @@ -408,14 +452,16 @@ describe('ledgerUtil unit test', function () {
assert.equal(result, null)
})

it('id is null', function () {
const result = ledgerUtil.getMediaKey(null, ledgerMediaProviders.YOUTUBE)
assert.equal(result, null)
})
describe('YouTube', function () {
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')
it('data is ok', function () {
const result = ledgerUtil.getMediaKey('kLiLOkzLetE', ledgerMediaProviders.YOUTUBE)
assert.equal(result, 'youtube_kLiLOkzLetE')
})
})
})

Expand All @@ -430,17 +476,17 @@ describe('ledgerUtil unit test', function () {
assert.equal(result, null)
})

it('query is not present', function () {
const result = ledgerUtil.getMediaData('https://youtube.com', ledgerMediaProviders.YOUTUBE)
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, {
Expand All @@ -450,6 +496,33 @@ describe('ledgerUtil unit test', function () {
})
})
})

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

it('data is missing', function () {
const result = ledgerUtil.getMediaData('https://api.mixpanel.com', ledgerMediaProviders.TWITCH)
assert.equal(result, null)
})

it('data is empty string', function () {
const result = ledgerUtil.getMediaData('https://api.mixpanel.com?data=', ledgerMediaProviders.TWITCH)
assert.equal(result, null)
})

it('obj is parsed correctly', function () {
const result = ledgerUtil.getMediaData('https://api.mixpanel.com?data=eyJldmVudCI6Im1pbnV0ZS13YXRjaGVkIiwicHJvcGVydGllcyI6eyJjaGFubmVsIjoidHcifX0=', ledgerMediaProviders.TWITCH)
assert.deepEqual(result, {
event: 'minute-watched',
properties: {
channel: 'tw'
}
})
})
})
})

describe('getYouTubeDuration', function () {
Expand All @@ -475,6 +548,27 @@ describe('ledgerUtil unit test', function () {
})
})

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

it('properties is missing', function () {
const result = ledgerUtil.getTwitchDuration({})
assert.equal(result, 0)
})

it('minutes_logged is present', function () {
const result = ledgerUtil.getTwitchDuration({
properties: {
minutes_logged: 1
}
})
assert.equal(result, 60000)
})
})

describe('getMediaProvider', function () {
it('null case', function () {
const result = ledgerUtil.getMediaProvider()
Expand All @@ -490,6 +584,30 @@ describe('ledgerUtil unit test', function () {
const result = ledgerUtil.getMediaProvider('https://www.youtube.com/api/stats/watchtime?docid=kLiLOkzLetE&st=11.338&et=21.339')
assert.equal(result, ledgerMediaProviders.YOUTUBE)
})

describe('twitch', function () {
it('we only have url', function () {
const result = ledgerUtil.getMediaProvider('https://api.mixpanel.com?data=lll')
assert.equal(result, null)
})

it('video is on twitch.tv', function () {
const result = ledgerUtil.getMediaProvider(
'https://api.mixpanel.com?data=lll',
'https://www.twitch.tv'
)
assert.equal(result, ledgerMediaProviders.TWITCH)
})

it('video is embeded', function () {
const result = ledgerUtil.getMediaProvider(
'https://api.mixpanel.com?data=lll',
'https://www.site.tv',
'https://player.twitch.tv'
)
assert.equal(result, ledgerMediaProviders.TWITCH)
})
})
})

describe('milliseconds', function () {
Expand Down Expand Up @@ -529,4 +647,21 @@ describe('ledgerUtil unit test', function () {
assert.deepEqual(ledgerUtil.defaultMonthlyAmounts.toJS(), [5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0])
})
})

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

it('youtube', function () {
const result = ledgerUtil.getMediaFavicon('YouTube')
assert.equal(result, 'youtube.png')
})

it('twitch', function () {
const result = ledgerUtil.getMediaFavicon('Twitch')
assert.equal(result, 'twitch.svg')
})
})
})

0 comments on commit dd42a4d

Please sign in to comment.