Skip to content

Commit

Permalink
include old test mocks, finish sidenav tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chase authored and shanzi committed Apr 12, 2015
1 parent 5ae014c commit a599d48
Show file tree
Hide file tree
Showing 6 changed files with 281 additions and 1 deletion.
7 changes: 6 additions & 1 deletion www/md_base/guanlecoja/config.coffee
Expand Up @@ -8,6 +8,7 @@ ANGULAR_MATERIAL_TAG = "~0.8.3"

path = require 'path'
gulp = require 'gulp'
shell = require("gulp-shell")
svgSymbols = require 'gulp-svg-symbols'

config =
Expand Down Expand Up @@ -54,7 +55,11 @@ config =
version: "~1.3.15"
files: "angular-mocks.js"

buildtasks: ['scripts', 'styles', 'index', 'icons', 'tests']
buildtasks: ['scripts', 'styles', 'index', 'icons', 'tests', 'generatedfixtures', 'fixtures']

generatedfixtures: ->
gulp.src ""
.pipe shell("buildbot dataspec -g window.dataspec -o " + path.join(config.dir.build,"generatedfixtures.js"))


gulp.task 'icons', ->
Expand Down
1 change: 1 addition & 0 deletions www/md_base/package.json
Expand Up @@ -2,6 +2,7 @@
"version": "0.1.0",
"devDependencies": {
"guanlecoja": "latest",
"gulp-shell": "^0.4.0",
"gulp-svg-symbols": "^0.3.1"
},
"name": "md-base",
Expand Down
58 changes: 58 additions & 0 deletions www/md_base/src/app/sidenav/sidenav.directive.spec.coffee
@@ -0,0 +1,58 @@
beforeEach module 'app'

describe 'MD: Sidebar directive', ->
$rootScope = $compile = $httpBackend = null

injected = ($injector) ->
$compile = $injector.get('$compile')
$rootScope = $injector.get('$rootScope')
$httpBackend = $injector.get('$httpBackend')
decorateHttpBackend($httpBackend)

beforeEach(inject(injected))

afterEach ->
$httpBackend.verifyNoOutstandingExpectation()
$httpBackend.verifyNoOutstandingRequest()

it 'should displays nav items', ->
$httpBackend.expectGETSVGIcons()
element = $compile('<div><sidenav items="items" current="current"></sidenav></div>')($rootScope)
$rootScope.$digest()
$httpBackend.flush()

sidenavEle = element.children().eq(0)

# sidenav contains a md-toolbar and a md-content
expect(sidenavEle.children().length).toBe(2)

# sidenav items is empty when not items specified
content = sidenavEle.children().eq(1)
expect(content.children().length).toBe(0)

# adding items
$rootScope.items = (
for n in [1..3]
name: 'testitem' + n
title: 'test item ' + n
icon: 'test-icon-' + n
)

$rootScope.$digest()

# there should be 3 nav items
expect(content.children().length).toBe(3)

for n in [1..3]
navitem = content.children().eq(n-1)
title = navitem.children().eq(1).text()
expect(title).toBe('test item ' + n)

# testing highlight
for i in [i..3]
$rootScope.current = 'testitem' + i
$rootScope.$digest()

for n in [1..3]
navitem = content.children().eq(n-1)
expect(navitem.hasClass('highlighted')).toBe(i == n)
1 change: 1 addition & 0 deletions www/md_base/test/scripts/mocks/config.mock.coffee
@@ -0,0 +1 @@
angular.module("app").constant("config", {title:"foo"})
28 changes: 28 additions & 0 deletions www/md_base/test/scripts/mocks/eventsource.mock.coffee
@@ -0,0 +1,28 @@
window.mockEventSource = ->
mocked = (url) ->
class EventSourceMock

constructor: (@url) ->
this.readyState = 1 # directly connect
this.onEvent = {}

addEventListener: (event, cb) ->
if not this.onEvent.hasOwnProperty(event)
this.onEvent[event] = []
this.onEvent[event].push(cb)

fakeEvent: (eventtype, event) ->
if this.onEvent.hasOwnProperty(eventtype)
for cb in this.onEvent[eventtype]
cb(event)

close: ->
this.readyState = 2

return new EventSourceMock(url)

# overrride "EventSource"
beforeEach module(($provide) ->
$provide.value("EventSource", mocked)
null # those module callbacks need to return null!
)
187 changes: 187 additions & 0 deletions www/md_base/test/scripts/mocks/http.mock.coffee
@@ -0,0 +1,187 @@
window.decorateHttpBackend = ($httpBackend) ->
ids = {}
getNextId = (namespace) ->
if not ids.hasOwnProperty(namespace)
ids[namespace] = 0
ids[namespace] += 1
return ids[namespace]

$httpBackend.epExample = (ep) ->
ep = ep.split("/")
for i in [0..ep.length - 1]
if ep[i].indexOf("n:") == 0
ep[i] = "1"
if ep[i].indexOf("i:") == 0
ep[i] = "id"
return ep.join("/")

$httpBackend.epRegexp = (ep) ->
ep = ep.split("/")
for i in [0..ep.length - 1]
if ep[i].indexOf("n:") == 0
ep[i] = "\\d+"
if ep[i].indexOf("i:") == 0
ep[i] = "[a-zA-Z_-][a-zA-Z0-9_-]*"
return RegExp("^"+ep.join("/")+"$")

$httpBackend.epLastPath = (path) ->
splitpath = path.split("/")
return splitpath[splitpath.length - 1]

$httpBackend.resetIds = ->
ids = {}

$httpBackend.buildDataValue = (ep, nItems) ->

valueFromBaseType = (spec, hint) ->
if spec.hasOwnProperty("fields")
return valueFromSpec(spec, hint)
hint ?= "mystring"
if not spec.name?
throw Error("no type: #{ JSON.stringify(spec) } #{ hint }")
type = spec.name
switch type
when "string"
return hint
when "binary"
return hint
when "identifier"
return hint + getNextId(hint)
when "integer"
return getNextId(hint)
when "boolean"
return false
when "jsonobject"
return {}
when "link"
return "http://link/link"
when "datetime"
return getNextId(hint)
when "sourced-properties"
return {prop: ['value', "source"]}
else
throw Error("unknown type: #{ type }")

valueFromSpec = (spec, basehint) ->
ret = {}
for field in spec.fields
hint = "my" + field.name
if field.name == "name"
hint = basehint
if field.type is "list"
ret[field.name] = [valueFromBaseType(field.type_spec.of, hint)]
else
ret[field.name] = valueFromBaseType(field.type_spec, hint)
return ret

hint = $httpBackend.epLastPath(ep).replace("n:","")
if not window.dataspec?
throw Error("dataspec is not available in test environment?!")
for dataEp in window.dataspec
dataEp.re ?= this.epRegexp(dataEp.path)
if dataEp.re.test(ep)
if nItems?
data = []
for i in [0..nItems - 1] by 1
data.push(valueFromBaseType(dataEp.type_spec, hint))
else
data = [valueFromBaseType(dataEp.type_spec, hint)]
ret = {meta:{links: [] }}
ret[dataEp.plural] = data
return ret
throw Error("endpoint not specified! #{ep}")

$httpBackend.whenDataGET = (ep, opts) ->
opts ?= {}
opts.when = true # use whenGetET instead of expectGET
return this.expectDataGET(ep, opts)
$httpBackend.expectDataGET = (ep, opts) ->
opts ?=
nItems: undefined # if nItems is defined, we will produce a collection
override: undefined # callback for overriding automaticly generated data
when: undefined # use whenGET instead of expectGET
ep_query = ep.split("?")
value = this.buildDataValue(this.epExample(ep_query[0]), opts.nItems)

if opts.override?
opts.override(value)
if opts.when?
this.whenGET(this.epRegexp("api/v2/" + ep)).respond(value)
else
this.expectGET("api/v2/" + ep).respond(value)
return null
$httpBackend.expectGETSVGIcons = (override) ->
override = '<svg><g id="test"></g></svg>'
this.expectGET('/icons/iconset.svg').respond(200, override)
$httpBackend.whenGETSVGIcons = (override) ->
override = '<svg><g id="test"></g></svg>'
this.whenGET('/icons/iconset.svg').respond(200, override)

if window.describe?
describe 'decorateHttpBackend', ->
$httpBackend = {}
injected = ($injector) ->
$httpBackend = $injector.get('$httpBackend')
decorateHttpBackend $httpBackend

beforeEach(inject(injected))

it 'should have correct endpoint matcher', ->
epMatch = (a,b) ->
re = $httpBackend.epRegexp(a)
return re.test(b)
expect(epMatch("change", "change")).toBe(true)
expect(epMatch("change/n:foo", "change/1")).toBe(true)
expect(epMatch("change/n:foo", "change/sd")).toBe(false)
expect(epMatch("change/foo/bar/n:foobar/foo", "change/foo/bar/1/foo")).toBe(true)
expect(epMatch("change/foo/bar/n:foobar/foo", "change/foo/bar/1/foo/")).toBe(false)

it 'should have correct value builder for change', ->
expected =
files: ['myfiles']
category: 'mycategory'
parent_changeids: [1]
repository: 'myrepository'
author: 'myauthor'
project: 'myproject'
comments: 'mycomments'
changeid: 1,
codebase: 'mycodebase'
branch: 'mybranch'
sourcestamp:
codebase: 'mycodebase'
ssid: 1
repository: 'myrepository'
created_at: 1
patch:
body: 'mybody'
comment: 'mycomment'
patchid: 1
level: 1
author: 'myauthor'
subdir: 'mysubdir'
project: 'myproject'
branch: 'mybranch'
revision: 'myrevision'
revision: 'myrevision'
revlink: 'myrevlink'
properties:
'prop': ['value', 'source']
when_timestamp: 1

value = $httpBackend.buildDataValue("changes").changes[0]
for k, v of value
expect(v).toEqual(expected[k])

$httpBackend.resetIds()
value = $httpBackend.buildDataValue("changes", 2).changes

# small hack to replace ones by twos for ids of the second change
expected = [expected, JSON.parse(JSON.stringify(expected).replace(/1/g,"2"))]
expect(value.length).toEqual(expected.length)
for i in [0..value.length - 1]
expect(value[i]).toEqual(expected[i])

it 'should have value builder not crash for all data spec cases', ->
for dataEp in window.dataspec
$httpBackend.buildDataValue($httpBackend.epExample(dataEp.path))

0 comments on commit a599d48

Please sign in to comment.