Skip to content

Commit

Permalink
🎨 Make good use of async/await rather than Promise.{resolve,then} in …
Browse files Browse the repository at this point in the history
…tests. Closes #319
  • Loading branch information
fasterthanlime committed Jan 22, 2016
1 parent b2c01a0 commit 4ca0830
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 128 deletions.
13 changes: 4 additions & 9 deletions testsrc/stores/credentials-store-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ test('CredentialsStore', t => {
t.notOk(CredentialsStore.get_me(), 'no me after setup')
})

t.case('login with password', t => {
t.case('login with password', async t => {
let user = {name: 'Pete'}
let username = 'foo'
let password = 'bar'
Expand All @@ -58,14 +58,9 @@ test('CredentialsStore', t => {
t.stub(api.client, 'login_with_password').resolves({key: {key}})
t.stub(api.user, 'me').resolves({user})

handler({ action_type: AppConstants.LOGIN_WITH_PASSWORD, username, password })
await handler({ action_type: AppConstants.LOGIN_WITH_PASSWORD, username, password })

return new Promise((resolve, reject) => {
setTimeout(() => {
t.ok(CredentialsStore.get_current_user())
t.same(CredentialsStore.get_me(), user)
resolve()
}, 20)
})
t.ok(CredentialsStore.get_current_user())
t.same(CredentialsStore.get_me(), user)
})
})
16 changes: 9 additions & 7 deletions testsrc/stores/setup-store-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ test('SetupStore', t => {
proxyquire('../../app/stores/setup-store', stubs)
let handler = AppDispatcher.get_handler('setup-store')

t.case('window_ready', t => {
t.case('window_ready', async t => {
t.stub(ibrew, 'fetch').resolves()
return handler({ action_type: AppConstants.WINDOW_READY })
await handler({ action_type: AppConstants.WINDOW_READY })
})

t.case('window_ready (err)', t => {
t.stub(ibrew, 'fetch', (opts, name) => {
if (name === 'butler') return Promise.reject('Ha!')
return Promise.resolve(null)
t.case('window_ready (err)', async t => {
t.stub(ibrew, 'fetch', async (opts, name) => {
if (name === 'butler') {
let err = {stack: 'Ha!'}
throw err
}
})
t.mock(AppActions).expects('setup_status').withArgs('login.status.setup_failure', 'error', { error: 'Ha!' })
return handler({ action_type: AppConstants.WINDOW_READY })
await handler({ action_type: AppConstants.WINDOW_READY })
})
})
4 changes: 1 addition & 3 deletions testsrc/stubs/api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@

let Promise = require('bluebird')

let noop = () => Promise.resolve()
let noop = async () => null

let user = {
me: noop,
Expand Down
5 changes: 1 addition & 4 deletions testsrc/stubs/cave-store.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@


let Promise = require('bluebird')

module.exports = {
find: () => Promise.resolve({
find: async () => ({
_id: 'kalamazoo',
upload_id: 42,
game_id: 84
Expand Down
5 changes: 2 additions & 3 deletions testsrc/tasks/configure-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
let test = require('zopf')
let proxyquire = require('proxyquire')
let path = require('path')
let Promise = require('bluebird')

let fixture = require('../fixture')
let electron = require('../stubs/electron')
Expand All @@ -16,7 +15,7 @@ let opts = {id: 'kalamazoo', logger}
test('configure', t => {
let os = {}

let noop = () => Promise.resolve()
let noop = async () => null
let win32 = {configure: noop}
let darwin = {configure: noop}
let linux = {configure: noop}
Expand Down Expand Up @@ -49,7 +48,7 @@ test('configure', t => {

test('configure (each platform)', t => {
let sf = {
chmod: () => Promise.resolve(),
chmod: async () => null,
'@global': true
}
let stubs = {
Expand Down
75 changes: 34 additions & 41 deletions testsrc/tasks/launch-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ test('launch', t => {
}

let configure = {
start: () => Promise.resolve()
start: async () => null
}

let sf = {
Expand All @@ -50,107 +50,100 @@ test('launch', t => {

let launch = proxyquire('../../app/tasks/launch', stubs)

t.case('rejects 0 execs', t => {
let spy = t.spy()
t.case('rejects 0 execs', async t => {
t.stub(CaveStore, 'find').resolves({})
return launch.start(opts).catch(spy).then(_ => {
t.is(spy.callCount, 1)
t.same(spy.getCall(0).args[0].message, 'No executables found')
})

let err
try {
await launch.start(opts)
} catch (e) { err = e }
t.same(err.message, 'No executables found')
})

t.case('launches top-most exec', t => {
t.case('launches top-most exec', async t => {
t.stub(CaveStore, 'find').resolves({
executables: [ '/a/b/c', '/a/bababa', '/a/b/c/d' ]
})
t.mock(launch).expects('launch').once().withArgs(path.normalize('/tmp/app/a/bababa')).resolves('Done!')
return launch.start(opts)
await launch.start(opts)
})

t.case('ignores uninstallers', t => {
t.case('ignores uninstallers', async t => {
t.stub(CaveStore, 'find').resolves({
executables: [ 'uninstall.exe', 'game.exe' ]
})
t.mock(launch).expects('launch').once().withArgs(path.normalize('/tmp/app/game.exe')).resolves('Done!')
return launch.start(opts)
await launch.start(opts)
})

t.case('ignores dxwebsetup', t => {
t.case('ignores dxwebsetup', async t => {
t.stub(CaveStore, 'find').resolves({
executables: [ 'dxwebsetup.exe', 'game.exe' ]
})
t.mock(launch).expects('launch').once().withArgs(path.normalize('/tmp/app/game.exe')).resolves('Done!')
return launch.start(opts)
await launch.start(opts)
})

t.case('reconfigures as needed', t => {
t.case('reconfigures as needed', async t => {
let find = t.stub(CaveStore, 'find')
find.resolves({ executables: [] })
t.stub(configure, 'start', () => {
t.stub(configure, 'start', async () => {
find.resolves({ executables: ['/a'] })
return Promise.resolve()
})
t.mock(launch).expects('launch').once().withArgs(path.normalize('/tmp/app/a')).resolves('Done!')
return launch.start(opts)
await launch.start(opts)
})

t.case('launch/.app', t => {
t.case('launch/.app', async t => {
t.stub(os, 'platform').returns('darwin')
t.mock(launch).expects('sh').once().withArgs('Dumbo.app', `open -W "Dumbo.app"`).resolves('Done!')
return launch.launch('Dumbo.app', [])
await launch.launch('Dumbo.app', [])
})

t.case('launch/.app - with args', t => {
t.case('launch/.app - with args', async t => {
t.stub(os, 'platform').returns('darwin')
t.mock(launch).expects('sh').once().withArgs('Dumbo.app', `open -W "Dumbo.app" --args "dumb" "du\\"mber" "frank spencer"`).resolves('Done!')
return launch.launch('Dumbo.app', ['dumb', 'du"mber', 'frank spencer'])
await launch.launch('Dumbo.app', ['dumb', 'du"mber', 'frank spencer'])
})

t.case('launch/binary', t => {
t.case('launch/binary', async t => {
t.mock(launch).expects('sh').once().withArgs('dumbo.exe', `"dumbo.exe"`).resolves('Done!')
return launch.launch('dumbo.exe', [])
await launch.launch('dumbo.exe', [])
})

t.case('launch/binary -with args', t => {
t.case('launch/binary -with args', async t => {
t.mock(launch).expects('sh').once().withArgs('dumbo.exe', `"dumbo.exe" "dumb" "du\\"mber" "frank spencer"`).resolves('Done!')
return launch.launch('dumbo.exe', ['dumb', 'du"mber', 'frank spencer'])
await launch.launch('dumbo.exe', ['dumb', 'du"mber', 'frank spencer'])
})

t.case('launch/unknown', t => {
t.case('launch/unknown', async t => {
t.stub(os, 'platform').returns('irix')
t.mock(launch).expects('sh').once().withArgs('dumbo', `"dumbo"`).resolves('Done!')
return launch.launch('dumbo', [])
})

t.case('sh sanity filter 1', t => {
return t.rejects(launch.sh('dumbo', 'dumbo & fork-bomb', opts))
await launch.launch('dumbo', [])
})

t.case('sh sanity filter 2', t => {
return t.rejects(launch.sh('dumbo', 'dumbo ; evil', opts))
})

t.case('sh error', t => {
t.case('sh error', async t => {
let dummy = make_dummy()
t.mock(child_process).expects('spawn').returns(dummy)
let p = launch.sh('dumbo', 'dumbo --fullscreen --no-sound', opts)
dummy.emit('error')
return t.rejects(p)

await t.rejects(p)
})

t.case('sh successful', t => {
t.case('sh successful', async t => {
let dummy = make_dummy()
t.mock(child_process).expects('spawn').returns(dummy)
let p = launch.sh('dumbo', 'dumbo --fullscreen --no-sound', opts)
dummy.emit('close', 0)
return p
await p
})

t.case('sh non-zero', t => {
t.case('sh non-zero', async t => {
let dummy = make_dummy()
t.mock(child_process).expects('spawn').returns(dummy)
let p = launch.sh('dumbo', 'dumbo --fullscreen --no-sound', opts)
dummy.emit('close', 127)
return t.rejects(p)
await t.rejects(p)
})
})
21 changes: 10 additions & 11 deletions testsrc/util/api-spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


let test = require('zopf')
let sinon = require('sinon')
let proxyquire = require('proxyquire')
Expand All @@ -8,9 +7,8 @@ let electron = require('../stubs/electron')
let cooldown = require('../stubs/cooldown')

test('api', t => {
let request = t.stub().resolves({body: {id: 12}, statusCode: 200})
let needle = {
requestAsync: request
requestAsync: async () => ({body: {id: 12}, statusCode: 200})
}

let stubs = Object.assign({
Expand All @@ -28,11 +26,13 @@ test('api', t => {
let uri = 'http://example.org/yo'

t.case('can GET', async t => {
let request = t.spy(needle, 'requestAsync')
await client.request('GET', 'yo', {b: 11})
sinon.assert.calledWith(request, 'GET', uri, {b: 11})
})

t.case('can POST', async t => {
let request = t.spy(needle, 'requestAsync')
await client.request('POST', 'yo', {b: 22})
sinon.assert.calledWith(request, 'POST', uri, {b: 22})
})
Expand All @@ -45,14 +45,13 @@ test('api', t => {

t.case('rejects API errors', async t => {
let errors = ['foo', 'bar', 'baz']
let spy = t.spy()

request.resolves({body: {errors}, statusCode: 200})
return client.request('GET', '', {}).catch(spy).then(res => {
sinon.assert.calledWith(spy, sinon.match({errors}))
}).then(res => {
request.resolves({body: {id: 42}, statusCode: 200})
})

t.stub(needle, 'requestAsync').resolves({body: {errors}, statusCode: 200})
let err
try {
await client.request('GET', '', {})
} catch (e) { err = e }
t.same(err, {errors})
})

{
Expand Down
24 changes: 8 additions & 16 deletions testsrc/util/auto-updater-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,31 @@ let setup_win32 = t => {
}

test('auto-updater', t => {
t.case('stubbed on darwin', t => {
t.case('stubbed on darwin', async t => {
let r = setup(t)
t.stub(r.os, 'platform').returns('darwin')
Promise.resolve(r.updater.start()).then((r) => {
t.false(r, 'returns false')
})
t.false(await r.updater.start(), 'returns false')
})

t.case('bypassed on linux', t => {
t.case('bypassed on linux', async t => {
let r = setup(t)
t.stub(r.os, 'platform').returns('linux')
Promise.resolve(r.updater.start()).then((r) => {
t.false(r, 'returns false')
})
t.false(await r.updater.start(), 'returns false')
})
})

test('auto-updater/win32', t => {
t.case(`noop if no squirrel command`, t => {
t.case(`noop if no squirrel command`, async t => {
let r = setup(t)
t.stub(r.os, 'platform').returns('win32')
Promise.resolve(r.updater.start()).then((r) => {
t.false(r, 'returns false')
})
t.false(await r.updater.start(), 'returns false')
})

;['install', 'updated', 'uninstall', 'obsolete'].forEach((action) => {
t.case(`quit on ${action}`, t => {
t.case(`quit on ${action}`, async t => {
let r = setup_win32(t)
t.stub(r.os, 'cli_args').returns(['hi mom', `--squirrel-${action}`])
Promise.resolve(r.win32.start()).then((r) => {
t.true(r, 'returns true')
})
t.true(await r.win32.start(), 'returns true')
})
})
})

0 comments on commit 4ca0830

Please sign in to comment.