Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

Commit

Permalink
Add tests about components
Browse files Browse the repository at this point in the history
  • Loading branch information
ks888 committed Mar 20, 2017
1 parent 3101961 commit 7df6318
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 359 deletions.
9 changes: 8 additions & 1 deletion packages/lambda/.babelrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"presets": ["es2015", "stage-0"],
"plugins": ["transform-runtime"]
"plugins": ["transform-runtime"],
"env": {
"development": {
"presets": [
"power-assert"
]
}
}
}
6 changes: 3 additions & 3 deletions packages/lambda/bin/release.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import path from 'path'
import fs from 'fs'
import { execSync } from 'child_process'
import { putObject, listObjects } from '../src/utils/s3'
import S3 from '../src/aws/s3'
import packageJSON from '../package.json'

const stopIfObjectsExist = (process.argv[2] !== '--force')
Expand Down Expand Up @@ -45,7 +45,7 @@ const release = async () => {
await Promise.all(regions.map(async (region) => {
const bucketName = 'lambstatus-' + region
const keyName = `fn/${packageJSON.version}`
const objectKeys = await listObjects(region, bucketName, keyName)
const objectKeys = await new S3().listObjects(region, bucketName, keyName)
if (objectKeys.length !== 0) {
throw new Error(`objects already exist under ${bucketName}/${keyName}`)
}
Expand All @@ -56,7 +56,7 @@ const release = async () => {
const body = fs.readFileSync(`${buildDir}/${func}.zip`)
await Promise.all(regions.map(async (region) => {
const bucketName = 'lambstatus-' + region
await putObject(region, bucketName, objectName, body)
await new S3().putObject(region, bucketName, objectName, body)
}))
console.log(`uploaded: ${func}.zip`)
}))
Expand Down
3 changes: 2 additions & 1 deletion packages/lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"aws-sdk-mock": "^1.6.1",
"babel-cli": "^6.18.0",
"babel-eslint": "^6.1.2",
"chai": "^3.5.0",
"babel-preset-power-assert": "^1.0.0",
"eslint": "^3.3.1",
"eslint-config-standard": "^6.0.0-beta.3",
"eslint-config-standard-react": "^3.0.0",
Expand All @@ -56,6 +56,7 @@
"eslint-plugin-react": "^6.1.2",
"eslint-plugin-standard": "^2.0.0",
"mocha": "^3.1.2",
"power-assert": "^1.4.2",
"sinon": "^1.17.6"
}
}
30 changes: 30 additions & 0 deletions packages/lambda/test/api/getComponents/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import assert from 'assert'
import sinon from 'sinon'
import { handle } from 'api/getComponents'
import { Components, Component } from 'model/components'

describe('getComponents', () => {
afterEach(() => {
Components.prototype.all.restore()
})

it('should return a list of components', async () => {
const components = [
new Component('2', undefined, undefined, undefined, 2),
new Component('1', undefined, undefined, undefined, 1)
]
sinon.stub(Components.prototype, 'all').returns(components)

return await handle({}, null, (error, result) => {
assert(error === null)
assert(result === JSON.stringify([{componentID: '1', order: 1}, {componentID: '2', order: 2}]))
})
})

it('should return error on exception thrown', async () => {
sinon.stub(Components.prototype, 'all').throws()
return await handle({}, null, (error, result) => {
assert(error.match(/Error/))
})
})
})
192 changes: 0 additions & 192 deletions packages/lambda/test/db/component.js

This file was deleted.

94 changes: 94 additions & 0 deletions packages/lambda/test/db/components.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import assert from 'assert'
import AWS from 'aws-sdk-mock'
import ComponentsStore from 'db/components'

describe('ComponentsStore', () => {
describe('getAll', () => {
afterEach(() => {
AWS.restore('DynamoDB.DocumentClient')
})

it('should return a list of components', async () => {
AWS.mock('DynamoDB.DocumentClient', 'scan', (params, callback) => {
callback(null, {Items: [{componentID: '1', name: '', description: '', status: '', order: 1}]})
})
const comps = await new ComponentsStore().getAll()
assert(comps.length === 1)
assert(comps[0].componentID === '1')
assert(comps[0].description === '')
})

it('should call reject on error', async () => {
AWS.mock('DynamoDB.DocumentClient', 'scan', (params, callback) => {
callback('Error')
})

let error
try {
await new ComponentsStore().getAll()
} catch (e) {
error = e
}
assert(error.message.match(/Error/))
})
})

describe('getByID', () => {
afterEach(() => {
AWS.restore('DynamoDB.DocumentClient')
})

it('should return a component', async () => {
AWS.mock('DynamoDB.DocumentClient', 'query', (params, callback) => {
callback(null, {Items: [{componentID: '1', name: '', description: '', status: '', order: 1}]})
})
const comps = await new ComponentsStore().getByID('1')
assert(comps.length === 1)
assert(comps[0].componentID === '1')
assert(comps[0].description === '')
})

it('should call reject on error', async () => {
AWS.mock('DynamoDB.DocumentClient', 'query', (params, callback) => {
callback('Error')
})

let error
try {
await new ComponentsStore().getByID()
} catch (e) {
error = e
}
assert(error.message.match(/Error/))
})
})

describe('update', () => {
afterEach(() => {
AWS.restore('DynamoDB.DocumentClient')
})

it('should update the component', async () => {
AWS.mock('DynamoDB.DocumentClient', 'update', (params, callback) => {
callback(null, {Attributes: {componentID: '1', name: '', description: '', status: '', order: 1}})
})
const comp = await new ComponentsStore().update('1', '', '', '', 1)
assert(comp.componentID === '1')
assert(comp.description === '')
})

it('should return error on exception thrown', async () => {
AWS.mock('DynamoDB.DocumentClient', 'update', (params, callback) => {
callback('Error')
})

let error
try {
await new ComponentsStore().update('1', '', '', '', 1)
} catch (e) {
error = e
}
assert(error.message.match(/Error/))
})
})
})

0 comments on commit 7df6318

Please sign in to comment.