How to use getCaller method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

implementation_spec.js

Source: implementation_spec.js Github

copy
1var expect          = require('chai').expect,
2    _implementation = require('../../lib/implementation')('../../lib/implementation'),
3    noop            = function () {};
4
5describe('implementation', function () {
6
7    it('is a function', function () {
8        var implementation = _implementation();
9        expect(implementation, 'is a valid function').to.be.a('function');
10        expect(implementation.length, 'has an call arity of 2').to.equal(2);
11    });
12
13    it('normalizes caller path', function () {
14
15        var calledCount = 0,
16            _require    = function () {},
17            implementation;
18
19        _require.resolve = function (path) {
20            calledCount++;
21            expect(
22                path,
23                'path has been correctly resolved'
24            ).to.equal('/user/path/config');
25            return '';
26        };
27
28        implementation = _implementation(_require, {
29            dirname: function (path) {
30                calledCount++;
31                expect(path).to.equal('/user/path/to/caller/index.js');
32                return '/user/path/to/caller/';
33            },
34            resolve: function (from, to) {
35                calledCount++;
36                expect(from).to.equal('/user/path/to/caller/');
37                expect(to).to.equal('../../config');
38                return '/user/path/config';
39            }
40        });
41
42        implementation.getCaller = function () {
43            calledCount++;
44            return '/user/path/to/caller/index.js';
45        };
46
47        implementation('../../config');
48        expect(calledCount, 'correctly called methods').to.equal(4);
49
50    });
51
52    it('requires implementation correctly ( suffix: _implementation )', function () {
53
54        var calledCount = 0,
55            _require    = function (path) {
56                calledCount++;
57                expect(
58                    path,
59                    'correctly formatted implementation path'
60                ).to.equal('/user/path/config/index_implementation');
61            },
62            implementation;
63
64        _require.resolve = function () {
65            calledCount++;
66            return '/user/path/config/index.js';
67        };
68
69        implementation = _implementation(_require, {
70            dirname: function () {
71                calledCount++;
72                return '/user/path/to/caller/';
73            },
74            resolve: function () {
75                calledCount++;
76                return '/user/path/config';
77            }
78        });
79
80        implementation.getCaller = function () {
81            calledCount++;
82            return '/user/path/to/caller/index.js';
83        };
84
85        implementation('../../config');
86        expect(calledCount, 'correctly called methods').to.equal(5);
87
88    });
89
90    it('requires implementation correctly ( custom suffix: _foo )', function () {
91
92        var calledCount = 0,
93            _require    = function (path) {
94                calledCount++;
95                expect(
96                    path,
97                    'correctly formatted implementation path'
98                ).to.equal('/user/path/config/index_foo');
99            },
100            implementation;
101
102        _require.resolve = function () {
103            calledCount++;
104            return '/user/path/config/index.js';
105        };
106
107        implementation = _implementation(_require, {
108            dirname: function () {
109                calledCount++;
110                return '/user/path/to/caller/';
111            },
112            resolve: function () {
113                calledCount++;
114                return '/user/path/config';
115            }
116        });
117
118        implementation.getCaller = function () {
119            calledCount++;
120            return '/user/path/to/caller/index.js';
121        };
122
123        implementation('../../config', '_foo');
124        expect(calledCount, 'correctly called methods').to.equal(5);
125
126    });
127
128    describe('self.getCaller', function () {
129
130        it('is a function', function () {
131            var getCaller = _implementation().getCaller;
132            expect(getCaller, 'is a valid function').to.be.a('function');
133            expect(getCaller.length, 'has an call arity of 0').to.equal(0);
134        });
135
136        it('called with the correct arguments', function () {
137
138            var _require = function () {},
139                implementation;
140
141            _require.resolve = function () { return ''; };
142
143            implementation = _implementation(_require, {
144                dirname: noop,
145                resolve: noop
146            });
147
148            implementation.getCaller = function (path) {
149                expect(path, 'correctly passed to getCaller').to.equal('../foo/bar');
150                return 'foo';
151            };
152
153            implementation('../foo/bar');
154
155        });
156
157        // self.getCaller = function (path) {
158        //
159        //     var stack   = callsite(path),
160        //         current = stack.shift().getFileName(),
161        //         caller;
162        //
163        //     while (stack.length) {
164        //         caller = stack.shift().getFileName();
165        //         if (current !== caller) break;
166        //     }
167        //
168        //     return caller;
169        //
170        // };
171
172        it('calls callsite', function () {
173
174            var calledCount = 0,
175                callsite = function () {
176                    calledCount++;
177                    return [{ getFileName: noop }];
178                },
179                getCaller = _implementation(null, null, callsite).getCaller;
180
181            getCaller();
182            expect(calledCount, 'callsite is called once').to.equal(1);
183
184        });
185
186        it('returns current caller', function () {
187
188            var calledCount = 0,
189                callsite = function () {
190                    calledCount++;
191                    return [
192                        {
193                            getFileName: function () {
194                                return 'foo';
195                            }
196                        }, {
197                            getFileName: function () {
198                                return 'bar';
199                            }
200                        }
201                    ];
202                },
203                getCaller = _implementation(null, null, callsite).getCaller,
204                result    = getCaller();
205
206            expect(result).to.equal('bar');
207            expect(calledCount, 'callsite is called once').to.equal(1);
208
209        });
210
211        it('returns undefined if cannot obtain current caller', function () {
212
213            var calledCount = 0,
214                callsite = function () {
215                    calledCount++;
216                    return [
217                        {
218                            getFileName: function () {
219                                return 'foo';
220                            }
221                        }
222                    ];
223                },
224                getCaller = _implementation(null, null, callsite).getCaller,
225                result    = getCaller();
226
227            expect(result).to.be.an('undefined');
228            expect(calledCount, 'callsite is called once').to.equal(1);
229
230        });
231
232    });
233
234    describe('exceptions', function () {
235
236        it('thrown for invalid input', function () {
237            var implementation = _implementation();
238            expect(
239                implementation,
240                'throws an exception for non-string input'
241            ).to.throw('Module path must be a string');
242        });
243
244        it('thrown when cannot determine caller', function () {
245            var implementation = _implementation();
246
247            implementation.getCaller = function () {
248                return undefined;
249            };
250
251            expect(
252                implementation.bind(null, 'foo'),
253                'throws an exception when cannot obtain caller'
254            ).to.throw('Unable to require implementation');
255
256        });
257
258        it('thrown when cannot resolve module', function () {
259
260            var _require    = function () {
261                    throw new Error('I should not be called');
262                },
263                implementation;
264
265            _require.resolve = function () {
266                var err = new Error('');
267                err.code = 'MODULE_NOT_FOUND';
268                throw err;
269            };
270
271            implementation = _implementation(_require, {
272                dirname: function () {
273                    return '/user/path/to/caller/';
274                },
275                resolve: function () {
276                    return '/user/path/config';
277                }
278            });
279
280            implementation.getCaller = function () {
281                return '/user/path/to/caller/index.js';
282            };
283
284            expect(
285                implementation.bind(null, '../../config', '_foo')
286            ).to.throw('No implementation file found for module: "../../config"');
287
288        });
289
290        it('thrown when cannot require implementation', function () {
291
292            var _require    = function () {
293                    var err = new Error('');
294                    err.code = 'MODULE_NOT_FOUND';
295                    throw err;
296                },
297                implementation;
298
299            _require.resolve = function () {
300                return '';
301            };
302
303            implementation = _implementation(_require, {
304                dirname: function () {
305                    return '/user/path/to/caller/';
306                },
307                resolve: function () {
308                    return '/user/path/config';
309                }
310            });
311
312            implementation.getCaller = function () {
313                return '/user/path/to/caller/index.js';
314            };
315
316            expect(
317                implementation.bind(null, '../../config', '_foo')
318            ).to.throw('No implementation file found for module: "../../config"');
319
320        });
321
322        it('throws unknown errors', function () {
323
324            var _require = function () {
325                    throw new Error('I should not be called');
326                },
327                implementation;
328
329            _require.resolve = function () {
330                throw new Error('I am an unknown error');
331            };
332
333            implementation = _implementation(_require, {
334                dirname: function () {
335                    return '/user/path/to/caller/';
336                },
337                resolve: function () {
338                    return '/user/path/config';
339                }
340            });
341
342            implementation.getCaller = function () {
343                return '/user/path/to/caller/index.js';
344            };
345
346            expect(
347                implementation.bind(null, '../../config', '_foo')
348            ).to.throw('I am an unknown error');
349
350        });
351
352    });
353
354});
355
Full Screen

