2013-08-29 21:33:26 +02:00
var assert = require ( 'assert' ) ;
add _dependencies ( {
_ : 'third/underscore/underscore.js'
} ) ;
set _global ( 'page_params' , {
alert _words : [ 'alertone' , 'alerttwo' , 'alertthree' , 'al*rt.*s' , '.+' ] ,
email : 'tester@zulip.com'
} ) ;
2013-09-03 15:16:00 +02:00
set _global ( 'feature_flags' , {
alert _words : true
} ) ;
2013-08-29 21:33:26 +02:00
var alert _words = require ( 'js/alert_words.js' ) ;
var regular _message = { sender _email : 'another@zulip.com' , content : '<p>a message</p>' ,
flags : [ ] } ;
var own _message = { sender _email : 'tester@zulip.com' , content : '<p>hey this message alertone</p>' ,
flags : [ 'has_alert_word' ] } ;
var other _message = { sender _email : 'another@zulip.com' , content : '<p>another alertone message</p>' ,
flags : [ 'has_alert_word' ] } ;
var caps _message = { sender _email : 'another@zulip.com' , content : '<p>another ALERTtwo message</p>' ,
flags : [ 'has_alert_word' ] } ;
var alertwordboundary _message = { sender _email : 'another@zulip.com' ,
content : '<p>another alertthreemessage</p>' , flags : [ ] } ;
var multialert _message = { sender _email : 'another@zulip.com' , content :
'<p>another alertthreemessage alertone and then alerttwo</p>' ,
flags : [ 'has_alert_word' ] } ;
var unsafe _word _message = { sender _email : 'another@zulip.com' , content : '<p>gotta al*rt.*s all</p>' ,
flags : [ 'has_alert_word' ] } ;
2013-10-09 20:09:48 +02:00
var alert _in _url _message = { sender _email : 'another@zulip.com' , content : '<p>http://www.google.com/alertone/me</p>' ,
flags : [ 'has_alert_word' ] } ;
var question _word _message = { sender _email : 'another@zulip.com' , content : '<p>still alertone? me</p>' ,
flags : [ 'has_alert_word' ] } ;
2013-08-29 21:33:26 +02:00
( function test _notifications ( ) {
assert . equal ( alert _words . notifies ( regular _message ) , false ) ;
assert . equal ( alert _words . notifies ( own _message ) , false ) ;
assert . equal ( alert _words . notifies ( other _message ) , true ) ;
assert . equal ( alert _words . notifies ( caps _message ) , true ) ;
assert . equal ( alert _words . notifies ( alertwordboundary _message ) , false ) ;
assert . equal ( alert _words . notifies ( multialert _message ) , true ) ;
assert . equal ( alert _words . notifies ( unsafe _word _message ) , true ) ;
} ( ) ) ;
( function test _munging ( ) {
var saved _content = regular _message . content ;
alert _words . process _message ( regular _message ) ;
assert . equal ( saved _content , regular _message . content ) ;
saved _content = alertwordboundary _message . content ;
alert _words . process _message ( alertwordboundary _message ) ;
assert . equal ( alertwordboundary _message . content , saved _content ) ;
alert _words . process _message ( other _message ) ;
assert . equal ( other _message . content , "<p>another <span class='alert-word'>alertone</span> message</p>" ) ;
alert _words . process _message ( caps _message ) ;
assert . equal ( caps _message . content , "<p>another <span class='alert-word'>ALERTtwo</span> message</p>" ) ;
alert _words . process _message ( multialert _message ) ;
assert . equal ( multialert _message . content , "<p>another alertthreemessage <span class='alert-word'>alertone</span> and then <span class='alert-word'>alerttwo</span></p>" ) ;
alert _words . process _message ( unsafe _word _message ) ;
assert . equal ( unsafe _word _message . content , "<p>gotta <span class='alert-word'>al*rt.*s</span> all</p>" ) ;
2013-10-09 20:09:48 +02:00
alert _words . process _message ( alert _in _url _message ) ;
assert . equal ( alert _in _url _message . content , "<p>http://www.google.com/alertone/me</p>" ) ;
alert _words . process _message ( question _word _message ) ;
assert . equal ( question _word _message . content , "<p>still <span class='alert-word'>alertone</span>? me</p>" ) ;
2013-08-29 21:33:26 +02:00
} ( ) ) ;