private _adapter, ES6 setters and getters

This commit is contained in:
Florent Vilmart
2016-02-22 14:12:51 -05:00
parent 23e55e941e
commit 045caca946
2 changed files with 56 additions and 17 deletions

View File

@@ -15,6 +15,11 @@ describe("AdaptableController", ()=>{
var adapter = new FilesAdapter();
var controller = new FilesController(adapter);
expect(controller.adapter).toBe(adapter);
// make sure _adapter is private
expect(controller._adapter).toBe(undefined);
// Override _adapter is not doing anything
controller._adapter = "Hello";
expect(controller.adapter).toBe(adapter);
done();
});
@@ -26,6 +31,17 @@ describe("AdaptableController", ()=>{
done();
});
it("should fail setting the wrong adapter to the controller", (done) => {
function WrongAdapter() {};
var adapter = new FilesAdapter();
var controller = new FilesController(adapter);
var otherAdapter = new WrongAdapter();
expect(() => {
controller.adapter = otherAdapter;
}).toThrow();
done();
});
it("should fail to instantiate a controller with wrong adapter", (done) => {
function WrongAdapter() {};
var adapter = new WrongAdapter();
@@ -41,4 +57,31 @@ describe("AdaptableController", ()=>{
}).toThrow();
done();
});
it("should accept an object adapter", (done) => {
var adapter = {
createFile: function(config, filename, data) { },
deleteFile: function(config, filename) { },
getFileData: function(config, filename) { },
getFileLocation: function(config, filename) { },
}
expect(() => {
new FilesController(adapter);
}).not.toThrow();
done();
});
it("should accept an object adapter", (done) => {
function AGoodAdapter() {};
AGoodAdapter.prototype.createFile = function(config, filename, data) { };
AGoodAdapter.prototype.deleteFile = function(config, filename) { };
AGoodAdapter.prototype.getFileData = function(config, filename) { };
AGoodAdapter.prototype.getFileLocation = function(config, filename) { };
var adapter = new AGoodAdapter();
expect(() => {
new FilesController(adapter);
}).not.toThrow();
done();
});
});

View File

@@ -8,26 +8,22 @@ based on the parameters passed
*/
// _adapter is private, use Symbol
var _adapter = Symbol();
export class AdaptableController {
/**
* Check whether the api call has master key or not.
* @param {options} the adapter options
* @param {defaultAdapter} the default adapter class or object to use
* @discussion
* Supported options types:
* - string: the options will be loaded with required, when loaded, if default
* is set on the returned object, we'll use that one to support modules
* - object: a plain javascript object (options.constructor === Object), if options.adapter is set, we'll try to load it with the same mechanics.
* - function: we'll create a new instance from that function, and pass the options object
*/
constructor(adapter, options) {
this.setAdapter(adapter, options);
constructor(adapter) {
this.adapter = adapter;
}
set adapter(adapter) {
this.validateAdapter(adapter);
this[_adapter] = adapter;
}
setAdapter(adapter, options) {
this.validateAdapter(adapter);
this.adapter = adapter;
this.options = options;
get adapter() {
return this[_adapter];
}
expectedAdapterType() {