Created
June 6, 2016 19:13
-
-
Save AbraaoAlves/9d9009329dc52e46db63ce22e695fc0e to your computer and use it in GitHub Desktop.
Async JS test sample
This file contains hidden or 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
| function criarComponente(name, type) { | |
| var events = { | |
| 'onAfterLoad': [], | |
| }; | |
| return { | |
| url: '', | |
| load: function () { | |
| $.getJSON(this.url).done(this._executeEvents('onAfterLoad')); | |
| }, | |
| addEvent: function (event, callback) { | |
| events[event].push(callback); | |
| }, | |
| _executeEvents: function (event) { | |
| var callbacks = events[event]; | |
| return function(){ | |
| var args = arguments; | |
| callbacks.forEach(function (cb) { | |
| cb.apply({}, args); | |
| }); | |
| } | |
| } | |
| } | |
| }; |
This file contains hidden or 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
| describe('my suite test', function () { | |
| var server; | |
| beforeEach(function () { | |
| server = sinon.fakeServer.create({ respondImmediately: false }); | |
| }); | |
| afterEach(function () { | |
| server.restore(); | |
| }); | |
| describe('my component data', function(){ | |
| var ds, url = '/url/arquivo.json'; | |
| beforeEach(function(){ | |
| server.respondWith('GET', url, | |
| [200, { 'Content-Type': 'application/json' }]); | |
| ds = criarComponente('dst', 'DataSet'); | |
| ds.url = url; | |
| }); | |
| }); | |
| it('should verify if the fake server return on load', function (done) { | |
| var response = { id: 1, comment: 'Hey there'}; | |
| ds.addEvent('onAfterLoad', function (data) { | |
| expect(data).toBe(response); | |
| done(); | |
| }); | |
| ds.load(); | |
| server.respond(response); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment