2014-09-17 20:47:30 +04:00
|
|
|
ViewRegistry = require '../src/view-registry'
|
|
|
|
{View} = require '../src/space-pen-extensions'
|
|
|
|
|
|
|
|
describe "ViewRegistry", ->
|
|
|
|
registry = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
registry = new ViewRegistry
|
|
|
|
|
|
|
|
describe "::getView(object)", ->
|
|
|
|
describe "when passed a DOM node", ->
|
|
|
|
it "returns the given DOM node", ->
|
|
|
|
node = document.createElement('div')
|
|
|
|
expect(registry.getView(node)).toBe node
|
|
|
|
|
|
|
|
describe "when passed a SpacePen view", ->
|
2015-01-02 22:58:42 +03:00
|
|
|
it "returns the root node of the view with a .spacePenView property pointing at the SpacePen view", ->
|
2014-09-17 20:47:30 +04:00
|
|
|
class TestView extends View
|
|
|
|
@content: -> @div "Hello"
|
|
|
|
|
|
|
|
view = new TestView
|
|
|
|
node = registry.getView(view)
|
|
|
|
expect(node.textContent).toBe "Hello"
|
2015-01-02 22:58:42 +03:00
|
|
|
expect(node.spacePenView).toBe view
|
2014-09-17 20:47:30 +04:00
|
|
|
|
|
|
|
describe "when passed a model object", ->
|
2014-09-17 20:58:46 +04:00
|
|
|
describe "when a view provider is registered matching the object's constructor", ->
|
2014-12-02 04:53:03 +03:00
|
|
|
it "constructs a view element and assigns the model on it", ->
|
|
|
|
class TestModel
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
class TestModelSubclass extends TestModel
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
class TestView
|
|
|
|
initialize: (@model) -> this
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
model = new TestModel
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
registry.addViewProvider TestModel, (model) ->
|
|
|
|
new TestView().initialize(model)
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
view = registry.getView(model)
|
|
|
|
expect(view instanceof TestView).toBe true
|
|
|
|
expect(view.model).toBe model
|
2014-09-17 20:58:46 +04:00
|
|
|
|
2014-12-02 04:53:03 +03:00
|
|
|
subclassModel = new TestModelSubclass
|
|
|
|
view2 = registry.getView(subclassModel)
|
|
|
|
expect(view2 instanceof TestView).toBe true
|
|
|
|
expect(view2.model).toBe subclassModel
|
2014-09-20 01:25:35 +04:00
|
|
|
|
2014-09-17 20:47:30 +04:00
|
|
|
describe "when no view provider is registered for the object's constructor", ->
|
|
|
|
describe "when the object has a .getViewClass() method", ->
|
|
|
|
it "builds an instance of the view class with the model, then returns its root node with a __spacePenView property pointing at the view", ->
|
|
|
|
class TestView extends View
|
|
|
|
@content: (model) -> @div model.name
|
|
|
|
initialize: (@model) ->
|
|
|
|
|
|
|
|
class TestModel
|
|
|
|
constructor: (@name) ->
|
|
|
|
getViewClass: -> TestView
|
|
|
|
|
|
|
|
model = new TestModel("hello")
|
|
|
|
node = registry.getView(model)
|
|
|
|
|
|
|
|
expect(node.textContent).toBe "hello"
|
2015-01-02 22:58:42 +03:00
|
|
|
view = node.spacePenView
|
2014-09-17 20:47:30 +04:00
|
|
|
expect(view instanceof TestView).toBe true
|
|
|
|
expect(view.model).toBe model
|
|
|
|
|
|
|
|
# returns the same DOM node for repeated calls
|
|
|
|
expect(registry.getView(model)).toBe node
|
|
|
|
|
|
|
|
describe "when the object has no .getViewClass() method", ->
|
|
|
|
it "throws an exception", ->
|
|
|
|
expect(-> registry.getView(new Object)).toThrow()
|
2014-09-20 00:44:25 +04:00
|
|
|
|
|
|
|
describe "::addViewProvider(providerSpec)", ->
|
2014-09-20 01:20:35 +04:00
|
|
|
it "returns a disposable that can be used to remove the provider", ->
|
2014-09-20 00:44:25 +04:00
|
|
|
class TestModel
|
|
|
|
class TestView
|
2014-12-02 04:53:03 +03:00
|
|
|
initialize: (@model) -> this
|
|
|
|
|
|
|
|
disposable = registry.addViewProvider TestModel, (model) ->
|
|
|
|
new TestView().initialize(model)
|
2014-09-20 00:44:25 +04:00
|
|
|
|
|
|
|
expect(registry.getView(new TestModel) instanceof TestView).toBe true
|
|
|
|
disposable.dispose()
|
|
|
|
expect(-> registry.getView(new TestModel)).toThrow()
|
2015-02-20 00:26:39 +03:00
|
|
|
|
|
|
|
describe "::updateDocument(fn) and ::readDocument(fn)", ->
|
|
|
|
frameRequests = null
|
|
|
|
|
|
|
|
beforeEach ->
|
|
|
|
frameRequests = []
|
|
|
|
spyOn(window, 'requestAnimationFrame').andCallFake (fn) -> frameRequests.push(fn)
|
|
|
|
|
|
|
|
it "performs all pending writes before all pending reads on the next animation frame", ->
|
|
|
|
events = []
|
|
|
|
|
|
|
|
registry.updateDocument -> events.push('write 1')
|
|
|
|
registry.readDocument -> events.push('read 1')
|
|
|
|
registry.readDocument -> events.push('read 2')
|
|
|
|
registry.updateDocument -> events.push('write 2')
|
|
|
|
|
|
|
|
expect(events).toEqual []
|
|
|
|
|
|
|
|
expect(frameRequests.length).toBe 1
|
|
|
|
frameRequests[0]()
|
|
|
|
expect(events).toEqual ['write 1', 'write 2', 'read 1', 'read 2']
|
|
|
|
|
|
|
|
frameRequests = []
|
|
|
|
events = []
|
|
|
|
disposable = registry.updateDocument -> events.push('write 3')
|
|
|
|
registry.updateDocument -> events.push('write 4')
|
|
|
|
registry.readDocument -> events.push('read 3')
|
|
|
|
|
|
|
|
disposable.dispose()
|
|
|
|
|
|
|
|
expect(frameRequests.length).toBe 1
|
|
|
|
frameRequests[0]()
|
|
|
|
expect(events).toEqual ['write 4', 'read 3']
|
|
|
|
|
2015-04-21 06:59:13 +03:00
|
|
|
it "performs writes requested from read callbacks in the same animation frame", ->
|
|
|
|
spyOn(window, 'setInterval').andCallFake(fakeSetInterval)
|
|
|
|
spyOn(window, 'clearInterval').andCallFake(fakeClearInterval)
|
|
|
|
events = []
|
|
|
|
|
|
|
|
registry.pollDocument -> events.push('poll')
|
|
|
|
registry.pollAfterNextUpdate()
|
|
|
|
registry.updateDocument -> events.push('write 1')
|
|
|
|
registry.readDocument ->
|
|
|
|
registry.updateDocument -> events.push('write from read 1')
|
|
|
|
events.push('read 1')
|
|
|
|
registry.readDocument ->
|
|
|
|
registry.updateDocument -> events.push('write from read 2')
|
|
|
|
events.push('read 2')
|
|
|
|
registry.updateDocument -> events.push('write 2')
|
|
|
|
|
|
|
|
expect(frameRequests.length).toBe 1
|
|
|
|
frameRequests[0]()
|
|
|
|
expect(frameRequests.length).toBe 1
|
|
|
|
|
|
|
|
expect(events).toEqual [
|
|
|
|
'write 1'
|
|
|
|
'write 2'
|
|
|
|
'read 1'
|
|
|
|
'read 2'
|
|
|
|
'poll'
|
|
|
|
'write from read 1'
|
|
|
|
'write from read 2'
|
|
|
|
]
|
|
|
|
|
2015-02-20 00:26:39 +03:00
|
|
|
it "pauses DOM polling when reads or writes are pending", ->
|
|
|
|
spyOn(window, 'setInterval').andCallFake(fakeSetInterval)
|
|
|
|
spyOn(window, 'clearInterval').andCallFake(fakeClearInterval)
|
|
|
|
events = []
|
|
|
|
|
|
|
|
registry.pollDocument -> events.push('poll')
|
|
|
|
registry.updateDocument -> events.push('write')
|
|
|
|
registry.readDocument -> events.push('read')
|
|
|
|
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
2015-02-20 03:14:35 +03:00
|
|
|
expect(events).toEqual []
|
2015-02-20 00:26:39 +03:00
|
|
|
|
|
|
|
frameRequests[0]()
|
2015-02-20 03:14:35 +03:00
|
|
|
expect(events).toEqual ['write', 'read', 'poll']
|
2015-02-20 00:26:39 +03:00
|
|
|
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
2015-02-20 03:14:35 +03:00
|
|
|
expect(events).toEqual ['write', 'read', 'poll', 'poll']
|
2015-02-20 00:26:39 +03:00
|
|
|
|
2015-04-02 00:01:00 +03:00
|
|
|
it "polls the document after updating when ::pollAfterNextUpdate() has been called", ->
|
|
|
|
events = []
|
|
|
|
registry.pollDocument -> events.push('poll')
|
|
|
|
registry.updateDocument -> events.push('write')
|
|
|
|
registry.readDocument -> events.push('read')
|
|
|
|
frameRequests.shift()()
|
|
|
|
expect(events).toEqual ['write', 'read']
|
|
|
|
|
|
|
|
events = []
|
|
|
|
registry.pollAfterNextUpdate()
|
|
|
|
registry.updateDocument -> events.push('write')
|
|
|
|
registry.readDocument -> events.push('read')
|
|
|
|
frameRequests.shift()()
|
|
|
|
expect(events).toEqual ['write', 'read', 'poll']
|
|
|
|
|
2015-02-20 00:26:39 +03:00
|
|
|
describe "::pollDocument(fn)", ->
|
|
|
|
it "calls all registered reader functions on an interval until they are disabled via a returned disposable", ->
|
|
|
|
spyOn(window, 'setInterval').andCallFake(fakeSetInterval)
|
|
|
|
|
|
|
|
events = []
|
|
|
|
disposable1 = registry.pollDocument -> events.push('poll 1')
|
|
|
|
disposable2 = registry.pollDocument -> events.push('poll 2')
|
|
|
|
|
|
|
|
expect(events).toEqual []
|
|
|
|
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
|
|
|
expect(events).toEqual ['poll 1', 'poll 2']
|
|
|
|
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
|
|
|
expect(events).toEqual ['poll 1', 'poll 2', 'poll 1', 'poll 2']
|
|
|
|
|
|
|
|
disposable1.dispose()
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
|
|
|
expect(events).toEqual ['poll 1', 'poll 2', 'poll 1', 'poll 2', 'poll 2']
|
|
|
|
|
|
|
|
disposable2.dispose()
|
|
|
|
advanceClock(registry.documentPollingInterval)
|
|
|
|
expect(events).toEqual ['poll 1', 'poll 2', 'poll 1', 'poll 2', 'poll 2']
|