Group.js

Source: Group.js Github

copy
1
2sylma.crud = sylma.crud || {};
3
4sylma.crud.Group = new Class({
5
6  Extends : sylma.ui.Clonable,
7  elements : [],
8  highlighted : 0,
9
10  initialize : function(props) {
11
12    this.parent(props);
13    this.elements = Object.keys(this.objects);
14  },
15
16  resetHighlight : function() {
17
18    this.highlighted = 0;
19  },
20
21  highlight : function(alias, sub) {
22
23    var obj = this.getObject(alias, false);
24
25    if (obj) {
26
27      obj.highlight(sub);
28      this.highlighted++;
29
30      if (this.getCaller) {
31
32        this.getCaller().getNode().addClass('field-statut-invalid');
33      }
34    }
35
36    return obj;
37  },
38
39  downlight : function() {
40
41    this.highlighted--;
42
43    if (!this.highlighted) {
44
45      this.getParent().downlight();
46
47      if (this.getCaller) {
48
49        this.getCaller().getNode().removeClass('field-statut-invalid');
50      }
51    }
52  }
53});
Full Screen

index.js

Source: index.js Github

copy
1const BigNumber = require('bignumber.js');
2const assert = require('assert');
3
4function doTransfer(amount) {
5    const accountA = '12nD5LgUnLZDbyncFnoFB43YxhSFsERcgQ';
6    const accountB = '1LuwjNj8wkqo237N7Gh8nZSSvUa6TZ5ds4';
7
8    var amountValue = new BigNumber(amount);
9    let toAValue = amountValue.div(2).toString();
10    let toBValue = amountValue.div(2).toString();
11
12    assert.equal(bcTransfer(accountA, toAValue), true);
13    assert.equal(bcTransfer(accountB, toAValue), true);
14    return true;
15}
16
17function Contract(receiver, caller) {
18    var that = this;
19
20    that._receiver = receiver;
21    that._caller = caller;
22}
23
24function doSetDB() {
25    bcLog('#### Receive is', this.getReceiver());
26    bcLog('#### caller is', this.getCaller());
27    assert.equal(this.getReceiver(), this.getCaller());
28
29    bcLog('#### DB create ret is', bcDBCreate('IotDemo'));
30    return true;
31}
32
33function doUnlock() {
34    bcLog('before bcDBGet');
35    let owner = bcDBGet('IotDemo', 'owner');
36    assert.equal(owner, this.getCaller());
37    assert.equal(bcDBSet('IotDemo', 'state', 'unlock'), true);
38    return true;
39}
40
41function doLock() {
42    let owner = bcDBGet('IotDemo', 'owner');
43    assert.equal(owner, this.getCaller());
44    assert.equal(bcDBSet('IotDemo', 'state', 'lock'), true);
45    return true;
46}
47
48function doSetOwner() {
49    assert.equal(bcDBSet('IotDemo', 'owner', this.getCaller()), true);
50    assert.equal(bcDBSet('IotDemo', 'state', 'lock'), true);
51    return true;
52}
53
54Contract.prototype.getReceiver = function() {
55    return this._receiver;
56}
57
58Contract.prototype.getCaller = function() {
59    return this._caller;
60}
61
62Contract.prototype.doTransfer = doTransfer;
63
64Contract.prototype.doSetDB = doSetDB;
65Contract.prototype.doSetOwner = doSetOwner;
66Contract.prototype.doUnlock = doUnlock;
67Contract.prototype.doLock = doLock;
68
69global.Contract = Contract;
70
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)