Cleanup tests

This commit is contained in:
Juan Cruz Viotti 2014-11-14 15:48:37 -04:00
parent 8143fa3ab3
commit 58cc0be0c9
8 changed files with 60 additions and 25 deletions

View File

@ -3,11 +3,18 @@ nock = require('nock')
async = require('async')
auth = require('./auth')
config = require('../../config')
mock = require('../../../tests/utils/mock')
johnDoeFixture = require('../../../tests/fixtures/johndoe')
janeDoeFixture = require('../../../tests/fixtures/janedoe')
describe 'Auth:', ->
beforeEach ->
mock.fs.init()
afterEach ->
mock.fs.restore()
describe 'given valid credentials', ->
beforeEach ->

View File

@ -2,3 +2,4 @@ module.exports =
# TODO: Should be configurable
baseUrl: 'https://staging.resin.io'
dataPrefix: '~/.resin'

View File

@ -1,11 +1,12 @@
expect = require('chai').expect
_ = require('lodash')
dataPrefix = require('./data-prefix')
config = require('../config')
PREFIXES =
main: '~/.resin'
new: '~/.resin-new'
invalid: { path: '~/.resin' }
main: config.dataPrefix
new: "#{config.dataPrefix}-new"
invalid: { path: '/abc' }
describe 'DataPrefix:', ->

View File

@ -1,12 +1,11 @@
expect = require('chai').expect
_ = require('lodash')
fsUtils = require('../fs-utils/fs-utils')
mock = require('../../tests/utils/mock')
async = require('async')
mockFs = require('mock-fs')
config = require('../config')
data = require('./data')
PREFIX = '~/.resin'
FILES_FIXTURES =
hello:
filename: 'hello_world.test'
@ -17,15 +16,15 @@ FILES_FIXTURES =
FILESYSTEM =
text:
name: "#{PREFIX}/text"
name: "#{config.dataPrefix}/text"
contents: 'Hello World'
key: 'text'
directory:
name: "#{PREFIX}/directory"
contents: mockFs.directory()
name: "#{config.dataPrefix}/directory"
contents: {}
key: 'directory'
nested:
name: "#{PREFIX}/nested/text"
name: "#{config.dataPrefix}/nested/text"
contents: 'Nested Hello World'
key: 'nested/text'
@ -60,15 +59,11 @@ describe 'Data:', ->
describe 'given a prefix', ->
beforeEach ->
mockFsOptions = {}
for key, value of FILESYSTEM
mockFsOptions[value.name] = value.contents
mockFs(mockFsOptions)
data.prefix.set(PREFIX)
mock.fs.init(FILESYSTEM)
data.prefix.set(config.dataPrefix)
afterEach ->
mockFs.restore()
mock.fs.restore()
data.prefix.clear()
describe '#get()', ->

View File

@ -1,6 +1,8 @@
expect = require('chai').expect
mockFs = require('mock-fs')
mock = require('../../tests/utils/mock')
fsUtils = require('./fs-utils')
config = require('../config')
data = require('../data/data')
FILESYSTEM =
text:
@ -8,7 +10,7 @@ FILESYSTEM =
contents: 'Hello World'
directory:
name: '/tmp/directory'
contents: mockFs.directory()
contents: {}
describe 'FsUtils:', ->
@ -28,8 +30,8 @@ describe 'FsUtils:', ->
it 'should return true for valid paths', ->
for validPath in [
config.dataPrefix
'/Users/johndoe'
'~/.resin'
'../parent'
'./file/../file2'
]
@ -38,13 +40,11 @@ describe 'FsUtils:', ->
describe '#isDirectory()', ->
beforeEach ->
mockFsOptions = {}
for key, value of FILESYSTEM
mockFsOptions[value.name] = value.contents
mockFs(mockFsOptions)
mock.fs.init(FILESYSTEM)
data.prefix.set(config.dataPrefix)
afterEach ->
mockFs.restore()
mock.fs.restore()
it 'should return true if directory', (done) ->
fsUtils.isDirectory FILESYSTEM.directory.name, (error, isDirectory) ->

View File

@ -3,6 +3,7 @@ nock = require('nock')
server = require('./server')
config = require('../config')
token = require('../token/token')
mock = require('../../tests/utils/mock')
johnDoeFixture = require('../../tests/fixtures/johndoe.json')
TEST_URI = config.baseUrl
@ -38,6 +39,11 @@ describe 'Server:', ->
lowercaseMethod = method.toLowerCase()
nock(TEST_URI)[lowercaseMethod](URI.ok).reply(200, status: STATUS.ok)
mock.fs.init()
afterEach ->
mock.fs.restore()
describe '#request()', ->
it 'should make a real HTTP request', (done) ->

View File

@ -1,12 +1,19 @@
expect = require('chai').expect
async = require('async')
token = require('./token')
mock = require('../../tests/utils/mock')
johnDoeFixture = require('../../tests/fixtures/johndoe.json')
janeDoeFixture = require('../../tests/fixtures/janedoe.json')
describe 'Token:', ->
beforeEach ->
mock.fs.init()
afterEach ->
mock.fs.restore()
describe 'given a user that is logged in', ->
beforeEach (done) ->

18
tests/utils/mock.coffee Normal file
View File

@ -0,0 +1,18 @@
mockFs = require('mock-fs')
config = require('../../lib/config')
exports.fs =
init: (filesystemConfig = {}) ->
mockFsOptions = {}
# Mock data prefix automatically to remove
# duplication in most of the tests
mockFsOptions[config.dataPrefix] = mockFs.directory()
for key, value of filesystemConfig
mockFsOptions[value.name] = value.contents
mockFs(mockFsOptions)
restore: ->
mockFs.restore()