clearbit-jsr-mocks
Advanced tools
Comparing version 3.0.3 to 3.1.0
@@ -18,3 +18,3 @@ 'use strict'; | ||
this.$mocks = mocks; | ||
if (!window.Visualforce) { | ||
if (!window.Visualforce || top !== self) { | ||
this.remoting = { | ||
@@ -21,0 +21,0 @@ Manager: { |
{ | ||
"name": "clearbit-jsr-mocks", | ||
"version": "3.0.3", | ||
"version": "3.1.0", | ||
"description": "jsr-mocks", | ||
@@ -5,0 +5,0 @@ "main": "build/jsr-promise.js", |
163
README.md
@@ -1,151 +0,50 @@ | ||
jsr-mocks | ||
========= | ||
# clearbit-jsr-mocks | ||
a mock shim for javascript remoting | ||
A mock shim for javascript remoting (special thanks to [Dan Shahin](https://github.com/dshahin)) | ||
Use to test local versions of single page salesforce apps without deploying to server | ||
install | ||
=========== | ||
```bower install jsr-mocks#angular``` | ||
Configure jsr-mocks to expect a global object named ```configSettings``` with a property called ```mocks``` | ||
## Install | ||
`yarn add clearbit-jsr-mocks` | ||
## Setup Mocks | ||
```javascript | ||
'use strict'; | ||
const remoting = vfr( | ||
new vfrMocks({ | ||
getAccounts: { | ||
method: (_) -> {...yourMock} | ||
} | ||
}) | ||
); | ||
``` | ||
angular.module('App', ['ngAnimate', 'ngSanitize', 'ui.router', 'jsrMocks']) | ||
## Setup RemoteActions | ||
.config(function ($stateProvider, $urlRouterProvider,jsrMocksProvider) { | ||
$stateProvider | ||
.state('home', { | ||
url: '/', | ||
templateUrl: 'app/main/main.html', | ||
controller: 'MainController' | ||
}); | ||
In your VF page add something like the following to resolve the names of the remote actions. | ||
$urlRouterProvider.otherwise('/'); | ||
//configure jsr-mocks | ||
jsrMocksProvider.setMocks(configSettings.mocks); | ||
}); | ||
; | ||
``` | ||
Now in your VF page, you must resolve the names of your Javascript Remote Actions. | ||
```html | ||
<head> | ||
<link rel="stylesheet" href="/static/css/main.css" /> | ||
</head> | ||
<div id="ready" class="ready">I am ready already</div> | ||
<div id="ready2" class="ready"><img src="http://s25.postimage.org/ykwiwxw23/ajax_loader_2.gif" alt="loading"/></div> | ||
<div id="ready3" class="ready"><img src="http://s25.postimage.org/ykwiwxw23/ajax_loader_2.gif" alt="loading"/></div> | ||
<script src="/static/bower_components/jquery/dist/jquery.js"></script> | ||
<script src="/static/js/myOtherModule.js"></script> | ||
<!-- first resolve VF variables into configSettings, our only global object --> | ||
<script> | ||
var configSettings = { | ||
remoteActions: { | ||
myFunction :'{!$RemoteAction.MyCustomController.myFunction}', | ||
myOtherFunction :'{!$RemoteAction.MyCustomController.myOtherFunction}' | ||
}, | ||
mocks :{ | ||
'{!$RemoteAction.MyCustomController.myFunction}' : { | ||
timeout : 2000, //2 seconds with inline callback | ||
method : function(args){ | ||
console.log('args',args); | ||
return { message: args[1]} | ||
} | ||
}, | ||
'{!$RemoteAction.MyCustomController.myOtherFunction}' : { | ||
timeout : 3000, //3 seconds with named callback function | ||
method : myCallback | ||
} | ||
} | ||
}; | ||
function myCallback(args){ | ||
console.log('args',args); | ||
return { message: args[1] + ' says the other callback'} | ||
} | ||
</script> | ||
<!-- since this is not a VF page, we load a shim library instead for JSR calls --> | ||
<script src="/static/bower_components/jsr-mocks/dist/jsr-mocks.provider.js"></script> | ||
<!-- now our main script can call JSR methods in VF and mock methods in HTML Page with same syntax --> | ||
<script src="/static/js/main.js"></script> | ||
... | ||
<script> | ||
window.vfr = { | ||
getAccounts: '{!$RemoteAction.AccountController.getAccounts}', | ||
} | ||
</script> | ||
... | ||
``` | ||
main.js can now look like this | ||
## Call the RemoteAction | ||
```javascript | ||
In your ES6 code, add | ||
angular.module('myModule', ['jsrMocks']) //inject dependency | ||
.config(function ($stateProvider, $urlRouterProvider, jsrMocksProvider){ | ||
jsrMocksProvider.setMocks(configSettings.mocks);//point to global config variable (see html) | ||
}); | ||
``` | ||
Now you can use the newly provided jsr method | ||
```javascript | ||
jsr({ | ||
method: configSettings.remoteActions.communityUserId, //required | ||
args: [patient.id], //only required if function expects arguments | ||
options : {buffer: true, escape: true, timeout: 30000} //optional | ||
}).then(function(communityUserId) { | ||
if (communityUserId) { | ||
patientTaskModal.patient.communityUserId = communityUserId; | ||
patientTaskModal.patientIsCommunityUser = true; | ||
} | ||
patientTaskModal.ready = true; | ||
}); | ||
arg => remoting({ method: 'getAccounts', args: [arg] }); | ||
``` | ||
or factories can just include the jsrMocks object and override Visualforce selectively | ||
```javascript | ||
'use strict'; | ||
angular.module('myModule').factory('PatientFactory', PatientFactory ); | ||
clearbit-jsr-mocks utilizes a global object named `vfr` to map your short method name to the fully qualified remote action name. However, you do not have to use this, you can specify the fully qualified remote action name. However, this approach is not dynamic-namespace safe, so is not recommended. | ||
function PatientFactory($q, $rootScope, $log, jsrMocks) { | ||
//insert the shim | ||
var Visualforce = jsrMocks; | ||
var factory = { | ||
GetPatient : GetPatient | ||
}; | ||
return factory; | ||
function GetPatient (patientId) { | ||
var deferred = $q.defer(); | ||
//always use namespace-safe jsr invocations | ||
Visualforce.remoting.Manager.invokeAction( | ||
configSettings.remoteActions.getPatient, | ||
patientId,//this is the first argument to jsr method | ||
function(result, event) { | ||
$rootScope.$apply(function() { | ||
if (event.status) { | ||
deferred.resolve(result); | ||
} else { | ||
deferred.reject(event); | ||
} | ||
}); | ||
} | ||
); | ||
//always return a promise when making async calls | ||
return deferred.promise; | ||
} | ||
} | ||
``` | ||
```javascript | ||
arg => remoting({ method: 'ns.AccountController.getAccounts', args: [arg] }); | ||
``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9251
50