Skip to content

Commit

Permalink
Added tests, reverted accidental commit #208
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisala committed Aug 24, 2023
1 parent 787026b commit 6f80da3
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 19 deletions.
5 changes: 2 additions & 3 deletions grails-app/assets/javascripts/forms-knockout-bindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -1165,14 +1165,13 @@
var dataLoader = new ecodata.forms.dataLoader(target.context, target.config);
var dataLoaderConfig = target.get('computed');
if (!dataLoaderConfig) {
throw "This extender can only be used with the metadata extender";
throw "This extender can only be used with the metadata extender and expects a computed property to be defined";
}
var dependencyTracker = ko.computed(function () {
return dataLoader.prepop(dataLoaderConfig).done( function(data) {
target(data);
});
});
//dependencyTracker.subscribe(function() { console.log("bananas")})
}); // This is a computed rather than a pureComputed as it has a side effect.
return target;
}

Expand Down
7 changes: 3 additions & 4 deletions grails-app/assets/javascripts/forms.js
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,6 @@ function orEmptyArray(v) {
}
}
else if (metadata.constraints.type == 'pre-populated') {
console.log("******************** Encountered pre-pop constraints for "+self.getName()+"***************************************")
var defaultConstraints = metadata.constraints.defaults || [];
var constraintsObservable = ko.observableArray(defaultConstraints);
if (!includeExcludeDefined) {
Expand Down Expand Up @@ -915,14 +914,14 @@ function orEmptyArray(v) {
self.displayOptions = metadata.displayOptions;
}
self.load = function(data) {
console.log("Loading data for "+self.getName()+" : "+data)
self(data);
if (constraintsInititaliser) {
constraintsInititaliser.always(function() {
console.log("Re-Loading data for "+self.getName()+" : "+data)
self(data);
})
}
else {
self(data);
}

}
};
Expand Down
14 changes: 2 additions & 12 deletions grails-app/assets/javascripts/knockout-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,16 +91,10 @@
return (typeof root.modelAsJSON === 'function') ? root.modelAsJSON() : ko.toJSON(root);
};
var _initialState = ko.observable(getRepresentation());
console.log("****************** Initial state ******************")
console.log( _initialState())

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

var dirty = _isInitiallyDirty() || _initialState() !== getRepresentation();
if (dirty) {
console.log("******************* new state *******************")
console.log(getRepresentation())
}
return dirty;
});
if (rateLimit) {
Expand All @@ -109,8 +103,6 @@

result.reset = function () {
_initialState(getRepresentation());
console.log("****************** Reset initial state ******************")
console.log( _initialState())
_isInitiallyDirty(false);
};

Expand Down Expand Up @@ -146,16 +138,14 @@

//just for subscriptions
getRepresentation();
console.log("****************** Initial state ******************")
console.log(getRepresentation())

//next time return true and avoid ko.toJS
_initialized(true);

//on initialization this flag is not dirty
return false;
}
console.log("****************** New state ******************")
console.log(getRepresentation())

//on subsequent changes, flag is now dirty
return true;
});
Expand Down
35 changes: 35 additions & 0 deletions src/test/js/spec/DataLoaderExtenderSpec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
describe("dataLoader extender spec", function () {
var turf ;
beforeEach(function() {
jasmine.clock().install();
});
afterEach(function() {
jasmine.clock().uninstall();
});


it("should augment an observable with geojson type methods", function() {
var metadata = {
name:'item',
dataType:'text',
computed: {
source: {
"context-path": "test"
}
}
};

var context = {
test:"test-value"
};
var config = {};

var dataItem = ko.observable().extend({metadata:{metadata:metadata, context:context, config:config}});
var withDataLoader = dataItem.extend({dataLoader:true});
jasmine.clock().tick();
expect(withDataLoader()).toEqual("test-value");

});


});
81 changes: 81 additions & 0 deletions src/test/js/spec/PrepopulationSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,85 @@ describe("Pre-population Spec", function () {
expect(result).toEqual({item1:"test"});
});
});

it("Should should support computing the pre-pop params via an expression", function() {
var context = {
data: {
item1: '1',
item2: '2'
}
};

var prepopConfig = {
source: {
url:'test',
params: [{
"type":"computed",
"expression":"2+2",
name:"p1",
}]
},
mapping: []
};

var config = {
prepopUrlPrefix:'/'
};

var url;
var params;
spyOn($, 'ajax').and.callFake(function(p1,p2) {
url = p1;
params = p2;
return $.Deferred().resolve(context).promise();
});

var dataLoader = ecodata.forms.dataLoader(context, config);
dataLoader.getPrepopData(prepopConfig).done(function(result) {
expect(url).toEqual(config.prepopUrlPrefix+prepopConfig.source.url);
expect(params.data[0]).toEqual({name:"p1", value:4});
expect(params.dataType).toEqual('json');

expect(result).toEqual(context);
});
});

// This prevents making calls that will return errors
it("Should not make a remote call if required params are undefined", function() {
var context = {
data: {
item1: '1',
item2: '2'
}
};

var prepopConfig = {
source: {
url:'test',
params: [{
"type":"computed",
"expression":"x",
name:"p1",
required:true
}]
},
mapping: []
};

var config = {
prepopUrlPrefix:'/'
};

var called = false;

spyOn($, 'ajax').and.callFake(function(p1,p2) {
called = true;
});

var dataLoader = ecodata.forms.dataLoader(context, config);
dataLoader.getPrepopData(prepopConfig).done(function(result) {
});

expect(called).toEqual(false);
});
});

0 comments on commit 6f80da3

Please sign in to comment.