Skip to content

Commit

Permalink
⭐ [Tests] Replace Mocha with Jest.
Browse files Browse the repository at this point in the history
  • Loading branch information
Duelist committed Mar 28, 2017
1 parent baef27f commit 1f0fbac
Show file tree
Hide file tree
Showing 19 changed files with 228 additions and 376 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Coverage directory used by tools like istanbul
coverage
5 changes: 5 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
{
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": 8
},
"rules": {
"comma-dangle" : [0, "always-multiline"],
"complexity" : [1, 7],
Expand Down Expand Up @@ -59,6 +62,8 @@
"node" : true
},
"globals": {
"expect": true,
"jest": true,
"PRODUCTION" : true,
"requireRoot" : true
},
Expand Down
10 changes: 5 additions & 5 deletions bot/commands/king/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ let redis = requireRoot('utils/redis').initialize()
* Makes the author of the command the king.
* @param {Object} options Message parameters.
*/
function* king(options) {
async function king(options) {

let response

// Get the previous king
let oldKing = yield redis.getString({ key: KING.REDIS_KEY })
let oldKing = await redis.getString({ key: KING.REDIS_KEY })

let author = options.message.author

Expand All @@ -27,15 +27,15 @@ function* king(options) {
}

// Set the new king to the message author
yield redis.setString({ key: KING.REDIS_KEY, value: author.username })
await redis.setString({ key: KING.REDIS_KEY, value: author.username })

// Send the response to the channel it was sent from
yield options.message.channel.createMessage(response)
await options.message.channel.createMessage(response)

}



module.exports = {
message : king
message: king
}
22 changes: 5 additions & 17 deletions bot/commands/metacoins/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
let _ = require('lodash')

let METACOINS = requireRoot('bot/commands/metacoins/constants')
let metabotConfig = requireRoot('bot/configs/metabot')
let services = requireRoot('bot/services')
let METACOINS = requireRoot('bot/commands/metacoins/constants')
let services = requireRoot('bot/services')

let metacoins = services.metacoins.register()

Expand All @@ -12,30 +11,19 @@ let metacoins = services.metacoins.register()
* Gets a user's metacoins or the metacoins leaderboard.
* @param {Object} options
*/
function* message(options) {
async function message(options) {

let author = options.message.author
let channel = options.message.channel

if (!options.args || _.isEmpty(options.args)) {
let coins = yield metacoins.getMetacoinsForUser(author.id)
yield channel.createMessage(
let coins = await metacoins.getMetacoinsForUser(author.id)
await channel.createMessage(
METACOINS.MESSAGE.METACOIN_COUNT(author.mention, coins.toString())
)
return
}

let isAdmin = _.includes(metabotConfig.adminIds, author.id)

// Admin commands
if (isAdmin) {
if (options.args[0] === METACOINS.COMMAND.LEADERBOARD) {
let leaderboard = yield metacoins.getLeaderboard()
yield channel.createMessage(leaderboard)
return
}
}

}


Expand Down
8 changes: 5 additions & 3 deletions bot/commands/ping/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
/**
* Returns a pong for your ping.
* @param {Object} options Message options.
* {Object} [options.args] Received arguments.
* {Message} options.message Received message.
*/
function* ping(options) {
async function ping(options) {
// Send the response to the channel it was sent from
yield options.message.channel.createMessage('pong')
await options.message.channel.createMessage('pong')
}



module.exports = {
message : ping
message: ping
}
16 changes: 8 additions & 8 deletions bot/services/metacoins.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function allowAward(token) {
*
* @return {Boolean}
*/
function* award(token, options) {
async function award(token, options) {

validateUtil(options).has({
amount : {
Expand All @@ -46,14 +46,14 @@ function* award(token, options) {
assert(allowAward(token))

// Award the coins
let awarded = yield redis.incrementScoreInSortedSet({
let awarded = await redis.incrementScoreInSortedSet({
amount : options.amount,
key : METACOINS.REDIS.LEADERBOARD_KEY,
member : options.userId
})

// Update the last time the registered function awarded coins.
yield redis.addToSortedSet({
await redis.addToSortedSet({
key : METACOINS.REDIS.LAST_AWARDED_KEY,
member : token,
score : (new Date()).getTime()
Expand Down Expand Up @@ -91,14 +91,14 @@ function formatLeaderboard(leaderboard) {
* Gets the leaderboard from cache.
* @return {String}
*/
function* getLeaderboard() {
async function getLeaderboard() {

let leaderboardExists = yield redis.exists({
let leaderboardExists = await redis.exists({
key: METACOINS.REDIS.LEADERBOARD_KEY
})

if (leaderboardExists) {
let leaderboard = yield redis.getBatchFromSortedSet({
let leaderboard = await redis.getBatchFromSortedSet({
includeScores : true,
key : METACOINS.REDIS.LEADERBOARD_KEY
})
Expand All @@ -116,9 +116,9 @@ function* getLeaderboard() {
* @param {Number} userId User's id.
* @return {Array}
*/
function* getMetacoinsForUser(userId) {
async function getMetacoinsForUser(userId) {

let metacoins = yield redis.getScoreFromSortedSet({
let metacoins = await redis.getScoreFromSortedSet({
key : METACOINS.REDIS.LEADERBOARD_KEY,
member : userId
})
Expand Down
80 changes: 24 additions & 56 deletions bot/test/commands/king/index.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,77 @@
let sinon = require('sinon')

let commands = requireRoot('bot/commands')



describe('@default', () => {

it('claims the throne if there is no old king', function* () {
test('claims the throne if there is no old king', async () => {

// Create test message options
let options = {
message : {
message: {
author : { username: 'Duelist' },
channel : {
createMessage : function* (message) {
return message
}
createMessage: jest.fn()
},
content : '!king'
content: '!king'
}
}

// Create a spy on the create message function
let createMessageSpy = sinon.spy(options.message.channel, 'createMessage')

// Create an expected result
let expectedResult = 'Duelist has claimed the throne.'

// Run the command
yield commands.king.message(options)
await commands.king.message(options)

// Ensure the create message function was called with the right message
createMessageSpy.calledOnce.should.eql(true)
createMessageSpy.lastCall.args[0].should.eql(expectedResult)

// Clean up
createMessageSpy.restore()
expect(options.message.channel.createMessage).toHaveBeenCalled()
expect(options.message.channel.createMessage.mock.calls[0][0])
.toBe('Duelist has claimed the throne.')

})


it('retains the throne', function* () {
test('retains the throne', async () => {

// Create test message options
let options = {
message : {
message: {
author : { username: 'Duelist' },
channel : {
createMessage : function* (message) {
return message
}
createMessage: jest.fn()
},
content : '!king'
content: '!king'
}
}

// Create a spy on the create message function
let createMessageSpy = sinon.spy(options.message.channel, 'createMessage')

// Create an expected result
let expectedResult = 'Duelist has retained the throne.'

// Run the command
yield commands.king.message(options)
await commands.king.message(options)

// Ensure the create message function was called with the right message
createMessageSpy.calledOnce.should.eql(true)
createMessageSpy.lastCall.args[0].should.eql(expectedResult)

// Clean up
createMessageSpy.restore()
expect(options.message.channel.createMessage).toHaveBeenCalled()
expect(options.message.channel.createMessage.mock.calls[0][0])
.toBe('Duelist has retained the throne.')

})


it('usurps the throne from another player', function* () {
test('usurps the throne from another player', async () => {

// Create test message options
let options = {
message : {
message: {
author : { username: 'Momentum' },
channel : {
createMessage : function* (message) {
return message
}
createMessage: jest.fn()
},
content : '!king'
content: '!king'
}
}

// Create a spy on the create message function
let createMessageSpy = sinon.spy(options.message.channel, 'createMessage')

// Create an expected result
let expectedResult = 'Momentum has usurped the throne from Duelist.'

// Run the command
yield commands.king.message(options)
await commands.king.message(options)

// Ensure the create message function was called with the right message
createMessageSpy.calledOnce.should.eql(true)
createMessageSpy.lastCall.args[0].should.eql(expectedResult)

// Clean up
createMessageSpy.restore()
expect(options.message.channel.createMessage).toHaveBeenCalled()
expect(options.message.channel.createMessage.mock.calls[0][0])
.toBe('Momentum has usurped the throne from Duelist.')

})

Expand Down
Loading

0 comments on commit 1f0fbac

Please sign in to comment.