Last active
November 20, 2018 11:17
-
-
Save jmbauguess/e977ecf0ef77716559e8 to your computer and use it in GitHub Desktop.
Allows unit testing of Events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @description Allows unit testing of Events | |
* @namespace | |
* @example | |
* //Create an incident, update the state, then ensure the incident.state.changed event was fired | |
* var incident = new GlideRecord('incident'); | |
* if (incident.get(sys_id)) { | |
* incident.state = 2; | |
* incident.update(); | |
* } | |
* assertTrue(new EventTester().isEventCorrect('incident.state.changed', incident)); | |
* @type {Class} | |
*/ | |
var EventTester = Class.create(); | |
EventTester.prototype = { | |
/** | |
* @description The name of the event log table | |
* @type {String} | |
*/ | |
TABLE_NAME: 'sysevent', | |
/** | |
* @description An encoded query that gets anything created within the current minute | |
* @type {String} | |
*/ | |
ENCODED_QUERY : 'sys_created_onONCurrent minute@javascript:gs.minutesAgoStart(0)@javascript:gs.minutesAgoEnd(0)', | |
/** | |
* @description Finds event records created in the current minute with a given name | |
* @param {String} name The name of an event | |
* @return {GlideRecord} Event Log records | |
*/ | |
findEventByNameAndParams: function(name, parm1, parm2) { | |
var record = new GlideRecord(this.TABLE_NAME); | |
record.addEncodedQuery(this.ENCODED_QUERY); | |
record.addQuery('name', name); | |
record.addQuery('parm1', parm1); | |
record.addQuery('parm2', parm2); | |
record.query(); | |
return record; | |
}, | |
/** | |
* @description Determines if an event is correct | |
* @param {String} name The name of an event | |
* @param {GlideRecord} record The record to check against | |
* @param {String} parm1 The expected 1st parameter | |
* @param {String} parm2 The expected 2nd parameter | |
* @return {Boolean} True if the event was correct; false if not | |
*/ | |
isEventCorrect: function(name, record, parm1, parm2) { | |
var test = this.findEventByNameAndParams(name, parm1, parm2); | |
if (test.next()) { | |
return (test.name == name && test.instance == record.sys_id && | |
test.parm1 == parm1 && test.parm2 == parm2); | |
} | |
return false; | |
}, | |
'type' : 'EventTester' | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment