2015-08-20 07:25:56 +08:00
|
|
|
_ = require 'underscore'
|
|
|
|
Model = require '../src/flux/models/model'
|
2016-09-22 00:40:06 +08:00
|
|
|
Attributes = require('../src/flux/attributes').default
|
2016-05-04 07:42:28 +08:00
|
|
|
DatabaseObjectRegistry = require('../src/database-object-registry').default
|
2015-08-20 07:25:56 +08:00
|
|
|
|
|
|
|
class GoodTest extends Model
|
|
|
|
@attributes: _.extend {}, Model.attributes,
|
|
|
|
"foo": Attributes.String
|
|
|
|
modelKey: 'foo'
|
|
|
|
jsonKey: 'foo'
|
|
|
|
|
|
|
|
describe 'DatabaseObjectRegistry', ->
|
|
|
|
beforeEach ->
|
|
|
|
DatabaseObjectRegistry.unregister("GoodTest")
|
|
|
|
|
|
|
|
it "can register constructors", ->
|
2016-04-23 04:30:42 +08:00
|
|
|
testFn = -> GoodTest
|
|
|
|
expect( -> DatabaseObjectRegistry.register("GoodTest", testFn)).not.toThrow()
|
|
|
|
expect(DatabaseObjectRegistry.get("GoodTest")).toBe GoodTest
|
2015-08-20 07:25:56 +08:00
|
|
|
|
|
|
|
it "Tests if a constructor is in the registry", ->
|
2016-04-23 04:30:42 +08:00
|
|
|
DatabaseObjectRegistry.register("GoodTest", -> GoodTest)
|
2015-08-20 07:25:56 +08:00
|
|
|
expect(DatabaseObjectRegistry.isInRegistry("GoodTest")).toBe true
|
|
|
|
|
|
|
|
it "deserializes the objects for a constructor", ->
|
2016-04-23 04:30:42 +08:00
|
|
|
DatabaseObjectRegistry.register("GoodTest", -> GoodTest)
|
2015-08-20 07:25:56 +08:00
|
|
|
obj = DatabaseObjectRegistry.deserialize("GoodTest", foo: "bar")
|
|
|
|
expect(obj instanceof GoodTest).toBe true
|
|
|
|
expect(obj.foo).toBe "bar"
|
|
|
|
|
|
|
|
it "throws an error if the object can't be deserialized", ->
|
|
|
|
expect( -> DatabaseObjectRegistry.deserialize("GoodTest", foo: "bar")).toThrow()
|