@@ -244,7 +244,6 @@ global.URL = window.URL = {
244244 revokeObjectURL : function ( ) { }
245245} ;
246246
247-
248247require ( '../js/jquery.terminal-src' ) ( global . $ ) ;
249248require ( '../js/unix_formatting' ) ( global . $ ) ;
250249require ( '../js/xml_formatting' ) ( global . $ ) ;
@@ -3441,33 +3440,48 @@ describe('Terminal plugin', function() {
34413440 } ) ;
34423441 } ) ;
34433442 describe ( 'renderHandler' , function ( ) {
3444- class Foo {
3445- valueOf ( ) {
3446- return "Hello" ;
3443+ describe ( 'default' , ( ) => {
3444+ class Foo {
3445+ valueOf ( ) {
3446+ return "Hello" ;
3447+ }
34473448 }
3448- }
3449- let term ;
3450- beforeEach ( ( ) => {
3451- term = $ ( '<div/>' ) . terminal ( function ( ) {
3452- return new Foo ( ) ;
3453- } , {
3454- renderHandler ( value ) {
3455- if ( value instanceof Foo ) {
3456- this . echo ( value . valueOf ( ) ) ;
3457- return false ;
3458- }
3459- } ,
3460- greetings : false
3449+ let term ;
3450+ beforeEach ( ( ) => {
3451+ term = $ ( '<div/>' ) . terminal ( function ( ) {
3452+ return new Foo ( ) ;
3453+ } , {
3454+ renderHandler ( value ) {
3455+ if ( value instanceof Foo ) {
3456+ this . echo ( value . valueOf ( ) ) ;
3457+ return false ;
3458+ }
3459+ } ,
3460+ greetings : false
3461+ } ) ;
3462+ } ) ;
3463+ it ( 'should render object from echo' , ( ) => {
3464+ term . echo ( new Foo ( ) ) ;
3465+ expect ( term . get_output ( ) ) . toEqual ( 'Hello' ) ;
3466+ } ) ;
3467+ it ( 'should render object from interpreter' , ( ) => {
3468+ term . exec ( 'foo' , true ) ;
3469+ expect ( term . get_output ( ) ) . toEqual ( 'Hello' ) ;
3470+ expect ( term . paused ( ) ) . toBeFalsy ( ) ; // #857
34613471 } ) ;
34623472 } ) ;
3463- it ( 'should render object from echo' , ( ) => {
3464- term . echo ( new Foo ( ) ) ;
3465- expect ( term . get_output ( ) ) . toEqual ( 'Hello' ) ;
3466- } ) ;
3467- it ( 'should render object from interpreter' , ( ) => {
3468- term . exec ( 'foo' , true ) ;
3469- expect ( term . get_output ( ) ) . toEqual ( 'Hello' ) ;
3470- expect ( term . paused ( ) ) . toBeFalsy ( ) ; // #857
3473+ describe ( 'resursive' , ( ) => {
3474+ it ( 'should render sync echo' , ( ) => {
3475+ const term = $ ( '<div/>' ) . terminal ( { } , {
3476+ renderHandler ( value ) {
3477+ this . echo ( `Hello ${ value } ` ) ;
3478+ return false ;
3479+ } ,
3480+ greetings : false
3481+ } ) ;
3482+ term . echo ( 'Terminal' ) ;
3483+ expect ( term . get_output ( ) ) . toEqual ( 'Hello Terminal' ) ;
3484+ } ) ;
34713485 } ) ;
34723486 } ) ;
34733487 describe ( 'pauseEvents' , function ( ) {
@@ -3672,6 +3686,182 @@ describe('Terminal plugin', function() {
36723686 } ;
36733687 }
36743688 describe ( 'events' , function ( ) {
3689+ describe ( 'abort signals' , ( ) => {
3690+ function ctrl_d ( ) {
3691+ // invoke_key doesn't work here because this shortcut don't use keymap
3692+ shortcut ( true , false , false , 'd' ) ;
3693+ }
3694+ const response = 'jQuery Terminal' ;
3695+ const fetch = function ( url , { signal } ) {
3696+ return new Promise ( ( resolve , reject ) => {
3697+ if ( signal . aborted ) {
3698+ reject ( signal . reason ) ;
3699+ }
3700+ setTimeout ( ( ) => {
3701+ resolve ( {
3702+ text : function ( ) {
3703+ return Promise . resolve ( response ) ;
3704+ }
3705+ } ) ;
3706+ } , 100 ) ;
3707+ signal . addEventListener ( "abort" , ( ) => {
3708+ reject ( signal . reason ) ;
3709+ } ) ;
3710+ } ) ;
3711+ }
3712+ function terminal ( fetch , options = { } ) {
3713+ return $ ( '<div/>' ) . terminal ( async function ( ) {
3714+ const res = await fetch . call ( this ) ;
3715+ const text = await res . text ( ) ;
3716+ this . echo ( text ) ;
3717+ } , { greetings : false , ...options } ) ;
3718+ }
3719+ describe ( 'fetch with abort' , ( ) => {
3720+ let term ;
3721+ beforeEach ( ( ) => {
3722+ term = terminal ( function ( ) {
3723+ const signal = this . signal ( ) ;
3724+ return fetch ( 'https://terminal.jcubic.pl' , { signal } ) ;
3725+ } ) ;
3726+ } ) ;
3727+ it ( 'should abort fetch on CTRL+D' , async ( ) => {
3728+ term . insert ( 'fetch' ) ;
3729+ enter_key ( ) ;
3730+ ctrl_d ( ) ;
3731+ await delay ( 200 ) ;
3732+ expect ( term . get_output ( ) ) . toMatch ( / > f e t c h \n .* A b o r t w i t h C T R L \+ D / ) ;
3733+ } ) ;
3734+ it ( 'should fetch the data' , async ( ) => {
3735+ term . insert ( 'fetch' ) ;
3736+ enter_key ( ) ;
3737+ await delay ( 200 ) ;
3738+ expect ( term . get_output ( ) ) . toEqual ( '> fetch\njQuery Terminal' ) ;
3739+ } ) ;
3740+ it ( 'should change abort message' , async ( ) => {
3741+ term . insert ( 'fetch' ) ;
3742+ enter_key ( ) ;
3743+ term . abort ( 'Nasty Abort' ) ;
3744+ await delay ( 200 ) ;
3745+ expect ( term . get_output ( ) ) . toMatch ( / > f e t c h \n .* N a s t y A b o r t / ) ;
3746+ } ) ;
3747+ } ) ;
3748+ describe ( 'fetch with timeout' , ( ) => {
3749+ let term ;
3750+ beforeEach ( ( ) => {
3751+ term = terminal ( function ( ) {
3752+ const signal = this . timeout ( 100 ) ;
3753+ return fetch ( 'https://terminal.jcubic.pl' , { signal } ) ;
3754+ } ) ;
3755+ } ) ;
3756+ it ( 'should abort fetch with error message' , async ( ) => {
3757+ term . insert ( 'fetch' ) ;
3758+ enter_key ( ) ;
3759+ await delay ( 200 ) ;
3760+ expect ( term . get_output ( ) ) . toMatch ( / > f e t c h \n .* S i g n a l t i m e d o u t / ) ;
3761+ } ) ;
3762+ it ( 'should abort the timer with CTRL+D' , async ( ) => {
3763+ term . insert ( 'fetch' ) ;
3764+ enter_key ( ) ;
3765+ ctrl_d ( ) ;
3766+ await delay ( 200 ) ;
3767+ expect ( term . get_output ( ) ) . toMatch ( / > f e t c h \n .* A b o r t w i t h C T R L \+ D / ) ;
3768+ } ) ;
3769+ } ) ;
3770+ describe ( 'catch exceptions' , ( ) => {
3771+ let term ;
3772+ beforeEach ( ( ) => {
3773+ term = $ ( '<div/>' ) . terminal ( async function ( ) {
3774+ try {
3775+ const signal = this . timeout ( 100 ) ;
3776+ const res = await fetch ( 'https://terminal.jcubic.pl' , { signal } ) ;
3777+ const text = await res . text ( ) ;
3778+ this . echo ( text ) ;
3779+ } catch ( err ) {
3780+ if ( ! [ 'AbortError' , 'TimeoutError' ] . includes ( err . name ) ) {
3781+ throw err ;
3782+ }
3783+ }
3784+ } , { greetings : false } ) ;
3785+ } ) ;
3786+ it ( 'should catch timeout' , async ( ) => {
3787+ term . insert ( 'fetch' ) ;
3788+ enter_key ( ) ;
3789+ await delay ( 200 ) ;
3790+ expect ( term . get_output ( ) ) . toEqual ( '> fetch' ) ;
3791+ } ) ;
3792+ it ( 'should catch abort timeout' , async ( ) => {
3793+ term . insert ( 'fetch' ) ;
3794+ enter_key ( ) ;
3795+ ctrl_d ( ) ;
3796+ await delay ( 200 ) ;
3797+ expect ( term . get_output ( ) ) . toEqual ( '> fetch' ) ;
3798+ } ) ;
3799+ } ) ;
3800+ describe ( 'ignore errors' , ( ) => {
3801+ let term ;
3802+ beforeEach ( ( ) => {
3803+ term = terminal ( function ( ) {
3804+ const signal = this . timeout ( 100 ) ;
3805+ return fetch ( 'https://terminal.jcubic.pl' , { signal } ) ;
3806+ } , { errorOnAbort : false } ) ;
3807+ } ) ;
3808+ it ( 'should ingore timeout' , async ( ) => {
3809+ term . insert ( 'fetch' ) ;
3810+ enter_key ( ) ;
3811+ await delay ( 200 ) ;
3812+ expect ( term . get_output ( ) ) . toEqual ( '> fetch' ) ;
3813+ } ) ;
3814+ it ( 'should ingore abort' , async ( ) => {
3815+ term . insert ( 'fetch' ) ;
3816+ enter_key ( ) ;
3817+ ctrl_d ( ) ;
3818+ await delay ( 200 ) ;
3819+ expect ( term . get_output ( ) ) . toEqual ( '> fetch' ) ;
3820+ } ) ;
3821+ it ( 'should show error' , async ( ) => {
3822+ const term = terminal ( async function ( ) {
3823+ throw new Error ( 'Nasty Error' ) ;
3824+ } , { errorOnAbort : false } ) ;
3825+ term . insert ( 'fetch' ) ;
3826+ enter_key ( ) ;
3827+ expect ( term . get_output ( ) ) . toEqual ( '> fetch' ) ;
3828+ } ) ;
3829+ } ) ;
3830+ describe ( 'read' , ( ) => {
3831+ let term ;
3832+ beforeEach ( ( ) => {
3833+ term = $ ( '<div/>' ) . terminal ( { } , {
3834+ greetings : false
3835+ } ) ;
3836+ } ) ;
3837+ it ( 'should aboort with CTRL+D and default signal' , async ( ) => {
3838+ expect . assertions ( 1 ) ;
3839+ try {
3840+ const ret = term . read ( '$ ' ) ;
3841+ setTimeout ( ( ) => {
3842+ term . focus ( ) ;
3843+ ctrl_d ( ) ;
3844+ } , 0 ) ;
3845+ await ret ;
3846+ } catch ( err ) {
3847+ expect ( err . name ) . toEqual ( 'AbortError' ) ;
3848+ }
3849+ } ) ;
3850+ it ( 'should timeout' , async ( ) => {
3851+ expect . assertions ( 1 ) ;
3852+ try {
3853+ await term . read ( '$ ' , { signal : term . timeout ( 100 ) } ) ;
3854+ } catch ( err ) {
3855+ expect ( err . name ) . toEqual ( 'TimeoutError' ) ;
3856+ }
3857+ } ) ;
3858+ it ( 'should read value' , async ( ) => {
3859+ const ret = term . read ( '$ ' , { signal : term . timeout ( 100 ) } ) ;
3860+ term . exec ( 'foo bar' ) ;
3861+ expect ( await ret ) . toEqual ( 'foo bar' ) ;
3862+ } ) ;
3863+ } ) ;
3864+ } ) ;
36753865 describe ( 'click' , function ( ) {
36763866 var term = $ ( '<div/>' ) . terminal ( $ . noop , { greetings : false , clickTimeout : 0 } ) ;
36773867 var cmd = term . cmd ( ) ;
@@ -6265,9 +6455,9 @@ describe('Terminal plugin', function() {
62656455 term . clear ( ) . echo ( input ) ;
62666456 expect ( output ( ) . join ( '\n' ) ) . toEqual ( input ) ;
62676457 } ) ;
6268- it ( 'should print undefined' , function ( ) {
6458+ it ( 'should not print undefined' , function ( ) {
62696459 term . clear ( ) . echo ( undefined ) ;
6270- expect ( output ( ) . join ( '\n' ) ) . toEqual ( 'undefined ' ) ;
6460+ expect ( output ( ) . join ( '\n' ) ) . toEqual ( '' ) ;
62716461 } ) ;
62726462 it ( 'should print value from function that return promise' , function ( done ) {
62736463 var term = $ ( '<div/>' ) . terminal ( ) ;
0 commit comments