forked from hash-gaming/robotk
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Currently robotk will come back with a single response at all times. This could get boring and makes things more bureaucratic than ordinary users would like to. We're implementing a specific functionality to register a list of responses and then return a .random() one when requested.
- Loading branch information
Showing
3 changed files
with
77 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Response { | ||
constructor(resp) { | ||
if (resp.constructor === Array) { | ||
this.list = resp; | ||
} | ||
else { | ||
this.list = [resp]; | ||
} | ||
} | ||
|
||
all() { | ||
return this.list; | ||
} | ||
|
||
random() { | ||
return this.list[Math.floor(Math.random() * this.list.length)]; | ||
} | ||
} | ||
|
||
module.exports = Response; |
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,34 @@ | ||
const { expect } = require('chai'); | ||
const Response = require('./response'); | ||
|
||
describe('Response', () => { | ||
context('configured with set of responses', () => { | ||
let response; | ||
beforeEach(() => { | ||
response = new Response(Array(999999).fill().map((_, idx) => 0 + idx)); | ||
}); | ||
|
||
it('should return a random() response correctly', () => { | ||
const a = response.random(); | ||
const b = response.random(); | ||
|
||
expect(a).not.to.equal(b); | ||
}); | ||
}); | ||
|
||
context('configured with a single response', () => { | ||
let response; | ||
beforeEach(() => { | ||
response = new Response('success'); | ||
}); | ||
|
||
it('should return a random() response correctly', () => { | ||
expect(response.random()).to.equal('success'); | ||
}); | ||
|
||
it('should return all() responses in a list', () => { | ||
expect(response.all().length).to.equal(1); | ||
expect(response.all()).to.contain('success'); | ||
}); | ||
}); | ||
}); |
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