Mailspring/app/spec/database-object-registry-spec.es6

40 lines
1.4 KiB
Text
Raw Normal View History

/* eslint quote-props: 0 */
import _ from 'underscore';
import Model from '../src/flux/models/model';
import Attributes from '../src/flux/attributes';
import DatabaseObjectRegistry from '../src/registries/database-object-registry';
class GoodTest extends Model {
static attributes = Object.assign({}, Model.attributes, {
2017-09-27 02:33:08 +08:00
foo: Attributes.String({
modelKey: 'foo',
jsonKey: 'foo',
}),
});
}
describe('DatabaseObjectRegistry', function DatabaseObjectRegistrySpecs() {
2017-09-27 02:33:08 +08:00
beforeEach(() => DatabaseObjectRegistry.unregister('GoodTest'));
2017-09-27 02:33:08 +08:00
it('can register constructors', () => {
const testFn = () => GoodTest;
2017-09-27 02:33:08 +08:00
expect(() => DatabaseObjectRegistry.register('GoodTest', testFn)).not.toThrow();
expect(DatabaseObjectRegistry.get('GoodTest')).toBe(GoodTest);
});
2017-09-27 02:33:08 +08:00
it('Tests if a constructor is in the registry', () => {
DatabaseObjectRegistry.register('GoodTest', () => GoodTest);
expect(DatabaseObjectRegistry.isInRegistry('GoodTest')).toBe(true);
});
2017-09-27 02:33:08 +08:00
it('deserializes the objects for a constructor', () => {
DatabaseObjectRegistry.register('GoodTest', () => GoodTest);
const obj = DatabaseObjectRegistry.deserialize('GoodTest', { foo: 'bar' });
expect(obj instanceof GoodTest).toBe(true);
2017-09-27 02:33:08 +08:00
expect(obj.foo).toBe('bar');
});
it("throws an error if the object can't be deserialized", () =>
2017-09-27 02:33:08 +08:00
expect(() => DatabaseObjectRegistry.deserialize('GoodTest', { foo: 'bar' })).toThrow());
});