2017-06-27 14:55:11 +02:00
set _global ( '$' , global . make _zjquery ( ) ) ;
2017-06-29 20:46:27 +02:00
set _global ( 'i18n' , global . stub _i18n ) ;
2017-02-24 16:18:56 +01:00
set _global ( 'page_params' , {
use _websockets : false ,
} ) ;
set _global ( 'document' , {
location : {
} ,
} ) ;
2017-06-28 19:24:53 +02:00
set _global ( 'channel' , { } ) ;
2017-06-28 20:28:18 +02:00
set _global ( 'templates' , { } ) ;
2017-02-24 16:18:56 +01:00
2017-06-27 14:55:11 +02:00
var noop = function ( ) { } ;
2017-06-29 06:54:10 +02:00
set _global ( 'blueslip' , { } ) ;
set _global ( 'drafts' , {
delete _draft _after _send : noop ,
} ) ;
set _global ( 'resize' , {
resize _bottom _whitespace : noop ,
} ) ;
set _global ( 'feature_flags' , {
resize _bottom _whitespace : noop ,
} ) ;
set _global ( 'echo' , { } ) ;
2017-02-24 16:18:56 +01:00
add _dependencies ( {
2017-06-27 14:55:11 +02:00
common : 'js/common' ,
2017-04-15 01:15:59 +02:00
compose _state : 'js/compose_state' ,
2017-06-29 06:54:10 +02:00
compose _ui : 'js/compose_ui.js' ,
2017-06-28 13:10:46 +02:00
Handlebars : 'handlebars' ,
2017-02-24 23:51:23 +01:00
people : 'js/people' ,
2017-02-24 16:18:56 +01:00
stream _data : 'js/stream_data' ,
util : 'js/util' ,
} ) ;
var compose = require ( 'js/compose.js' ) ;
2017-02-24 23:51:23 +01:00
var me = {
email : 'me@example.com' ,
user _id : 30 ,
full _name : 'Me Myself' ,
} ;
var alice = {
email : 'alice@example.com' ,
user _id : 31 ,
full _name : 'Alice' ,
} ;
var bob = {
email : 'bob@example.com' ,
user _id : 32 ,
full _name : 'Bob' ,
} ;
people . add ( me ) ;
people . initialize _current _user ( me . user _id ) ;
people . add ( alice ) ;
people . add ( bob ) ;
2017-06-28 21:52:36 +02:00
( function test _update _email ( ) {
compose _state . recipient ( '' ) ;
assert . equal ( compose . update _email ( ) , undefined ) ;
compose _state . recipient ( 'bob@example.com' ) ;
compose . update _email ( 32 , 'bob_alias@example.com' ) ;
assert . equal ( compose _state . recipient ( ) , 'bob_alias@example.com' ) ;
} ( ) ) ;
2017-06-28 12:55:04 +02:00
( function test _validate _stream _message _address _info ( ) {
var sub = {
stream _id : 101 ,
name : 'social' ,
subscribed : true ,
} ;
stream _data . add _sub ( 'social' , sub ) ;
assert ( compose . validate _stream _message _address _info ( 'social' ) ) ;
2017-06-28 13:10:46 +02:00
$ ( '#stream' ) . select ( noop ) ;
assert ( ! compose . validate _stream _message _address _info ( 'foobar' ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , "<p>The stream <b>foobar</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>" ) ;
2017-06-28 19:24:53 +02:00
sub . subscribed = false ;
stream _data . add _sub ( 'social' , sub ) ;
assert ( ! compose . validate _stream _message _address _info ( 'social' ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , "<p>You're not subscribed to the stream <b>social</b>.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>" ) ;
global . page _params . narrow _stream = false ;
channel . post = function ( payload ) {
assert . equal ( payload . data . stream , 'social' ) ;
payload . data . subscribed = true ;
payload . success ( payload . data ) ;
} ;
assert ( compose . validate _stream _message _address _info ( 'social' ) ) ;
sub . name = 'Frontend' ;
sub . stream _id = 102 ;
stream _data . add _sub ( 'Frontend' , sub ) ;
channel . post = function ( payload ) {
assert . equal ( payload . data . stream , 'Frontend' ) ;
payload . data . subscribed = false ;
payload . success ( payload . data ) ;
} ;
assert ( ! compose . validate _stream _message _address _info ( 'Frontend' ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , "<p>You're not subscribed to the stream <b>Frontend</b>.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>" ) ;
channel . post = function ( payload ) {
assert . equal ( payload . data . stream , 'Frontend' ) ;
payload . error ( { status : 404 } ) ;
} ;
assert ( ! compose . validate _stream _message _address _info ( 'Frontend' ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , "<p>The stream <b>Frontend</b> does not exist.</p><p>Manage your subscriptions <a href='#streams/all'>on your Streams page</a>.</p>" ) ;
channel . post = function ( payload ) {
assert . equal ( payload . data . stream , 'social' ) ;
payload . error ( { status : 500 } ) ;
} ;
assert ( ! compose . validate _stream _message _address _info ( 'social' ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( "Error checking subscription" ) ) ;
2017-06-28 12:55:04 +02:00
} ( ) ) ;
2017-02-24 23:51:23 +01:00
2017-06-27 14:55:11 +02:00
( function test _validate ( ) {
$ ( "#compose-send-button" ) . removeAttr ( 'disabled' ) ;
$ ( "#compose-send-button" ) . focus ( ) ;
$ ( "#sending-indicator" ) . hide ( ) ;
$ ( "#new_message_content" ) . select ( noop ) ;
assert ( ! compose . validate ( ) ) ;
assert ( ! $ ( "#sending-indicator" ) . visible ( ) ) ;
assert ( ! $ ( "#compose-send-button" ) . is _focused ( ) ) ;
assert . equal ( $ ( "#compose-send-button" ) . attr ( 'disabled' ) , undefined ) ;
2017-06-29 20:48:49 +02:00
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'You have nothing to send!' ) ) ;
2017-06-27 14:55:11 +02:00
$ ( "#new_message_content" ) . val ( 'foobarfoobar' ) ;
var zephyr _checked = false ;
$ ( "#zephyr-mirror-error" ) . is = function ( ) {
if ( ! zephyr _checked ) {
zephyr _checked = true ;
return true ;
}
return false ;
} ;
assert ( ! compose . validate ( ) ) ;
assert ( zephyr _checked ) ;
2017-06-29 20:48:49 +02:00
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'You need to be running Zephyr mirroring in order to send messages!' ) ) ;
2017-06-27 14:55:11 +02:00
compose _state . set _message _type ( 'private' ) ;
compose _state . recipient ( '' ) ;
$ ( "#private_message_recipient" ) . select ( noop ) ;
assert ( ! compose . validate ( ) ) ;
2017-06-29 20:48:49 +02:00
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'Please specify at least one recipient' ) ) ;
2017-06-27 14:55:11 +02:00
compose _state . recipient ( 'foo@zulip.com' ) ;
global . page _params . realm _is _zephyr _mirror _realm = true ;
assert ( compose . validate ( ) ) ;
global . page _params . realm _is _zephyr _mirror _realm = false ;
assert ( ! compose . validate ( ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'The recipient foo@zulip.com is not valid' , { } ) ) ;
compose _state . recipient ( 'foo@zulip.com,alice@zulip.com' ) ;
assert ( ! compose . validate ( ) ) ;
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'The recipients foo@zulip.com,alice@zulip.com are not valid' , { } ) ) ;
people . add _in _realm ( bob ) ;
compose _state . recipient ( 'bob@example.com' ) ;
assert ( compose . validate ( ) ) ;
compose _state . set _message _type ( 'stream' ) ;
compose _state . stream _name ( '' ) ;
$ ( "#stream" ) . select ( noop ) ;
assert ( ! compose . validate ( ) ) ;
2017-06-29 20:48:49 +02:00
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'Please specify a stream' ) ) ;
2017-06-27 14:55:11 +02:00
compose _state . stream _name ( 'Denmark' ) ;
global . page _params . realm _mandatory _topics = true ;
compose _state . subject ( '' ) ;
$ ( "#subject" ) . select ( noop ) ;
assert ( ! compose . validate ( ) ) ;
2017-06-29 20:48:49 +02:00
assert . equal ( $ ( '#error-msg' ) . html ( ) , i18n . t ( 'Please specify a topic' ) ) ;
2017-06-27 14:55:11 +02:00
} ( ) ) ;
2017-06-28 21:36:57 +02:00
( function test _get _invalid _recipient _emails ( ) {
var feedback _bot = {
email : 'feedback@example.com' ,
user _id : 124 ,
full _name : 'Feedback Bot' ,
} ;
global . page _params . cross _realm _bots = [ feedback _bot ] ;
global . page _params . user _id = 30 ;
people . initialize ( ) ;
compose _state . recipient ( 'feedback@example.com' ) ;
assert . deepEqual ( compose . get _invalid _recipient _emails ( ) , [ ] ) ;
} ( ) ) ;
2017-06-28 20:28:18 +02:00
( function test _validate _stream _message ( ) {
// This test is in kind of continuation to test_validate but since it is
// primarly used to get coverage over functions called from validate()
// we are seperating it up in different test. Though their relative position
// of execution should not be changed.
global . page _params . realm _mandatory _topics = false ;
var sub = {
stream _id : 101 ,
name : 'social' ,
subscribed : true ,
} ;
stream _data . add _sub ( 'social' , sub ) ;
compose _state . stream _name ( 'social' ) ;
assert ( compose . validate ( ) ) ;
assert ( ! $ ( "#compose-all-everyone" ) . visible ( ) ) ;
assert ( ! $ ( "#send-status" ) . visible ( ) ) ;
stream _data . get _subscriber _count = function ( stream _name ) {
assert . equal ( stream _name , 'social' ) ;
return 16 ;
} ;
global . templates . render = function ( template _name , data ) {
assert . equal ( template _name , 'compose_all_everyone' ) ;
assert . equal ( data . count , 16 ) ;
return 'compose_all_everyone_stub' ;
} ;
$ ( '#compose-all-everyone' ) . is = function ( sel ) {
if ( sel === ':visible' ) {
return $ ( '#compose-all-everyone' ) . visible ( ) ;
}
} ;
var compose _content ;
$ ( '#compose-all-everyone' ) . append = function ( data ) {
compose _content = data ;
} ;
compose _state . message _content ( 'Hey @all' ) ;
assert ( ! compose . validate ( ) ) ;
assert . equal ( $ ( "#compose-send-button" ) . attr ( 'disabled' ) , undefined ) ;
assert ( ! $ ( "#send-status" ) . visible ( ) ) ;
assert . equal ( compose _content , 'compose_all_everyone_stub' ) ;
assert ( $ ( "#compose-all-everyone" ) . visible ( ) ) ;
} ( ) ) ;
2017-06-29 06:54:10 +02:00
( function test _send _message _success ( ) {
blueslip . error = noop ;
blueslip . log = noop ;
$ ( "#new_message_content" ) . val ( 'foobarfoobar' ) ;
$ ( "#new_message_content" ) . blur ( ) ;
$ ( "#send-status" ) . show ( ) ;
$ ( "#compose-send-button" ) . attr ( 'disabled' , 'disabled' ) ;
$ ( "#sending-indicator" ) . show ( ) ;
global . feature _flags . log _send _times = true ;
global . feature _flags . collect _send _times = true ;
var set _timeout _called = false ;
global . patch _builtin ( 'setTimeout' , function ( func , delay ) {
assert . equal ( delay , 5000 ) ;
func ( ) ;
set _timeout _called = true ;
} ) ;
var server _events _triggered ;
global . server _events = {
restart _get _events : function ( ) {
server _events _triggered = true ;
} ,
} ;
var reify _message _id _checked ;
echo . reify _message _id = function ( local _id , message _id ) {
assert . equal ( local _id , 1001 ) ;
assert . equal ( message _id , 12 ) ;
reify _message _id _checked = true ;
} ;
var test _date = 'Wed Jun 28 2017 22:12:48 GMT+0000 (UTC)' ;
compose . send _message _success ( 1001 , 12 , new Date ( test _date ) , false ) ;
assert . equal ( $ ( "#new_message_content" ) . val ( ) , '' ) ;
assert ( $ ( "#new_message_content" ) . is _focused ( ) ) ;
assert ( ! $ ( "#send-status" ) . visible ( ) ) ;
assert . equal ( $ ( "#compose-send-button" ) . attr ( 'disabled' ) , undefined ) ;
assert ( ! $ ( "#sending-indicator" ) . visible ( ) ) ;
assert . equal ( _ . keys ( compose . send _times _data ) . length , 1 ) ;
assert . equal ( compose . send _times _data [ 12 ] . start . getTime ( ) , new Date ( test _date ) . getTime ( ) ) ;
assert ( ! compose . send _times _data [ 12 ] . locally _echoed ) ;
assert ( reify _message _id _checked ) ;
assert ( server _events _triggered ) ;
assert ( set _timeout _called ) ;
} ( ) ) ;
2017-06-29 14:02:42 +02:00
( function test _mark _rendered _content _disparity ( ) {
compose . mark _rendered _content _disparity ( 13 , true ) ;
assert . deepEqual ( compose . send _times _data [ 13 ] , { rendered _content _disparity : true } ) ;
} ( ) ) ;
2017-06-29 14:31:26 +02:00
( function test _report _as _received ( ) {
var msg = {
id : 12 ,
sent _by _me : true ,
} ;
var set _timeout _called = false ;
global . patch _builtin ( 'setTimeout' , function ( func , delay ) {
assert . equal ( delay , 0 ) ;
func ( ) ;
set _timeout _called = true ;
} ) ;
compose . send _times _data [ 12 ] . locally _echoed = true ;
channel . post = function ( payload ) {
assert . equal ( payload . url , '/json/report_send_time' ) ;
assert . equal ( typeof ( payload . data . time ) , 'string' ) ;
assert ( payload . data . locally _echoed ) ;
assert ( ! payload . data . rendered _content _disparity ) ;
} ;
compose . report _as _received ( msg ) ;
assert . equal ( typeof ( compose . send _times _data [ 12 ] . received ) , 'object' ) ;
assert . equal ( typeof ( compose . send _times _data [ 12 ] . displayed ) , 'object' ) ;
assert ( set _timeout _called ) ;
delete compose . send _times _data [ 13 ] ;
msg . id = 13 ;
compose . report _as _received ( msg ) ;
assert . equal ( typeof ( compose . send _times _data [ 13 ] . received ) , 'object' ) ;
assert . equal ( typeof ( compose . send _times _data [ 13 ] . displayed ) , 'object' ) ;
} ( ) ) ;
2017-02-24 16:18:56 +01:00
( function test _set _focused _recipient ( ) {
var sub = {
stream _id : 101 ,
name : 'social' ,
subscribed : true ,
} ;
stream _data . add _sub ( 'social' , sub ) ;
var page = {
'#stream' : 'social' ,
'#subject' : 'lunch' ,
'#new_message_content' : 'burrito' ,
'#private_message_recipient' : 'alice@example.com, bob@example.com' ,
} ;
global . $ = function ( selector ) {
return {
val : function ( ) {
return page [ selector ] ;
} ,
} ;
} ;
2017-04-24 20:35:26 +02:00
global . compose _state . get _message _type = function ( ) {
2017-02-24 16:18:56 +01:00
return 'stream' ;
} ;
global . $ . trim = function ( s ) {
return s ;
} ;
2017-03-29 08:54:26 +02:00
var message = compose . create _message _object ( ) ;
2017-02-24 16:18:56 +01:00
assert . equal ( message . to , 'social' ) ;
assert . equal ( message . subject , 'lunch' ) ;
assert . equal ( message . content , 'burrito' ) ;
2017-04-24 20:35:26 +02:00
global . compose _state . get _message _type = function ( ) {
2017-02-24 16:18:56 +01:00
return 'private' ;
} ;
2017-03-29 08:54:26 +02:00
message = compose . create _message _object ( ) ;
2017-02-24 16:18:56 +01:00
assert . deepEqual ( message . to , [ 'alice@example.com' , 'bob@example.com' ] ) ;
2017-02-24 23:51:23 +01:00
assert . equal ( message . to _user _ids , '31,32' ) ;
2017-02-24 16:18:56 +01:00
assert . equal ( message . content , 'burrito' ) ;
} ( ) ) ;