Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add example test to hubot --create output (#1710)
- Loading branch information
1 parent
2e65d78
commit a842eb6
Showing
5 changed files
with
235 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { describe, it, beforeEach, afterEach } from 'node:test' | ||
import assert from 'node:assert/strict' | ||
|
||
// Replace this with import { Robot } from 'hubot' | ||
import { Robot } from '../index.mjs' | ||
|
||
// You need a dummy adapter to test scripts | ||
import dummyRobot from './doubles/DummyAdapter.mjs' | ||
|
||
// Mocks Aren't Stubs | ||
// https://www.martinfowler.com/articles/mocksArentStubs.html | ||
|
||
describe('Xample testing Hubot scripts', () => { | ||
let robot = null | ||
beforeEach(async () => { | ||
robot = new Robot(dummyRobot, false, 'Dumbotheelephant') | ||
await robot.loadAdapter() | ||
await robot.loadFile('./test/scripts', 'Xample.mjs') | ||
await robot.run() | ||
}) | ||
afterEach(() => { | ||
robot.shutdown() | ||
}) | ||
it('should reply with expected message', async () => { | ||
const expected = 'HELO World! I\'m Dumbotheelephant.' | ||
const user = robot.brain.userForId('test-user', { name: 'test user' }) | ||
let actual = '' | ||
robot.on('reply', (envelope, ...strings) => { | ||
actual = strings.join('') | ||
}) | ||
await robot.adapter.say(user, '@Dumbotheelephant helo', 'test-room') | ||
assert.strictEqual(actual, expected) | ||
}) | ||
|
||
it('should send message to the #general room', async () => { | ||
const expected = 'general' | ||
const user = robot.brain.userForId('test-user', { name: 'test user' }) | ||
let actual = '' | ||
robot.on('send', (envelope, ...strings) => { | ||
actual = envelope.room | ||
}) | ||
await robot.adapter.say(user, '@Dumbotheelephant helo room', 'general') | ||
assert.strictEqual(actual, expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
'use strict' | ||
// Replace this with import { Adapter, TextMessage } from 'hubot' | ||
import { Adapter, TextMessage } from '../../index.mjs' | ||
|
||
export class DummyAdapter extends Adapter { | ||
constructor (robot) { | ||
super(robot) | ||
this.name = 'DummyAdapter' | ||
this.messages = new Set() | ||
} | ||
|
||
async send (envelope, ...strings) { | ||
this.emit('send', envelope, ...strings) | ||
this.robot.emit('send', envelope, ...strings) | ||
} | ||
|
||
async reply (envelope, ...strings) { | ||
this.emit('reply', envelope, ...strings) | ||
this.robot.emit('reply', envelope, ...strings) | ||
} | ||
|
||
async topic (envelope, ...strings) { | ||
this.emit('topic', envelope, ...strings) | ||
this.robot.emit('topic', envelope, ...strings) | ||
} | ||
|
||
async play (envelope, ...strings) { | ||
this.emit('play', envelope, ...strings) | ||
this.robot.emit('play', envelope, ...strings) | ||
} | ||
|
||
run () { | ||
// This is required to get the scripts loaded | ||
this.emit('connected') | ||
} | ||
|
||
close () { | ||
this.emit('closed') | ||
} | ||
|
||
async say (user, message, room) { | ||
this.messages.add(message) | ||
user.room = room | ||
await this.robot.receive(new TextMessage(user, message)) | ||
} | ||
} | ||
export default { | ||
use (robot) { | ||
return new DummyAdapter(robot) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Description: | ||
// Test script | ||
// | ||
// Commands: | ||
// hubot helo - Responds with HELO World!. | ||
// | ||
// Notes: | ||
// This is a test script. | ||
// | ||
|
||
export default (robot) => { | ||
robot.respond(/helo$/, async res => { | ||
await res.reply(`HELO World! I'm ${robot.name}.`) | ||
}) | ||
robot.respond(/helo (.*)/gi, async res => { | ||
await res.send(`Hello World! I'm ${robot.name}.`) | ||
}) | ||
} |