2020-06-23 13:30:34 -04:00
/ * e s l i n t - d i s a b l e
camelcase ,
handle - callback - err ,
no - return - assign ,
no - unused - vars ,
* /
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
2020-06-23 13:30:29 -04:00
/ *
* decaffeinate suggestions :
* DS101 : Remove unnecessary use of Array . from
* DS102 : Remove unnecessary code created because of implicit returns
* DS207 : Consider shorter variations of null checks
* Full docs : https : //github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
* /
const chai = require ( "chai" ) ;
const {
expect
} = chai ;
chai . should ( ) ;
const sinon = require ( "sinon" ) ;
2014-11-13 06:54:10 -05:00
2020-06-23 13:30:29 -04:00
const RealTimeClient = require ( "./helpers/RealTimeClient" ) ;
const MockDocUpdaterServer = require ( "./helpers/MockDocUpdaterServer" ) ;
const FixturesManager = require ( "./helpers/FixturesManager" ) ;
const logger = require ( "logger-sharelatex" ) ;
2014-11-13 06:54:10 -05:00
2020-06-23 13:30:29 -04:00
const async = require ( "async" ) ;
2014-11-14 05:21:54 -05:00
2020-06-23 13:30:29 -04:00
describe ( "leaveDoc" , function ( ) {
before ( function ( ) {
this . lines = [ "test" , "doc" , "lines" ] ;
this . version = 42 ;
this . ops = [ "mock" , "doc" , "ops" ] ;
sinon . spy ( logger , "error" ) ;
sinon . spy ( logger , "warn" ) ;
sinon . spy ( logger , "log" ) ;
return this . other _doc _id = FixturesManager . getRandomId ( ) ;
} ) ;
2019-07-26 03:07:49 -04:00
2020-06-23 13:30:29 -04:00
after ( function ( ) {
logger . error . restore ( ) ; // remove the spy
logger . warn . restore ( ) ;
return logger . log . restore ( ) ;
} ) ;
2020-05-19 12:41:20 -04:00
2020-06-23 13:30:29 -04:00
return describe ( "when joined to a doc" , function ( ) {
beforeEach ( function ( done ) {
return async . series ( [
cb => {
return FixturesManager . setUpProject ( {
2014-11-14 05:21:54 -05:00
privilegeLevel : "readAndWrite"
2020-06-23 13:30:29 -04:00
} , ( e , { project _id , user _id } ) => {
this . project _id = project _id ;
this . user _id = user _id ;
return cb ( e ) ;
} ) ;
} ,
2014-11-14 05:21:54 -05:00
2020-06-23 13:30:29 -04:00
cb => {
return FixturesManager . setUpDoc ( this . project _id , { lines : this . lines , version : this . version , ops : this . ops } , ( e , { doc _id } ) => {
this . doc _id = doc _id ;
return cb ( e ) ;
} ) ;
} ,
2014-11-14 05:21:54 -05:00
2020-06-23 13:30:29 -04:00
cb => {
this . client = RealTimeClient . connect ( ) ;
return this . client . on ( "connectionAccepted" , cb ) ;
} ,
2014-11-14 05:21:54 -05:00
2020-06-23 13:30:29 -04:00
cb => {
return this . client . emit ( "joinProject" , { project _id : this . project _id } , cb ) ;
} ,
2014-11-14 05:21:54 -05:00
2020-06-23 13:30:29 -04:00
cb => {
return this . client . emit ( "joinDoc" , this . doc _id , ( error , ... rest ) => { [ ... this . returnedArgs ] = Array . from ( rest ) ; return cb ( error ) ; } ) ;
}
] , done ) ;
} ) ;
2014-11-13 06:54:10 -05:00
2020-06-23 13:30:29 -04:00
describe ( "then leaving the doc" , function ( ) {
beforeEach ( function ( done ) {
2020-06-23 13:30:34 -04:00
return this . client . emit ( "leaveDoc" , this . doc _id , ( error ) => {
2020-06-23 13:30:29 -04:00
if ( error != null ) { throw error ; }
return done ( ) ;
} ) ;
} ) ;
2014-11-13 06:54:10 -05:00
2020-06-23 13:30:29 -04:00
return it ( "should have left the doc room" , function ( done ) {
return RealTimeClient . getConnectedClient ( this . client . socket . sessionid , ( error , client ) => {
expect ( Array . from ( client . rooms ) . includes ( this . doc _id ) ) . to . equal ( false ) ;
return done ( ) ;
} ) ;
} ) ;
} ) ;
2019-07-26 03:07:49 -04:00
2020-06-23 13:30:29 -04:00
describe ( "when sending a leaveDoc request before the previous joinDoc request has completed" , function ( ) {
beforeEach ( function ( done ) {
2020-06-23 13:30:34 -04:00
this . client . emit ( "leaveDoc" , this . doc _id , ( ) => { } ) ;
this . client . emit ( "joinDoc" , this . doc _id , ( ) => { } ) ;
return this . client . emit ( "leaveDoc" , this . doc _id , ( error ) => {
2020-06-23 13:30:29 -04:00
if ( error != null ) { throw error ; }
return done ( ) ;
} ) ;
} ) ;
2019-07-26 03:07:49 -04:00
2020-06-23 13:30:34 -04:00
it ( "should not trigger an error" , function ( ) { return sinon . assert . neverCalledWith ( logger . error , sinon . match . any , "not subscribed - shouldn't happen" ) ; } ) ;
2019-07-26 03:07:49 -04:00
2020-06-23 13:30:29 -04:00
return it ( "should have left the doc room" , function ( done ) {
return RealTimeClient . getConnectedClient ( this . client . socket . sessionid , ( error , client ) => {
expect ( Array . from ( client . rooms ) . includes ( this . doc _id ) ) . to . equal ( false ) ;
return done ( ) ;
} ) ;
} ) ;
} ) ;
2019-07-29 10:19:08 -04:00
2020-06-23 13:30:29 -04:00
return describe ( "when sending a leaveDoc for a room the client has not joined " , function ( ) {
beforeEach ( function ( done ) {
2020-06-23 13:30:34 -04:00
return this . client . emit ( "leaveDoc" , this . other _doc _id , ( error ) => {
2020-06-23 13:30:29 -04:00
if ( error != null ) { throw error ; }
return done ( ) ;
} ) ;
} ) ;
2019-07-29 10:19:08 -04:00
2020-06-23 13:30:34 -04:00
return it ( "should trigger a low level message only" , function ( ) { return sinon . assert . calledWith ( logger . log , sinon . match . any , "ignoring request from client to leave room it is not in" ) ; } ) ;
2020-06-23 13:30:29 -04:00
} ) ;
} ) ;
} ) ;