import { expect } from "chai" ; import "mocha" ; import * as sinon from "sinon" ; import { ReplyContext } from "../src/reply_context" ; import { SlackService } from "../src/services/slack_service" ; const conversationsListJson = require ( "./slack/conversations_list.json" ) ; const teamInfoJson = require ( "./slack/team_info.json" ) ; const usersListJson = require ( "./slack/users_list.json" ) ; const botkit = require ( "botkit" ) ; describe ( "Slack Service" , ( ) => { let webClient ; beforeEach ( ( ) => { const bot = { startRTM : ( ) => ( { api : { channels : { list : ( opts , fn ) => { throw Error ( "This function is deprecated and should not be called." ) ; } , } , users : { list : ( opts , fn ) => { return fn ( null , usersListJson ) ; } , } , team : { info : sinon . stub ( ) . returns ( teamInfoJson ) , } , } , } ) , } ; webClient = { conversations : { list : async ( ) => conversationsListJson , } , } ; const slackbot = { on : sinon . stub ( ) . returns ( { } ) , setupWebserver : sinon . stub ( ) . returns ( { } ) , spawn : sinon . stub ( ) . returns ( bot ) , } ; sinon . stub ( botkit , "slackbot" ) . returns ( slackbot ) ; } ) ; it ( "usableChannels works" , async ( ) => { const service = new SlackService ( ) ; sinon . stub ( service , "getSlackWebClient" as any ) . returns ( webClient ) ; service . begin ( { listeners : [ ] , messageHandler : ( context ) => undefined , urlHandler : ( context , url ) => undefined , } ) ; const result = await service . usableChannels ( ) ; expect ( result . length ) . to . equal ( 4 ) ; expect ( result ) . to . deep . equal ( [ { id : "C0G9QKBBL" , label : "#general" } , { id : "C0G9QF9GW" , label : "#random" } , { id : "W07QCRPA4" , label : "@glinda" } , { id : "W012A3CDE" , label : "@spengler" } , ] ) ; } ) ; } ) ;