Options, specs

This commit is contained in:
mourner 2010-09-06 16:58:06 +03:00
parent 36f7ada676
commit 14bd27b088
3 changed files with 53 additions and 0 deletions

View File

@ -18,6 +18,7 @@
<script type="text/javascript" src="../src/core/Util.js"></script>
<script type="text/javascript" src="../src/core/Class.js"></script>
<script type="text/javascript" src="../src/core/Events.js"></script>
<script type="text/javascript" src="../src/core/Options.js"></script>
<!-- /geometry -->
<script type="text/javascript" src="../src/geometry/Point.js"></script>
@ -31,6 +32,7 @@
<script type="text/javascript" src="suites/core/UtilSpec.js"></script>
<script type="text/javascript" src="suites/core/ClassSpec.js"></script>
<script type="text/javascript" src="suites/core/EventsSpec.js"></script>
<script type="text/javascript" src="suites/core/OptionsSpec.js"></script>
<!-- /geometry -->
<script type="text/javascript" src="suites/geometry/PointSpec.js"></script>

View File

@ -0,0 +1,37 @@
describe('Options', function() {
var Klass;
beforeEach(function() {
Klass = L.Class.extend({
options: {
foo1: 1,
foo2: 2
},
includes: [L.Mixin.Options]
});
});
describe('#setOptions', function() {
it('should set the given options, overwriting the default ones', function() {
var a = new Klass();
expect(a.options).toEqual({foo1: 1, foo2: 2});
a.setOptions({foo2: 3, foo3: 4});
expect(a.options).toEqual({foo1: 1, foo2: 3, foo3: 4});
});
it('should fire optionschange event if Options module is availalbe', function() {
Klass.include(L.Mixin.Events);
var a = new Klass(),
spy = jasmine.createSpy('optionschange handler');
a.on('optionschange', spy);
a.setOptions({foo1: 3});
expect(spy).toHaveBeenCalled();
});
});
});

14
src/core/Options.js Normal file
View File

@ -0,0 +1,14 @@
/*
* L.Mixin.Options adds a small shortcut for options handling
*/
L.Mixin.Options = {
setOptions: function(/*Object*/ options) {
this.options = this.options || {};
L.Util.extend(this.options, options);
if (this.fireEvent) {
this.fireEvent('optionschange');
}
}
};