x-team / vacation-status-update

Slack integration helping to communicate your vacation time off to team members
MIT License
11 stars 7 forks source link

Tests #105

Open jacekelgda opened 6 years ago

jacekelgda commented 6 years ago

There is one test added in this project as example. Take a look inside ./util/__tests__. We need to add unit tests in some parts of this application. There is no good idea to create tests for handlers and controllers right now so discussion is more than welcome.

andrewgreenh commented 6 years ago

For the controllers, I have a couple of suggestions:

jacekelgda commented 6 years ago

Good ideas @andreasgruenh ! Would you be willing to create a proof of concept for your suggestions ? Do you have any ideas for testing RTM API ( websockets ) for slack part ?

andrewgreenh commented 6 years ago

It could look something like this:

Advantages of this solution:

Disadvantages

controllers/composeApplyToRouter.js

export default const composeApplyToRouter = ({ method, path }) => function(router) {
    router[method](path, this)
}

controllers/auth.js

import composeApplyToRouter from './composeApplyToRouter'

const composeAuth = ({
    exchangeCodeForToken,
    storeTeamToken,
    createNewBotConnection,
    sendPostInstallMessage,
}) => {
    async function auth(req, res) {
        try {
            const token = await exchangeCodeForToken(req.query.code)
            storeTeamToken(token)
            const bot = createNewBotConnection({
                token: token.bot.bot_access_token,
                team: token.team_id
            })
            sendPostInstallMessage(bot, token.user_id, token.team_id)
            res.send('Thank you for authorizing our application')
        } catch (e) {
            res.send('Error. Invalid/expired code.')
        }
    }
    auth.applyToRouter = composeApplyToRouter({ method: 'get', path: '/auth' })
    return auth
}

export default composeAuth

routes.js

import { Router } from 'express'
import { exchangeCodeForToken } from '../handlers/api'
import { storeTeamToken } from '../handlers/store'
import { createNewBotConnection, sendPostInstallMessage } from '../handlers/bot'
import composeAuth from './auth'

const router = new Router()

composeAuth({
    exchangeCodeForToken,
    storeTeamToken,
    createNewBotConnection,
    sendPostInstallMessage
}).applyToRouter(router)
RafalWilinski commented 6 years ago

Great suggestions @andreasgruenh, thanks for valuable input! 💯

Your suggestion definitely makes sense, I'm also not a big fan of overwriting require calls so I'd rather proceed with the flow proposed by you.

Regarding the first disadvantage - I don't think that putting that code into router is a big issue.