socket_io.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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.emit('join', 'index')
  51. client.on('new thread', data => {
  52. data.should.have.property('name', 'category_name')
  53. done()
  54. })
  55. //Post new thread
  56. agent
  57. .post('/api/v1/thread')
  58. .set('content-type', 'application/json')
  59. .send({ name: `THREAD 3` , category: 'category_name' })
  60. .then(res => {
  61. return agent
  62. .post('/api/v1/post')
  63. .set('content-type', 'application/json')
  64. .send({ threadId: res.body.id , content: `POST 3` })
  65. })
  66. .catch(done)
  67. })
  68. it('should emit a notification when a notification is emitted', (done) => {
  69. let client = io.connect('http://localhost:3000')
  70. client.on('notification', data => {
  71. data.should.have.property('type', 'mention')
  72. done()
  73. })
  74. //Post new thread
  75. agent
  76. .post('/api/v1/thread')
  77. .set('content-type', 'application/json')
  78. .send({ name: `THREAD 3` , category: 'category_name' })
  79. .then(res => {
  80. return agent
  81. .post('/api/v1/post')
  82. .set('content-type', 'application/json')
  83. .send({ threadId: res.body.id , content: `POST 4`, mentions: ['adminaccount'] })
  84. })
  85. .catch(done)
  86. })
  87. })
  88. })