Permalink
Browse files

Page test are refactored unused functions removed and naming is left …

…for component instead of being set by page, all this crap is brought to you by XaV1Uzz
  • Loading branch information...
1 parent 2d8aa46 commit 4be674878833dc7cedb3cbf2c563cea45ef15a5a @xaviuzz xaviuzz committed Mar 20, 2012
Showing with 74 additions and 111 deletions.
  1. +49 −74 spec/Cuore.Page.Spec.js
  2. +25 −37 src/Cuore.Page.js
View
@@ -1,143 +1,118 @@
describe("Page", function() {
- var aPage, registry, directory;
+ var aPage;
beforeEach(function() {
aPage = new CUORE.Page();
- registry = CUORE.Mocks.Registry();
- directory = CUORE.Mocks.Directory();
- aPage.setDirectory(directory);
- aPage.setRegistry(registry);
});
- it("initialization calls initializeServices", function() {
- var initializeServicesCalled = false;
-
- aPage.initializeServices = function() {
- initializeServicesCalled = true;
- };
-
+ it("calls initializeServices at instanciation", function() {
+ spyOn(aPage, "initializeServices");
aPage.init();
-
- expect(initializeServicesCalled).toBeTruthy();
+ expect(aPage.initializeServices).toHaveBeenCalled();
});
- it("initialization calls initializeComponents", function() {
- var initializeComponentsCalled = false;
-
- aPage.initializeComponents = function() {
- initializeComponentsCalled = true;
- };
-
+ it("calls initializeComponents at instanciation", function() {
+ spyOn(aPage, "initializeComponents");
aPage.init();
-
- expect(initializeComponentsCalled).toBeTruthy();
+ expect(aPage.initializeComponents).toHaveBeenCalled();
});
it("should set and retrieve states by key", function() {
- var retrievedState = undefined;
var key = 'arbitraryKey';
var savedState = 'savedState';
-
+
aPage.save(key, savedState);
- retrievedState = aPage.retrieve(key);
-
+ var retrievedState = aPage.retrieve(key);
+
expect(retrievedState).toEqual(savedState);
});
+ describe("uses a Directory for managing services", function() {
+ var directory;
- it("can lookup for services by name", function() {
- var aServiceName = "a service to be looked up";
- var expectedService = "resulting service";
- directory.getService.andReturn(expectedService);
+ beforeEach(function() {
+ directory = CUORE.Mocks.Directory();
+ aPage.setDirectory(directory);
+ });
- var aService = aPage.getService(aServiceName);
+ it("can lookup for services by name", function() {
+ var aServiceName = "a service to be looked up";
+ var expectedService = "resulting service";
+ directory.getService.andReturn(expectedService);
- expect(directory.getService).toHaveBeenCalledWith(aServiceName);
- expect(aService).toEqual(expectedService);
- });
+ var aService = aPage.getService(aServiceName);
- it("when baseURL is configured, it informs the directory", function() {
- var baseURL = "A Base URL";
- aPage = new CUORE.Page(baseURL);
- aPage.setDirectory(directory);
+ expect(directory.getService).toHaveBeenCalledWith(aServiceName);
+ expect(aService).toEqual(expectedService);
+ });
- expect(directory.setBaseURL).toHaveBeenCalledWith(baseURL);
- });
+ it("when baseURL is configured, it informs the directory", function() {
+ var baseURL = "A Base URL";
+ aPage = new CUORE.Page(baseURL);
+ aPage.setDirectory(directory);
- it("when a service is added then adds the service to the directory", function() {
- var aService = CUORE.Mocks.Service();
- aPage.setDirectory(directory);
+ expect(directory.setBaseURL).toHaveBeenCalledWith(baseURL);
+ });
+
+ it("when a service is added then adds the service to the directory", function() {
+ var aService = CUORE.Mocks.Service();
+ aPage.setDirectory(directory);
+ aPage.addService(aService);
- aPage.addService(aService);
+ expect(directory.add).toHaveBeenCalledWith(aService);
+ });
- expect(directory.add).toHaveBeenCalledWith(aService);
});
+
describe("when a component is added", function() {
var testingContainer = "testingContainer",
- aComponent;
+ aComponent, registry;
beforeEach(function() {
+ registry = CUORE.Mocks.Registry();
+ aPage.setRegistry(registry);
aComponent = CUORE.Mocks.Component('fake');
}),
it("registers component with the registry", function() {
aPage.addComponent(aComponent, testingContainer, true);
-
expect(registry.register).toHaveBeenCalledWith(aComponent);
});
it("configures the component with the service directory", function() {
+ var aDirectory = CUORE.Mocks.Directory();
+ aPage.setDirectory(aDirectory);
aPage.addComponent(aComponent, testingContainer, true);
-
- expect(aComponent.setDirectory).toHaveBeenCalledWith(directory);
+ expect(aComponent.setDirectory).toHaveBeenCalledWith(aDirectory);
});
it("configures the component with a container", function() {
aPage.addComponent(aComponent, testingContainer, true);
-
expect(aComponent.setContainer).toHaveBeenCalledWith(testingContainer);
});
- it("gives the component a name", function() {
- aPage.addComponent(aComponent, testingContainer, true);
-
- expect(aComponent.setName).toHaveBeenCalled();
- });
-
- it("gives the component an unique name", function() {
- var otherComponent = CUORE.Mocks.Component('other component');
-
- aPage.addComponent(aComponent, testingContainer, true);
- aPage.addComponent(otherComponent, testingContainer, true);
-
- expect(otherComponent.getName()).not.toEqual(aComponent.getName());
- });
-
-
it("configures component with the Injecting behaviour provided", function() {
aPage.addComponent(aComponent, testingContainer, CUORE.Behaviours.REPLACE);
-
expect(aComponent.behave).toHaveBeenCalledWith(CUORE.Behaviours.REPLACE);
});
-
+
it("lets component behaviour untouched by default", function() {
aPage.addComponent(aComponent, testingContainer);
expect(aComponent.behave).not.toHaveBeenCalled();
});
-
- it("calls component onEnvinromentUp after bus registering", function() {
+
+ it("calls component onEnvinromentUp after been register in the page", function() {
var container = null;
aComponent.setContainer = jasmine.createSpy('setContainer');
- aComponent.onEnvironmentUp=function(){
+ aComponent.onEnvironmentUp = function() {
expect(aComponent.setContainer).toHaveBeenCalledWith(testingContainer);
}
-
aPage.addComponent(aComponent, testingContainer);
});
- it("and the page is drawn, it will draw each component", function() {
+ it("and when the page is drawn, it will call draw in each component", function() {
registry.each.andCallFake(function(callback) {
callback(aComponent);
});
View
@@ -9,40 +9,18 @@ CUORE.Page = CUORE.Class(null, {
this.setUp();
},
- setRegistry:function(registry) {
- this.components = registry;
- },
-
- _getBaseURL:function() {
- return this.baseURL;
- },
-
- setDirectory:function(directory) {
- this.services = directory;
- this.services.setBaseURL(this._getBaseURL());
- },
-
- initializeServices: function() {},
- initializeComponents: function() {},
-
setUp: function() {
this.initializeServices();
this.initializeComponents();
},
- addService: function(service) {
- this.services.add(service);
- },
-
- getService: function(name) {
- return this.services.getService(name);
- },
+ initializeServices: function() {},
+ initializeComponents: function() {},
addComponent: function(component, container, behaviour) {
- component.setName(this._generateUUID());
component.setDirectory(this.services);
-
- if(behaviour) component.behave(behaviour);
+
+ if (behaviour) component.behave(behaviour);
this.components.register(component);
component.setContainer(container);
@@ -54,21 +32,31 @@ CUORE.Page = CUORE.Class(null, {
component.draw();
});
},
-
- save: function(key,value) {
- this.state.save(key,value);
+
+ addService: function(service) {
+ this.services.add(service);
+ },
+
+ getService: function(name) {
+ return this.services.getService(name);
},
-
+ save: function(key, value) {
+ this.state.save(key, value);
+ },
+
retrieve: function(key) {
return this.state.retrieve(key);
},
-
- _getBus: function() {
- return CUORE.Bus;
+
+ setRegistry: function(registry) {
+ this.components = registry;
},
-
- _generateUUID: function() {
- return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
- }
+
+ setDirectory: function(directory) {
+ this.services = directory;
+ this.services.setBaseURL(this.baseURL);
+ },
+
+
});

0 comments on commit 4be6748

Please sign in to comment.