Mailspring/spec-nylas/database-object-registry-spec.coffee
Evan Morikawa d2b979e716 refactor(models): Enables 3rd party plugins to add Models & Tasks
Summary:
Adding serialzable registry

Added DatabaseObjectRegistry

rename modelReviver to deserializeObject

Consolidate deserizlie

Get rid of model methods from Utils

DatabaseRegistry change notifications

Logic to throttle database refresh requests

Fixes in nylas-exports

Silent model setup

Continue to resolving the database setup phase for non main windows.

A packages `activate` method does not actually get called until the
DatabaseStore says that it's ready. This is necessary to ensure that a
package that introduces database changes has those schema changes take
hold before the activate happens.

However, in windows like the `onboarding` window that do not depend on a
database at all, there is no setup that is run and the promise use to
never resolve thereby making the packages never activate.

In this case, any external windows will go ahead and let their packages
activate.

Check subclass instead of instance!

Use the correct types for "messages" and "drafts"

Move Salesforce models

Proper references to Model and Attributes

Convert Salesforce stores to NylasStores and fix paths

Move database setup to DB

Test Plan: todo

Reviewers: bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D1899
2015-08-19 16:25:56 -07:00

46 lines
1.7 KiB
CoffeeScript

_ = require 'underscore'
Model = require '../src/flux/models/model'
Attributes = require '../src/flux/attributes'
DatabaseObjectRegistry = require '../src/database-object-registry'
class BadTest
class GoodTest extends Model
@attributes: _.extend {}, Model.attributes,
"foo": Attributes.String
modelKey: 'foo'
jsonKey: 'foo'
describe 'DatabaseObjectRegistry', ->
beforeEach ->
DatabaseObjectRegistry.unregister("GoodTest")
it "throws an error if the constructor isn't a Model", ->
expect( -> DatabaseObjectRegistry.register()).toThrow()
expect( -> DatabaseObjectRegistry.register(BadTest)).toThrow()
it "can register constructors", ->
expect( -> DatabaseObjectRegistry.register(GoodTest)).not.toThrow()
expect(DatabaseObjectRegistry._constructors["GoodTest"]).toBe GoodTest
it "Retrurns a map of constructors", ->
DatabaseObjectRegistry.register(GoodTest)
map = DatabaseObjectRegistry.classMap()
expect(map.GoodTest).toBe GoodTest
it "Tests if a constructor is in the registry", ->
DatabaseObjectRegistry.register(GoodTest)
expect(DatabaseObjectRegistry.isInRegistry("GoodTest")).toBe true
it "deserializes the objects for a constructor", ->
DatabaseObjectRegistry.register(GoodTest)
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()
it "returns the original object in deserialization failure if option is passed", ->
json = DatabaseObjectRegistry.deserialize("GoodTest", {foo: "bar"}, {ignoreError: true})
expect(json).toEqual foo: "bar"