Best JavaScript code snippet using jest
message_channel.js
Source:message_channel.js
...29 @param {String} eventName the name of the event30 @param {Function} callback the callback to call when the event occurs31 @param {any?} binding an optional value of `this` inside of the callback32 */33 on: mustImplement('OasisPort', 'on'),34 /**35 Allows you to register an event handler that is called for all events36 that are sent to the port.37 */38 all: mustImplement('OasisPort', 'all'),39 /**40 This allows you to unregister an event handler for an event name41 and callback. You should not pass in the optional binding.42 @param {String} eventName the name of the event43 @param {Function} callback a reference to the callback that was44 passed into `.on`.45 */46 off: mustImplement('OasisPort', 'off'),47 /**48 This method sends an event to the other side of the connection.49 @param {String} eventName the name of the event50 @param {Structured?} data optional data to pass along with the event51 */52 send: mustImplement('OasisPort', 'send'),53 /**54 Adapters should implement this to start receiving messages from the55 other side of the connection.56 It is up to the adapter to make sure that no messages are dropped if57 they are sent before `start` is called.58 */59 start: mustImplement('OasisPort', 'start'),60 /**61 Adapters should implement this to stop receiving messages from the62 other side of the connection.63 */64 close: mustImplement('OasisPort', 'close'),65 /**66 This method sends a request to the other side of the connection.67 @param {String} requestName the name of the request68 @return {Promise} a promise that will be resolved with the value69 provided by the other side of the connection, or rejected if the other70 side indicates retrieving the value resulted in an error. The fulfillment71 value must be structured data.72 */73 request: function(eventName) {74 var oasis = this.oasis;75 var port = this;76 var args = [].slice.call(arguments, 1);77 return new RSVP.Promise(function (resolve, reject) {78 var requestId = getRequestId(oasis);79 var clearObservers = function () {80 port.off('@response:' + eventName, observer);81 port.off('@errorResponse:' + eventName, errorObserver);82 };83 var observer = function(event) {84 if (event.requestId === requestId) {85 clearObservers();86 resolve(event.data);87 }88 };89 var errorObserver = function (event) {90 if (event.requestId === requestId) {91 clearObservers();92 reject(event.data);93 }94 };95 port.on('@response:' + eventName, observer, port);96 port.on('@errorResponse:' + eventName, errorObserver, port);97 port.send('@request:' + eventName, { requestId: requestId, args: args });98 });99 },100 /**101 This method registers a callback to be called when a request is made102 by the other side of the connection.103 The callback will be called with any arguments passed in the request. It104 may either return a value directly, or return a promise if the value must be105 retrieved asynchronously.106 Examples:107 // This completes the request immediately.108 service.onRequest('name', function () {109 return 'David';110 });111 // This completely the request asynchronously.112 service.onRequest('name', function () {113 return new Oasis.RSVP.Promise(function (resolve, reject) {114 setTimeout( function() {115 resolve('David');116 }, 200);117 });118 });119 @param {String} requestName the name of the request120 @param {Function} callback the callback to be called when a request121 is made.122 @param {any?} binding the value of `this` in the callback123 */124 onRequest: function(eventName, callback, binding) {125 var self = this;126 this.on('@request:' + eventName, function(data) {127 var requestId = data.requestId,128 args = data.args,129 getResponse = new RSVP.Promise(function (resolve, reject) {130 var value = callback.apply(binding, data.args);131 if (undefined !== value) {132 resolve(value);133 } else {134 reject("@request:" + eventName + " [" + data.requestId + "] did not return a value. If you want to return a literal `undefined` return `RSVP.resolve(undefined)`");135 }136 });137 getResponse.then(function (value) {138 self.send('@response:' + eventName, {139 requestId: requestId,140 data: value141 });142 }, function (error) {143 var value = error;144 if (error instanceof Error) {145 value = {146 message: error.message,147 stack: error.stack148 };149 }150 self.send('@errorResponse:' + eventName, {151 requestId: requestId,152 data: value153 });154 });155 });156 }157 };158 function OasisMessageChannel(oasis) {}159 OasisMessageChannel.prototype = {160 start: mustImplement('OasisMessageChannel', 'start')161 };162 var PostMessageMessageChannel = extend(OasisMessageChannel, {163 initialize: function(oasis) {164 this.channel = new MessageChannel();165 this.port1 = new PostMessagePort(oasis, this.channel.port1);166 this.port2 = new PostMessagePort(oasis, this.channel.port2);167 },168 start: function() {169 this.port1.start();170 this.port2.start();171 },172 destroy: function() {173 this.port1.close();174 this.port2.close();...
base_adapter.js
Source:base_adapter.js
...14 function BaseAdapter() {15 this._unsupportedCapabilities = [];16 }17 BaseAdapter.prototype = {18 initializeSandbox: mustImplement('BaseAdapter', 'initializeSandbox'),19 name: mustImplement('BaseAdapter', 'name'),20 unsupportedCapabilities: function () {21 return this._unsupportedCapabilities;22 },23 addUnsupportedCapability: function (capability) {24 this._unsupportedCapabilities.push(capability);25 },26 filterCapabilities: function(capabilities) {27 var unsupported = this._unsupportedCapabilities;28 return a_filter.call(capabilities, function (capability) {29 var index = a_indexOf.call(unsupported, capability);30 return index === -1;31 });32 },33 createChannel: function(oasis) {...
snapshot_resolver.js
Source:snapshot_resolver.js
...38 snapshotResolverPath: Path,39): SnapshotResolver {40 const custom = (require(snapshotResolverPath): SnapshotResolver);41 if (typeof custom.resolveSnapshotPath !== 'function') {42 throw new TypeError(mustImplement('resolveSnapshotPath'));43 }44 if (typeof custom.resolveTestPath !== 'function') {45 throw new TypeError(mustImplement('resolveTestPath'));46 }47 const customResolver = {48 resolveSnapshotPath: testPath =>49 custom.resolveSnapshotPath(testPath, DOT_EXTENSION),50 resolveTestPath: snapshotPath =>51 custom.resolveTestPath(snapshotPath, DOT_EXTENSION),52 };53 verifyConsistentTransformations(customResolver);54 return customResolver;55}56function mustImplement(functionName: string) {57 return (58 chalk.bold(59 `Custom snapshot resolver must implement a \`${functionName}\` function.`,60 ) +61 '\nDocumentation: https://facebook.github.io/jest/docs/en/configuration.html#snapshotResolver'62 );63}64function verifyConsistentTransformations(custom: SnapshotResolver) {65 const fakeTestPath = path.posix.join(66 'some-path',67 '__tests__',68 'snapshot_resolver.test.js',69 );70 const transformedPath = custom.resolveTestPath(...
bin.js
Source:bin.js
...3export default function Bin(content, width) {4 this.width = width || 0;5 this.content = content;6}7function mustImplement(name) {8 return function() {9 throw new TypeError("MustImplement: " + name );10 };11}12// abstract13Bin.prototype.objectAt = function (collection, index) {14 return collection[index];15};16// abstract: return coordinates of element at index.17//18// @param index: index of the element in content19// @param width: viewport width.20// @returns {x, y} coordinates of element at index.21//22// May reset cached viewport width.23Bin.prototype.position = mustImplement('position');24// abstract: reset internal state to be anchored at index.25// @param index: index of the element in content26Bin.prototype.flush = mustImplement('flush');27// abstract: return total content height given viewport width.28// @param width: viewport width29//30// May reset cached viewport width.31Bin.prototype.height = mustImplement('height');32// abstract: true if layout places more than one object on a line.33Bin.prototype.isGrid = mustImplement('isGrid');34export function rangeError(length, index) {35 throw new RangeError("Parameter must be within: [" + 0 + " and " + length + ") but was: " + index);36}37// abstract: returns number of elements in content.38Bin.prototype.length = function () {39 return this.content.length;40};41// maximum offset of content wrt to viewport42// The amount by which content (after being layed out) is taller than43// the viewport.44Bin.prototype.maxContentOffset = function Bin_maxContentOffset(width, height) {45 var contentHeight = this.height(width);46 var maxOffset = Math.max(contentHeight - height, 0);47 return maxOffset;48}49// abstract: returns index of first visible item.50// @param topOffset: scroll position51// @param width: width of viewport52// @param height: height of viewport53//54Bin.prototype.visibleStartingIndex = mustImplement('visibleStartingIndex');55// abstract: returns number of items visible in viewport.56// @param topOffset: scroll position57// @param width: width of viewport58// @param height: height of viewport59Bin.prototype.numberVisibleWithin = mustImplement('numberVisibleWithin');60Bin.prototype.heightAtIndex = function (index) {61 return this.content[index].height;62};63Bin.prototype.widthAtIndex = function (index) {64 return this.content[index].width;...
util.js
Source:util.js
...11 throw new Error(string);12 }13 }14 function noop() { }15 function mustImplement(className, name) {16 return function() {17 throw new Error("Subclasses of " + className + " must implement " + name);18 };19 }20 function extend(parent, object) {21 function OasisObject() {22 parent.apply(this, arguments);23 if (this.initialize) {24 this.initialize.apply(this, arguments);25 }26 }27 OasisObject.prototype = o_create(parent.prototype);28 for (var prop in object) {29 if (!object.hasOwnProperty(prop)) { continue; }...
outcome.mjs
Source:outcome.mjs
2 static of(result) {3 return new Success(result);4 }5 map(f) {6 mustImplement();7 }8 forEach(f) {9 mustImplement();10 }11 chain(f) {12 mustImplement();13 }14}15export class Success extends Outcome {16 constructor(result) {17 super();18 this.success = true;19 this.result = result;20 }21 // f: A => B22 // ret: Outcome<B>23 map(f) {24 return new Success(f(this.result));25 }26 // f: A => void27 // ret: Outcome<A>28 forEach(f) {29 f(this.result);30 return this;31 }32 // f: A => Outcome<B>33 // ret: Outcome<B>34 chain(f) {35 return f(this.result);36 }37}38export class Failure extends Outcome {39 constructor(error, subErrors) {40 super();41 this.success = false;42 this.result = error;43 this.subErrors = subErrors;44 }45 toString() {46 return `Failure { result: ${this.result}, subErrors: ${this.subErrors} }`;47 }48 // f: A => B49 // ret: Outcome<B>50 map(f) {51 return this;52 }53 // f: A => void54 // ret: Outcome<A>55 forEach(f) {56 return this;57 }58 // f: A => Outcome<B>59 // ret: Outcome<B>60 chain(f) {61 return this;62 }63}64function mustImplement() {65 throw new Error('Must implement');...
interface.js
Source:interface.js
...3 * 4 * The interface all dialogs must implement.5**/6Dialog.Interface = (function() {7 function mustImplement(failureMessage) {8 return function() {9 Dialog.error(message);10 throw message;11 };12 }13 14 return {15 /**16 * Dialog.Interface.getFrame() -> Element17 * 18 * Should be implemented to return the dialog's outermost element.19 **/20 getFrame: mustImplement('getFrame() must be implemented to return the outermost dialog frame.'),21 /**22 * Dialog.Interface.close() -> undefined23 * 24 * Should be implemented to close and dispose of the dialog.25 **/26 close: mustImplement('close() must be implemented to close the dialog.'),27 /**28 * Dialog.Interface.isModal() -> Boolean29 * 30 * Should return true if the dialog is intended to be modal, false otherwise (the default).31 **/32 isModal: function() {33 return false;34 },35 36 callback: function(which) {37 if (this.options[which] && Object.isFunction(this.options[which])) {38 return this.options[which]();39 }40 }...
adapter.js
Source:adapter.js
1function mustImplement(message) {2 var fn = function() {3 var className = this.constructor.toString();4 throw new Error(message.replace('{{className}}', className));5 };6 fn.isUnimplemented = true;7 return fn;8}9Ember.Adapter = Ember.Object.extend({10 find: mustImplement('{{className}} must implement find'),11 findQuery: mustImplement('{{className}} must implement findQuery'),12 findMany: mustImplement('{{className}} must implement findMany'),13 findAll: mustImplement('{{className}} must implement findAll'),14 createRecord: mustImplement('{{className}} must implement createRecord'),15 saveRecord: mustImplement('{{className}} must implement saveRecord'),16 deleteRecord: mustImplement('{{className}} must implement deleteRecord'),17 load: function(record, id, data) {18 record.load(id, data);19 }...
LambdaTest’s Jest Testing Tutorial covers step-by-step guides around Jest with code examples to help you be proficient with the Jest framework. The Jest tutorial has chapters to help you learn right from the basics of Jest framework to code-based tutorials around testing react apps with Jest, perform snapshot testing, import ES modules and more.
|<p>it('check_object_of_Car', () => {</p><p>
expect(newCar()).toBeInstanceOf(Car);</p><p>
});</p>|
| :- |
Get 100 minutes of automation test minutes FREE!!