Skip to content

Commit

Permalink
fix unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre Tardy <tardyp@gmail.com>
  • Loading branch information
tardyp committed Nov 5, 2015
1 parent 2ed8a1a commit 66f1c12
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
4 changes: 2 additions & 2 deletions www/data_module/guanlecoja/config.coffee
Expand Up @@ -14,7 +14,7 @@ gulp.task "publish", ['default'], ->
bower_json =
name: "buildbot-data"
version: "1.0.15"
main: ["scripts.js"]
main: ["buildbot-data.js"]
moduleType: [],
license: "MIT",
ignore: []
Expand Down Expand Up @@ -71,7 +71,7 @@ config =
files: 'lodash.js'
# as angular is a test deps, the tests need to be loaded first!
karma:
files: [ "tests.js", "scripts.js"]
files: [ "tests.js", "buildbot-data.js"]

ngclassify: (config) ->
return {
Expand Down
5 changes: 5 additions & 0 deletions www/data_module/src/data.config.coffee
Expand Up @@ -4,3 +4,8 @@ class HttpConfig extends Config
# of multiple http responses received at around
# the same time via $rootScope.$applyAsync
$httpProvider.useApplyAsync(true)


class Dataconfig extends Constant
constructor: ->
return enableIndexedDB: false
13 changes: 7 additions & 6 deletions www/data_module/src/services/indexeddb/indexeddb.service.coffee
@@ -1,8 +1,7 @@
class IndexedDB extends Service
constructor: ($log, $injector, $q, $window, config, $timeout, dataUtilsService, DBSTORES, SPECIFICATION) ->
constructor: ($log, $injector, $q, $window, DATACONFIG, $timeout, dataUtilsService, DBSTORES, SPECIFICATION) ->
return new class IndexedDBService
constructor: ->
# config.enableIndexedDB = true
@db = new $window.Dexie('BBCache')
version = $window.localStorage.getItem('BBCacheVERSION')
# just recreate in case of old version in the browser
Expand Down Expand Up @@ -48,19 +47,21 @@ class IndexedDB extends Service

put: (table, value) ->
table = @getTable(table)
if config.enableIndexedDB
if DATACONFIG.enableIndexedDB
element = { }
for k, v of value
if not angular.isObject(v)
if angular.isObject(v)
element[k] = angular.toJson(v)
else
element[k] = v
return table.put(element)
else
table.cache[value[table.schema.primKey.keyPath]] = value
return $q.when()

getObject: (table, id) ->
table = @getTable(table)
if config.enableIndexedDB
if DATACONFIG.enableIndexedDB
return table.get(id)
if not table.cache.hasOwnProperty(id)
table.cache[id] = {}
Expand Down Expand Up @@ -131,7 +132,7 @@ class IndexedDB extends Service
return false

native_filter: (table, query) ->
if not config.enableIndexedDB
if not DATACONFIG.enableIndexedDB
return $.when([_.values(table.cache), query])

$q (resolve, reject) =>
Expand Down
4 changes: 2 additions & 2 deletions www/data_module/src/services/tabex/tabex.service.coffee
@@ -1,5 +1,5 @@
class Tabex extends Service
constructor: ($log, $window, $q, config, $timeout, socketService, restService, dataUtilsService, indexedDBService, SPECIFICATION) ->
constructor: ($log, $window, $q, DATACONFIG, $timeout, socketService, restService, dataUtilsService, indexedDBService, SPECIFICATION) ->
return new class TabexService
CHANNELS =
MASTER: '!sys.master'
Expand Down Expand Up @@ -132,7 +132,7 @@ class Tabex extends Service
t = dataUtilsService.type(path)
specification = @getSpecification(t)
# test if cached and active
if not config.enableIndexedDB
if not DATACONFIG.enableIndexedDB
dbPaths = []
for dbPath in dbPaths
dbPath.query = angular.fromJson(dbPath.query)
Expand Down
16 changes: 10 additions & 6 deletions www/data_module/src/services/tabex/tabex.service.spec.coffee
Expand Up @@ -25,16 +25,20 @@ describe 'Tabex service', ->
if angular.isArray(@channels[c]) and self
for l in @channels[c]
if angular.isFunction(l) then l(m)
class DbMock
put: -> $q.resolve()
schema: {primKey: 'x'}

tabexService = socketService = indexedDBService = restService = undefined
tabexService = socketService = indexedDBService = restService = DATACONFIG = undefined
$window = $q = $rootScope = $timeout = undefined
clientMock = new ClientMock()
injected = ($injector) ->
$window = $injector.get('$window')
$q = $injector.get('$q')
$rootScope = $injector.get('$rootScope')
$timeout = $injector.get('$timeout')

DATACONFIG = $injector.get('DATACONFIG')
DATACONFIG.enableIndexedDB = true
spyOn($window.tabex, 'client').and.returnValue(clientMock)

tabexService = $injector.get('tabexService')
Expand Down Expand Up @@ -159,7 +163,7 @@ describe 'Tabex service', ->
describe 'messageHandler(key, message)', ->

it 'should update the object in the indexedDB', ->
indexedDBService.db = 'bsd': put: ->
indexedDBService.db = 'bsd': new DbMock()
spyOn(indexedDBService.db['bsd'], 'put').and.returnValue($q.resolve())
expect(indexedDBService.db['bsd'].put).not.toHaveBeenCalled()
message = bsd: 1
Expand All @@ -168,8 +172,8 @@ describe 'Tabex service', ->

it 'should emit update events for matching paths', ->
indexedDBService.db =
asd: put: -> $q.resolve()
bsd: put: -> $q.resolve()
asd: new DbMock()
bsd: new DbMock()

spyOn(tabexService, 'activatePaths').and.returnValue($q.resolve())
spyOn(tabexService, 'startConsumingAll').and.returnValue($q.resolve())
Expand All @@ -195,7 +199,7 @@ describe 'Tabex service', ->

it 'should emit update events for logs if the path contains `log`', ->
indexedDBService.db =
logs: put: -> $q.resolve()
logs: new DbMock()

spyOn(tabexService, 'activatePaths').and.returnValue($q.resolve())
spyOn(tabexService, 'startConsumingAll').and.returnValue($q.resolve())
Expand Down

0 comments on commit 66f1c12

Please sign in to comment.