2021-03-05 22:45:18 +01:00
"use strict" ;
const { strict : assert } = require ( "assert" ) ;
2021-03-06 19:05:17 +01:00
const { mock _module , set _global , zrequire } = require ( "../zjsunit/namespace" ) ;
2021-03-05 22:45:18 +01:00
const { run _test } = require ( "../zjsunit/test" ) ;
const $ = require ( "../zjsunit/zjquery" ) ;
const events = require ( "./lib/events" ) ;
const resize = {
watch _manual _resize ( ) { } ,
} ;
2021-03-06 19:05:17 +01:00
const channel = mock _module ( "channel" ) ;
2021-03-05 22:45:18 +01:00
const page _params = set _global ( "page_params" , { } ) ;
2021-03-06 19:05:17 +01:00
const upload = mock _module ( "upload" ) ;
mock _module ( "resize" , resize ) ;
2021-03-05 22:45:18 +01:00
set _global ( "csrf_token" , "fake-csrf-token" ) ;
set _global ( "document" , {
execCommand ( ) {
return false ;
} ,
location : { } ,
to _$ : ( ) => $ ( "document-stub" ) ,
} ) ;
const server _events _dispatch = zrequire ( "server_events_dispatch" ) ;
const compose _ui = zrequire ( "compose_ui" ) ;
const compose = zrequire ( "compose" ) ;
function stub _out _video _calls ( ) {
const elem = $ ( "#below-compose-content .video_link" ) ;
elem . toggle = ( show ) => {
if ( show ) {
elem . show ( ) ;
} else {
elem . hide ( ) ;
}
} ;
}
page _params . realm _available _video _chat _providers = {
disabled : {
id : 0 ,
name : "disabled" ,
} ,
jitsi _meet : {
id : 1 ,
name : "Jitsi Meet" ,
} ,
zoom : {
id : 3 ,
name : "Zoom" ,
} ,
big _blue _button : {
id : 4 ,
name : "Big Blue Button" ,
} ,
} ;
run _test ( "videos" , ( override ) => {
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . disabled . id ;
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
stub _out _video _calls ( ) ;
compose . initialize ( ) ;
( function test _no _provider _video _link _compose _clicked ( ) {
let called = false ;
const textarea = $ . create ( "target-stub" ) ;
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
compose _ui . _ _Rewire _ _ ( "insert_syntax_and_focus" , ( ) => {
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
handler ( ev ) ;
assert ( ! called ) ;
} ) ( ) ;
( function test _jitsi _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
const textarea = $ . create ( "jitsi-target-stub" ) ;
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
compose _ui . _ _Rewire _ _ ( "insert_syntax_and_focus" , ( syntax ) => {
syntax _to _insert = syntax ;
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . jitsi _meet . id ;
page _params . jitsi _server _url = null ;
handler ( ev ) ;
assert ( ! called ) ;
page _params . jitsi _server _url = "https://meet.jit.si" ;
handler ( ev ) ;
// video link ids consist of 15 random digits
const video _link _regex = /\[translated: Click to join video call]\(https:\/\/meet.jit.si\/\d{15}\)/ ;
assert ( called ) ;
assert . match ( syntax _to _insert , video _link _regex ) ;
} ) ( ) ;
( function test _zoom _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
const textarea = $ . create ( "zoom-target-stub" ) ;
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
compose _ui . _ _Rewire _ _ ( "insert_syntax_and_focus" , ( syntax ) => {
syntax _to _insert = syntax ;
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . zoom . id ;
page _params . has _zoom _token = false ;
window . open = ( url ) => {
assert ( url . endsWith ( "/calls/zoom/register" ) ) ;
// The event here has value=true. We keep it in events.js to
// allow our tooling to verify its schema.
server _events _dispatch . dispatch _normal _event ( events . fixtures . has _zoom _token ) ;
} ;
channel . post = ( payload ) => {
assert . equal ( payload . url , "/json/calls/zoom/create" ) ;
payload . success ( { url : "example.zoom.com" } ) ;
return { abort : ( ) => { } } ;
} ;
handler ( ev ) ;
const video _link _regex = /\[translated: Click to join video call]\(example\.zoom\.com\)/ ;
assert ( called ) ;
assert . match ( syntax _to _insert , video _link _regex ) ;
} ) ( ) ;
( function test _bbb _video _link _compose _clicked ( ) {
let syntax _to _insert ;
let called = false ;
const textarea = $ . create ( "bbb-target-stub" ) ;
const ev = {
preventDefault : ( ) => { } ,
target : {
to _$ : ( ) => textarea ,
} ,
} ;
compose _ui . _ _Rewire _ _ ( "insert_syntax_and_focus" , ( syntax ) => {
syntax _to _insert = syntax ;
called = true ;
} ) ;
const handler = $ ( "body" ) . get _on _handler ( "click" , ".video_link" ) ;
$ ( "#compose-textarea" ) . val ( "" ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . big _blue _button . id ;
channel . get = ( options ) => {
assert ( options . url === "/json/calls/bigbluebutton/create" ) ;
options . success ( {
url :
"/calls/bigbluebutton/join?meeting_id=%22zulip-1%22&password=%22AAAAAAAAAA%22&checksum=%2232702220bff2a22a44aee72e96cfdb4c4091752e%22" ,
} ) ;
} ;
handler ( ev ) ;
const video _link _regex = /\[translated: Click to join video call]\(\/calls\/bigbluebutton\/join\?meeting_id=%22zulip-1%22&password=%22AAAAAAAAAA%22&checksum=%2232702220bff2a22a44aee72e96cfdb4c4091752e%22\)/ ;
assert ( called ) ;
assert . match ( syntax _to _insert , video _link _regex ) ;
} ) ( ) ;
} ) ;
run _test ( "test_video_chat_button_toggle disabled" , ( override ) => {
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . disabled . id ;
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , false ) ;
} ) ;
run _test ( "test_video_chat_button_toggle no url" , ( override ) => {
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . jitsi _meet . id ;
page _params . jitsi _server _url = null ;
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , false ) ;
} ) ;
run _test ( "test_video_chat_button_toggle enabled" , ( override ) => {
override ( upload , "setup_upload" , ( ) => { } ) ;
override ( upload , "feature_check" , ( ) => { } ) ;
page _params . realm _video _chat _provider =
page _params . realm _available _video _chat _providers . jitsi _meet . id ;
page _params . jitsi _server _url = "https://meet.jit.si" ;
compose . initialize ( ) ;
assert . equal ( $ ( "#below-compose-content .video_link" ) . visible ( ) , true ) ;
} ) ;