socket_io.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. process.env.NODE_ENV = 'test'
  2. let chai = require('chai')
  3. let should = chai.should()
  4. chai.use(require('chai-http'))
  5. chai.use(require('chai-things'))
  6. let server = require('../server')
  7. let io = require('socket.io-client');
  8. let { sequelize } = require('../models')
  9. const Errors = require('../lib/errors.js')
  10. describe('Socket-io', () => {
  11. //Wait for app to start before commencing
  12. before((done) => {
  13. if(server.locals.appStarted) done()
  14. server.on('appStarted', () => { done() })
  15. })
  16. //Delete all rows in table after
  17. //tests completed
  18. after(() => {
  19. sequelize.sync({ force: true })
  20. })
  21. describe('new thread notifications', async () => {
  22. let agent = chai.request.agent(server)
  23. //Create mock threads and posts
  24. before(async () => {
  25. await agent
  26. .post('/api/v1/user')
  27. .set('content-type', 'application/json')
  28. .send({
  29. username: 'adminaccount',
  30. password: 'password',
  31. admin: true
  32. })
  33. await agent
  34. .post('/api/v1/category')
  35. .set('content-type', 'application/json')
  36. .send({ name: 'category_name' })
  37. for(var i = 0; i < 3; i++) {
  38. let thread = await agent
  39. .post('/api/v1/thread')
  40. .set('content-type', 'application/json')
  41. .send({ name: `THREAD ${i}` , category: 'category_name' })
  42. await agent
  43. .post('/api/v1/post')
  44. .set('content-type', 'application/json')
  45. .send({ threadId: thread.body.id , content: `POST ${i}` })
  46. }
  47. })
  48. it('should emit a notification when a thread is created ', (done) => {
  49. let client = io.connect('http://localhost:3000')
  50. client.on('new thread', data => {
  51. data.should.have.property('category', 'category_name')
  52. done()
  53. })
  54. //Post new thread
  55. agent
  56. .post('/api/v1/thread')
  57. .set('content-type', 'application/json')
  58. .send({ name: `THREAD 3` , category: 'category_name' })
  59. .then(res => {
  60. return agent
  61. .post('/api/v1/post')
  62. .set('content-type', 'application/json')
  63. .send({ threadId: res.body.id , content: `POST 3` })
  64. })
  65. .catch(done)
  66. })
  67. })
  68. })