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' ] } ;
( 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>" ) ;
} ( ) ) ;