Skip to content

Commit

Permalink
Merge 2c9a970 into 202a0c3
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Mar 11, 2018
2 parents 202a0c3 + 2c9a970 commit 891eaa4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 1 deletion.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,31 @@ sauceBrowsers([
*/
```

### Callback variant: `sauceBrowsers([list, ]callback)`

For error-first callback support, use `sauce-browsers/callback`:

```js
const sauceBrowsers = require('sauce-browsers/callback');

sauceBrowsers([
{ name: 'firefox', version: 50, platform: 'Mac 10.9'},
{ name: 'chrome', version: ['oldest', 'latest'] }
], function (err, browsers) {
if (err) throw err;
console.log(browsers);
});
```

If the `list` argument is omitted, the callback receives all platforms currently supported on Sauce Labs:

```js
sauceBrowsers(function (err, browsers) {
if (err) throw err;
console.log(browsers);
});
```

## License

[MIT](LICENSE)
Expand Down
17 changes: 17 additions & 0 deletions callback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict';

const sauceBrowsers = require('.');

module.exports = function(wanted, callback) {
if (typeof wanted === 'function') {
callback = wanted;
wanted = undefined;
}

sauceBrowsers(wanted).then((result) => {
// Escape promise chain
process.nextTick(callback, null, result);
}).catch((err) => {
process.nextTick(callback, err);
});
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
"node": ">=4.0.0"
},
"files": [
"index.js"
"index.js",
"callback.js"
],
"dependencies": {
"got": "^8.2.0"
Expand Down
36 changes: 36 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const test = require('tape');

const allBrowsers = require('./fixtures/all-browsers');
const sauceBrowsers = require('..');
const sauceBrowsersCallback = require('../callback');

function map(browsers) {
return browsers.map((el) => [el.os, el.api_name, el.short_version]);
Expand Down Expand Up @@ -228,3 +229,38 @@ test('returns an error if the end version in a range is not found', (t) => {
t.end();
});
});

test('supports callback with browser list', (t) => {
sauceBrowsersCallback([{
name: 'internet explorer',
version: [6, '-1..latest']
}], function (err, browsers) {
t.ifError(err, 'no error');
t.deepEqual(map(browsers), [
['Windows 2003', 'internet explorer', '6'],
['Windows 2012', 'internet explorer', '10'],
['Windows 10', 'internet explorer', '11']
]);
t.end();
});
});

test('supports callback without browser list', (t) => {
sauceBrowsersCallback(function (err, browsers) {
t.ifError(err, 'no error');
t.deepEqual(browsers, allBrowsers);
t.end();
});
});

test('supports callback with an error', (t) => {
sauceBrowsersCallback([{
version: 'oldest..13',
name: 'opera'
}], (err, browsers) => {
t.equal(err instanceof Error, true);
t.equal(err.message, 'Unable to find end version: 13');
t.equal(browsers, undefined);
t.end();
});
});

0 comments on commit 891eaa4

Please sign in to comment.