socket_io.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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(() => sequelize.sync({ force: true }))
  19. describe('new thread notifications', async () => {
  20. let agent = chai.request.agent(server)
  21. //Create mock threads and posts
  22. before(async () => {
  23. await agent
  24. .post('/api/v1/user')
  25. .set('content-type', 'application/json')
  26. .send({
  27. username: 'adminaccount',
  28. password: 'password',
  29. admin: true
  30. })
  31. await agent
  32. .post('/api/v1/category')
  33. .set('content-type', 'application/json')
  34. .send({ name: 'category_name' })
  35. for(var i = 0; i < 3; i++) {
  36. let thread = await agent
  37. .post('/api/v1/thread')
  38. .set('content-type', 'application/json')
  39. .send({ name: `THREAD ${i}` , category: 'category_name' })
  40. await agent
  41. .post('/api/v1/post')
  42. .set('content-type', 'application/json')
  43. .send({ threadId: thread.body.id , content: `POST ${i}` })
  44. }
  45. })
  46. it('should emit a notification when a thread is created ', (done) => {
  47. let client = io.connect('http://localhost:8080')
  48. //Post new thread
  49. agent
  50. .post('/api/v1/thread')
  51. .set('content-type', 'application/json')
  52. .send({ name: `THREAD 3` , category: 'category_name' })
  53. .then(res => {
  54. return agent
  55. .post('/api/v1/post')
  56. .set('content-type', 'application/json')
  57. .send({ threadId: res.body.id , content: `POST 3` })
  58. })
  59. .catch(done)
  60. client.on('new thread', data => {
  61. data.should.have.property('category', 'category_name')
  62. done()
  63. })
  64. })
  65. })
  66. })