Skip to content

Commit 6f80da3

Browse files
committed
Added tests, reverted accidental commit #208
1 parent 787026b commit 6f80da3

File tree

5 files changed

+123
-19
lines changed

5 files changed

+123
-19
lines changed

grails-app/assets/javascripts/forms-knockout-bindings.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -1165,14 +1165,13 @@
11651165
var dataLoader = new ecodata.forms.dataLoader(target.context, target.config);
11661166
var dataLoaderConfig = target.get('computed');
11671167
if (!dataLoaderConfig) {
1168-
throw "This extender can only be used with the metadata extender";
1168+
throw "This extender can only be used with the metadata extender and expects a computed property to be defined";
11691169
}
11701170
var dependencyTracker = ko.computed(function () {
11711171
return dataLoader.prepop(dataLoaderConfig).done( function(data) {
11721172
target(data);
11731173
});
1174-
});
1175-
//dependencyTracker.subscribe(function() { console.log("bananas")})
1174+
}); // This is a computed rather than a pureComputed as it has a side effect.
11761175
return target;
11771176
}
11781177

grails-app/assets/javascripts/forms.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -860,7 +860,6 @@ function orEmptyArray(v) {
860860
}
861861
}
862862
else if (metadata.constraints.type == 'pre-populated') {
863-
console.log("******************** Encountered pre-pop constraints for "+self.getName()+"***************************************")
864863
var defaultConstraints = metadata.constraints.defaults || [];
865864
var constraintsObservable = ko.observableArray(defaultConstraints);
866865
if (!includeExcludeDefined) {
@@ -915,14 +914,14 @@ function orEmptyArray(v) {
915914
self.displayOptions = metadata.displayOptions;
916915
}
917916
self.load = function(data) {
918-
console.log("Loading data for "+self.getName()+" : "+data)
919-
self(data);
920917
if (constraintsInititaliser) {
921918
constraintsInititaliser.always(function() {
922-
console.log("Re-Loading data for "+self.getName()+" : "+data)
923919
self(data);
924920
})
925921
}
922+
else {
923+
self(data);
924+
}
926925

927926
}
928927
};

grails-app/assets/javascripts/knockout-utils.js

+2-12
Original file line numberDiff line numberDiff line change
@@ -91,16 +91,10 @@
9191
return (typeof root.modelAsJSON === 'function') ? root.modelAsJSON() : ko.toJSON(root);
9292
};
9393
var _initialState = ko.observable(getRepresentation());
94-
console.log("****************** Initial state ******************")
95-
console.log( _initialState())
9694

9795
result.isDirty = ko.pureComputed(function () {
9896

9997
var dirty = _isInitiallyDirty() || _initialState() !== getRepresentation();
100-
if (dirty) {
101-
console.log("******************* new state *******************")
102-
console.log(getRepresentation())
103-
}
10498
return dirty;
10599
});
106100
if (rateLimit) {
@@ -109,8 +103,6 @@
109103

110104
result.reset = function () {
111105
_initialState(getRepresentation());
112-
console.log("****************** Reset initial state ******************")
113-
console.log( _initialState())
114106
_isInitiallyDirty(false);
115107
};
116108

@@ -146,16 +138,14 @@
146138

147139
//just for subscriptions
148140
getRepresentation();
149-
console.log("****************** Initial state ******************")
150-
console.log(getRepresentation())
141+
151142
//next time return true and avoid ko.toJS
152143
_initialized(true);
153144

154145
//on initialization this flag is not dirty
155146
return false;
156147
}
157-
console.log("****************** New state ******************")
158-
console.log(getRepresentation())
148+
159149
//on subsequent changes, flag is now dirty
160150
return true;
161151
});
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
describe("dataLoader extender spec", function () {
2+
var turf ;
3+
beforeEach(function() {
4+
jasmine.clock().install();
5+
});
6+
afterEach(function() {
7+
jasmine.clock().uninstall();
8+
});
9+
10+
11+
it("should augment an observable with geojson type methods", function() {
12+
var metadata = {
13+
name:'item',
14+
dataType:'text',
15+
computed: {
16+
source: {
17+
"context-path": "test"
18+
}
19+
}
20+
};
21+
22+
var context = {
23+
test:"test-value"
24+
};
25+
var config = {};
26+
27+
var dataItem = ko.observable().extend({metadata:{metadata:metadata, context:context, config:config}});
28+
var withDataLoader = dataItem.extend({dataLoader:true});
29+
jasmine.clock().tick();
30+
expect(withDataLoader()).toEqual("test-value");
31+
32+
});
33+
34+
35+
});

src/test/js/spec/PrepopulationSpec.js

+81
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,85 @@ describe("Pre-population Spec", function () {
7575
expect(result).toEqual({item1:"test"});
7676
});
7777
});
78+
79+
it("Should should support computing the pre-pop params via an expression", function() {
80+
var context = {
81+
data: {
82+
item1: '1',
83+
item2: '2'
84+
}
85+
};
86+
87+
var prepopConfig = {
88+
source: {
89+
url:'test',
90+
params: [{
91+
"type":"computed",
92+
"expression":"2+2",
93+
name:"p1",
94+
}]
95+
},
96+
mapping: []
97+
};
98+
99+
var config = {
100+
prepopUrlPrefix:'/'
101+
};
102+
103+
var url;
104+
var params;
105+
spyOn($, 'ajax').and.callFake(function(p1,p2) {
106+
url = p1;
107+
params = p2;
108+
return $.Deferred().resolve(context).promise();
109+
});
110+
111+
var dataLoader = ecodata.forms.dataLoader(context, config);
112+
dataLoader.getPrepopData(prepopConfig).done(function(result) {
113+
expect(url).toEqual(config.prepopUrlPrefix+prepopConfig.source.url);
114+
expect(params.data[0]).toEqual({name:"p1", value:4});
115+
expect(params.dataType).toEqual('json');
116+
117+
expect(result).toEqual(context);
118+
});
119+
});
120+
121+
// This prevents making calls that will return errors
122+
it("Should not make a remote call if required params are undefined", function() {
123+
var context = {
124+
data: {
125+
item1: '1',
126+
item2: '2'
127+
}
128+
};
129+
130+
var prepopConfig = {
131+
source: {
132+
url:'test',
133+
params: [{
134+
"type":"computed",
135+
"expression":"x",
136+
name:"p1",
137+
required:true
138+
}]
139+
},
140+
mapping: []
141+
};
142+
143+
var config = {
144+
prepopUrlPrefix:'/'
145+
};
146+
147+
var called = false;
148+
149+
spyOn($, 'ajax').and.callFake(function(p1,p2) {
150+
called = true;
151+
});
152+
153+
var dataLoader = ecodata.forms.dataLoader(context, config);
154+
dataLoader.getPrepopData(prepopConfig).done(function(result) {
155+
});
156+
157+
expect(called).toEqual(false);
158+
});
78159
});

0 commit comments

Comments
 (0)